進程間的通信IPC介紹
進程間通信(IPC,InterProcess Communication)是指在不同進程之間傳播或交換信息。
IPC的方式通常有管道(包括無名管道和命名管道)、消息隊列、信號量、共享存儲、Socket、Streams等。其中 Socket和Streams支持不同主機上的兩個進程IPC。
一、管道
管道,通常指無名管道,是 UNIX 系統IPC最古老的形式。
(1)特點:
它是半雙工的(即數據只能在一個方向上流動),具有固定的讀端和寫端。它只能用于具有親緣關系的進程之間的通信(也是父子進程或者兄弟進程之間)。它可以看成是一種特殊的文件,對于它的讀寫也可以使用普通的read、write 等函數。但是它不是普通的文件,并不屬于其他任何文件系統,并且只存在于內存中。
(2)原型:
#include <unistd.h>
int pipe(int pipefd[2]);
// 返回值:若成功返回0,失敗返回-1
當一個管道建立時,它會創建兩個文件描述符:fd[0]為讀而打開,fd[1]為寫而打開。要關閉管道只需將這兩個文件描述符關閉即可。
#include<stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{//int pipe(int pipefd[2]);int fd[2];//兩個文件描述符pid_t pid;char *readbuf=NULL;readbuf=(char*)malloc(128);if(pipe(fd)==-1){//創建管道printf("creat pipe fail\n");}pid=fork();if(pid<0){printf("creat child fail\n");}else if(pid>0){sleep(3);printf("this is father\n");close(fd[0]);write(fd[1],"hello from father",strlen("hello from father"));wait(NULL);}else{printf("this is child\n");close(fd[1]);read(fd[0],readbuf,128);printf("read from father is %s\n",readbuf);exit(-1);
}
二、FIFO
FIFO,也稱為命名管道,它是一種文件類型。
特點:
FIFO可以在無關的進程之間交換數據,與無名管道不同。FIFO有路徑名與之相關聯,它以一種特殊設備文件形式存在于文件系統中。
原型:
#include <sys/stat.h>
// 返回值:成功返回0,出錯返回-1
int mkfifo(const char *pathname, mode_t mode);
其中的 mode 參數與open函數中的 mode 相同。一旦創建了一個 FIFO,就可以用一般的文件I/O函數操作它。pathname是文件名(管道名)。
當 open 一個FIFO時,是否設置非阻塞標志(O_NONBLOCK)的區別:
若沒有指定O_NONBLOCK(默認),只讀 open 要阻塞到某個其他進程為寫而打開此 FIFO。類似的,只寫 open 要阻塞到某個其他進程為讀而打開它。若指定了O_NONBLOCK,則只讀 open 立即返回。而只寫 open 將出錯返回 -1 如果沒有進程已經為讀而打開該 FIFO,其errno置ENXIO。
寫端代碼
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<errno.h>
#include <fcntl.h>
#include<string.h>
int main()
{//int mkfifo(char*pathname,mode_t mode);int fd;int cnt=0;char*str="message from file";fd=open("./file",O_WRONLY);while(1){write(fd,str,strlen(str));sleep(1);if(cnt==5){break;}}close(fd);printf("write open successful\n");return 0;
}
讀端代碼
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<errno.h>
#include <fcntl.h>
int main()
{//int mkfifo(char*pathname,mode_t mode);int fd;int nread=0;char buf[30]={0};if(mkfifo("./file",0600)==-1&& errno!=EEXIST){printf("mkfifo fail\n");perror("why");}fd=open("./file",O_RDONLY);//fd=open("./file",O_RDONLY|O_NONBLOCK);非堵塞方式打開printf("open successful\n");while(1){nread=read(fd,buf,20);//若沒有寫端將會堵塞在這里printf("read %d byte from file,context is%s\n",nread,buf);}close(fd);return 0;
}
詳細介紹