IPC:
IPC, 即 Inter-Process Communication, 进程间通信是进程间通信的对象, 包括管道消息队列信号量套接字等关于 IPC 结构, 首先 IPC 结构是内核维护的, 不属于某个特定进程 IPC 结构由两个东西标识: 标识符 (ID) 和键 (key) 其中, ID 是 IPC 的内部名字, 只在进程间通信使用; key 是 IPC 的外部名字, 多个进程针对同一个 key 调用 get 函数时, 会得到相同的 ID, 即标识了同一个 IPC, 进程间就可以通过这个共同的 IPC 进行通信
无名管道:
无名管道只能用于两个具有公共祖先的进程之间, 即: 只能用于相关的两个进程通信, 不能用于不相关的两个进程通信常用实现函数是: int pipe(int fd[2])pipe 函数创建一个半双工管道 fd[0]用于进程读端, fd[1]用于进程写端, 一个进程的 fd[1]输出是另一个进程 fd[0]的输入实现例程如下:
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/types.h>
- #include<unistd.h>
- int main()
- {
- int fd[2];
- char line[MAX_LINE];
- if(pipe(fd)<0); // 建立管道, fd[1]写, fd[0]读返回 0 表示成功, 返回 - 1 表示失败
- err_sys("pipe erroe");
- if((pid = fork()) < 0) // 创建子进程
- err_sys("fork error");
- else if(pid > 0) // 父进程
- {
- close(fd[0]); // 关闭父进程的读端
- write(fd[1], "data", 4); // 父进程向 fd[1]描述符中写
- }
- else // 子进程
- {
- close(fd[1]); // 关闭子进程的写端
- int n = read(fd[0], line, MAX_LINE); // 子进程从 fd[0]描述符中读
- write(STDOUT_FIFENO, line, n); // 将读到的内容写到标准输出(cout)
- }
- exit(0);
- }
使用无名管道的优缺点: 优点: 相对于建立一个磁盘文件, 两个进程通过磁盘读写来进行通信的方法, 使用无名管道不需要创建中间临时磁盘文件, 提升了进程间通信的速度缺点: 无名管道只能在具有公共祖先的两个进程间使用, 即两个进程只能是父子进程或有共同父进程的两个子进程
有名管道(FIFO):
无名管道只能在两个相关的进程间使用(两个进程有公共祖先), 但是通过 FIFO, 不相关的进程也能交换数据进行通信常用的实现函数是 mkfifo(path, mode), 他创建一个 FIFO, 类似于创建一个文件, 路径名 path 位于文件系统 FIFIO 的使用类似于 CS 通信, write_fifo 相当于客户端, 向事先知道的管道发送消息; read_fifo 相当于服务器端, 创建一个众所周知的管道, 监控管道的读端, 当有数据到来时读出并处理实现例程如下:
- (1)read_fifo:
- #include<stdio.h>
- #include<stdlib.h>
- #include<errno.h>
- #include<fcntl.h>
- #include<sys/stat.h>
- int main()
- {
- int fd;
- int len;
- char buf[1024];
- if(mkfifo("fifo1", 0666) < 0 && errno!=EEXIST) // 创建 FIFO 管道
- perror("Create FIFO Failed");
- if((fd = open("fifo1", O_RDONLY)) < 0) // 以读打开 FIFO
- {
- perror("Open FIFO Failed");
- exit(1);
- }
- while((len = read(fd, buf, 1024)) > 0) // 读取 FIFO 管道
- printf("Read message: %s", buf);
- close(fd); // 关闭 FIFO 文件
- return 0;
- }
- (2)write_fifo:
- #include<stdio.h>
- #include<stdlib.h> // exit
- #include<fcntl.h> // O_WRONLY
- #include<sys/stat.h>
- int main()
- {
- int fd;
- printf("I am %d process.\n", getpid()); // 说明进程 ID
- if((fd = open("fifo1", O_WRONLY)) < 0) // 以写打开一个 FIFO
- {
- perror("Open FIFO Failed");
- exit(1);
- }
- char buf[10] = "hello,fifo";
- if(write(fd, buf, 11) < 0) // 写入到 FIFO 中
- {
- perror("Write FIFO Failed");
- close(fd);
- exit(1);
- }
- sleep(2); // 休眠 2 秒
- close(fd); // 关闭 FIFO 文件
- return 0;
- }
来源: http://www.bubuko.com/infodetail-2504914.html