目录
掌握:进程间通信方式介绍、无名管道特点、无名管道创建、无名管道读写特性;有名管道特点、有名管道创建、有名管道读写
无名管道(pipe)
有名管道 (fifo)
信号(signal)
共享内存(mmap)
套接字(socket) 开销较大,但是常用
System V IPC 不需要过度关注
共享内存(share memory) 不需要过度关注
消息队列(message queue) 不需要过度关注
信号灯集(semaphore set) 不需要过度关注

无名管道创建 – pipe
- #include
- int pipe(int pfd[2]);
无名管道通信

示例:
- #include
- #include
- #include
- int main(){
- int pfd[2];
- int re;
- char buf[20]={0};
- pid_t pid;
- re = pipe(pfd);
- if(re<0){
- perror("pipe");
- return 0;
- }
- printf("%d,%d\n",pfd[0],pfd[1]);
-
- //因为是无名管道是亲缘关系间通信,所以使用fork创建一个子进程
- pid = fork();
- if(pid<0){
- perror("fork");
- return 0;
- }else if(pid>0){
- //close(pfd[0]); //父写
- while(1){
- strcpy(buf,"hhahahahah");
- write(pfd[1],buf,strlen(buf));
-
- sleep(1);
- }
-
- }else{
- //close(pfd[1]); //子读
- while(1){
- re=read(pfd[0],buf,20);
- if(re>0){
- printf("read pipe=%s\n",buf);
- }
- }
- }
-
- }
- //运行结果
示例2:2个子进程写,一个父进程读
- #include
- #include
- #include
- int main(){
- int pfd[2];
- int i;
- int re;
- char buf[40]={0};
- pid_t pid;
- re = pipe(pfd);
- if(re<0){
- perror("pipe");
- return 0;
- }
- printf("%d,%d\n",pfd[0],pfd[1]);
-
- for(i=0;i<2;i++){
- pid = fork();
- if(pid<0){
- perror("fork");
- return 0;
- }else if(pid>0){
-
- }else{
- break;
-
- }
- }
- if(i==2){
- close(pfd[1]);
- while(1){
- memset(buf,0,40);
- re=read(pfd[0],buf,40);
- if(re>0){
- printf("%s\n",buf);
- }
- }
- return 0;
- }
-
- if(i==1){
- close(pfd[0]);
- while(1){
- strcpy(buf,"this is 2 process");
- write(pfd[1],buf,strlen(buf));
- usleep(930000); //如果同时写,读会有一些字符串重叠乱码
- }
- return 0;
- }
- if(i==0){
- close(pfd[0]);
- while(1){
- strcpy(buf,"this is 1 process");
- write(pfd[1],buf,strlen(buf));
- sleep(1);
- }
-
- return 0;
- }
-
-
- }
重点理解i = 2 是父进程,通过fork的返回值区分父进程和子进程

无名管道注意事项:

读管道:
-管道中有数据,read返回实际读到的字节数。
-管道中无数据:
管道写端被全部关闭,read返回0 (好像读到文件结尾)
写端没有全部被关闭,read阻塞等待(不久的将来可能有数据递达,此时会让出cpu)
示例:写端关闭,读read=0

不关闭也不写,阻塞

写管道:
-管道读端全部被关闭, 进程异常终止(也可使用捕捉SIGPIPE信号,使进程不终止)
-管道读端没有全部关闭:
管道已满,write阻塞。(管道大小64K)
管道未满,write将数据写入,并返回实际写入的字节数。
示例:
- #include
- #include
- #include
- #include
- int main(){
- int pfd[2];
- int re;
- char buf[20]={0};
- pid_t pid;
- re = pipe(pfd);
- if(re<0){
- perror("pipe");
- return 0;
- }
- printf("%d,%d\n",pfd[0],pfd[1]);
- pid = fork();
- if(pid<0){
- perror("fork");
- return 0;
- }else if(pid>0){
- close(pfd[0]);
- // close(pfd[1]);
- int j=0;
- while(1){
- j++;
- strcpy(buf,"hhahahahah");
- for(int i=0;i<1000;i++){
- write(pfd[1],buf,strlen(buf));
- }
- printf("write %d times\n",j);
- sleep(1);
- }
-
- }else{
- close(pfd[1]);
- // close(pfd[0]);
- sleep(30000);
- exit(0);
- while(1){
- re=read(pfd[0],buf,20);
- if(re>0){
- printf("read pipe=%s\n",buf);
- }else if(re==0){
- printf("re=0\n");
- }
- }
- }
-
-
- }
如何获取无名管道的大小?
循环写入管道,直到阻塞
统计循环次数
读端不存在 有空间 无空间 管道断裂!
如何验证管道断裂(进程被信号结束)?
子进程写管道
父进程回收
有名管道创建 – mkfifo
- #include
- #include
- int mkfifo(const char *path, mode_t mode);
有名管道读写 – 示例
进程A:循环从键盘输入并写入有名管道myfifo,输入quit时退出
进程B:循环统计进程A每次写入myfifo的字符串的长度
有个管道的打开
- open(const char *path, O_RDONLY);//1
- open(const char *path, O_RDONLY | O_NONBLOCK);//2
- open(const char *path, O_WRONLY);//3
- open(const char *path, O_WRONLY | O_NONBLOCK);//4
示例:注意不要创建在windows与linux的共享目录,因为win不支持
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
-
- int main(){
- int re;
- int fd;
- char buf[32];
- re = mkfifo("/myfifo",0666);
- if(re<0){
- perror("mkfifo");
- //return 0;
- }
- fd = open("/myfifo",O_WRONLY|O_NONBLOCK);
- if(fd<0){
- perror("open");
- return 0;
- }
- printf("after open\n");
- while(1){
- fgets(buf,32,stdin);
- write(fd,buf,strlen(buf));
-
- }
- }
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
-
- int main(){
- int re;
- int fd;
- char buf[32];
- /*
- re = mkfifo("/myfifo",0666);
- if(re<0){
- perror("mkfifo");
- return 0;
- }
- */
- fd = open("/myfifo",O_RDONLY);
- if(fd<0){
- perror("open");
- return 0;
- }
- printf("after open\n");
- while(1){
-
- re=read(fd,buf,32);
- if(re>0){
- printf("read fifo=%s\n",buf);
- }else if(re==0){ //写退出,读程序的处理
- exit(0);
- }
-
- }
- }
读写效果:写终端操作输入,另一终端输出
- /* create_fifo.c */
- // 省略头文件
- int main(void)
- {
- if(mkfifo(“myfifo”, 0666) < 0)
- {
- perror(“mkfifo”);
- exit(-1);
- }
- return 0;
- }
注意事项:
2 第二个参数中的选项O_NONBLOCK,选项O_NONBLOCK表示非阻塞,加上这个选项后,表示open调用是非阻塞的,如果没有这个选项,则表示open调用是阻塞的。
3 对于以只读方式(O_RDONLY)打开的FIFO文件,如果open调用是阻塞的(即第二个参数为O_RDONLY),除非有一个进程以写方式打开同一个FIFO,否则它不会返回;如果open调用是非阻塞的的(即第二个参数为O_RDONLY | O_NONBLOCK),则即使没有其他进程以写方式打开同一个FIFO文件,open调用将成功并立即返回。
4 对于以只写方式(O_WRONLY)打开的FIFO文件,如果open调用是阻塞的(即第二个参数为O_WRONLY),open调用将被阻塞,直到有一个进程以只读方式打开同一个FIFO文件为止;如果open调用是非阻塞的(即第二个参数为O_WRONLY | O_NONBLOCK),open总会立即返回,但如果没有其他进程以只读方式打开同一个FIFO文件,open调用将返回-1,并且FIFO也不会被打开。
5 数据完整性,如果有多个进程写同一个管道,使用O_WRONLY方式打开管道,如果写入的数据长度小于等于PIPE_BUF(4K),那么或者写入全部字节,或者一个字节都不写入,系统就可以确保数据决不会交错在一起。