Linus进程间通信(一)管道、命名管道的原理及实现
int pipe(int filedes[2]);
2. 父进程调用fork创建子进程,那么子进程也有两个蚊件描述符指向同一管道。
#include<stdio.h> #include<unistd.h> #include<errno.h> #include<string.h> int main() { int _pipe[2]; int ret = pipe(_pipe); if(ret == -1 ) { printf("create pipe failed!\n"); return 2; } pid_t id = fork(); if(id < 0) { printf("fork error!\n"); return 0; } else if(id == 0) //child { close(_pipe[1]); char mesg[1000]; int i = 0; while(i < 5) { memset(mesg, '\0', sizeof(mesg)); read(_pipe[0], mesg, sizeof(mesg)); printf("%s\n",mesg); i++; } } else//father { close(_pipe[0]); int j = 0; while(j < 5) { char *_mesg_c = "i am you father!!!"; write(_pipe[1], _mesg_c, strlen(_mesg_c)+1); sleep(2); j++; } } return 1; }
使用管道有一些限制:
#include<stdio.h> #include<unistd.h> #include<errno.h> #include<string.h> #include<sys/wait.h> int main() { int _pipe[2]; int ret = pipe(_pipe); if(ret == -1) { printf("create pipe error! errno code is: %d\n",errno); return 1; } pid_t id = fork(); if(id < 0) { printf("fork error!\n"); return 2; } else if(id == 0)//child { close(_pipe[0]); int i = 0; char *_mesg_c = NULL; while(i < 10) { _mesg_c = "i am child!"; write(_pipe[1], _mesg_c, strlen(_mesg_c)+1); sleep(1); i++; } close(_pipe[1]); } else//father { close(_pipe[1]); char _mesg[1000]; int j = 0; while(j < 100) { memset(_mesg, '\0', sizeof(_mesg)); int ret = read(_pipe[0], _mesg, sizeof(_mesg)); printf("%s, code is: %d\n",_mesg,ret); j++; } if(waitpid(id, NULL, 0) < 0) return 3; return 0; } }2、如果有指向管道写端的文件描述符没关闭(管道写端的引用计数大于0),而持有管道写进程也没有向管道中写数据,这时有进程从管道读端读数据,那么管道中剩余的数据都被读 取后,再次read会阻塞,直到管道中有数据可读了才读取数据并返回。
代码测试:
#include<stdio.h> #include<unistd.h> #include<errno.h> #include<string.h> #include<sys/wait.h> int main() { int _pipe[2]; int ret = pipe(_pipe); if(ret == -1) { printf("create pipe error! errno code is: %d\n",errno); return 1; } pid_t id = fork(); if(id < 0) { printf("fork error!\n"); return 2; } else if(id == 0)//child { close(_pipe[0]); int i = 0; char *_mesg_c = NULL; while(i < 20) { if(i < 10) { _mesg_c = "i am child!"; write(_pipe[1], _mesg_c, strlen(_mesg_c)+1); } sleep(1); i++; if(i >= 14) { _mesg_c = "i am child!"; write(_pipe[1], _mesg_c, strlen(_mesg_c)+1); } } close(_pipe[1]); } else//father { close(_pipe[1]); char _mesg[1000]; int j = 0; while(j < 20) { memset(_mesg, '\0', sizeof(_mesg)); int ret = read(_pipe[0], _mesg, sizeof(_mesg)); printf("%s, code is: %d\n",_mesg,ret); j++; } if(waitpid(id, NULL, 0) < 0) return 3; } <span style="white-space:pre"> </span>return 0; }
3. 如果所有指向管道读端的文件描述符都关闭了(管道读端的引用计数等于0),这时有进程向管道的写端write,那么该进程会收到信号SIGPIPE,通常会导致进程异常终止。
#include<stdio.h> #include<unistd.h> #include<errno.h> #include<string.h> #include<sys/wait.h> int main() { int _pipe[2]; int ret = pipe(_pipe); if(ret == -1) { printf("create pipe error! errno code is: %d\n",errno); return 1; } pid_t id = fork(); if(id < 0) { printf("fork error!\n"); return 2; } else if(id == 0)//child { close(_pipe[0]); int i = 0; char *_mesg_c = NULL; while(i < 20) { if(i < 10) { _mesg_c = "i am child!"; write(_pipe[1], _mesg_c, strlen(_mesg_c)+1); } sleep(1); i++; } } else//father { close(_pipe[1]); char _mesg[1000]; int j = 0; while(j < 3) { memset(_mesg, '\0', sizeof(_mesg)); int ret = read(_pipe[0], _mesg, sizeof(_mesg)); printf("%s, code is: %d\n",_mesg,ret); j++; } close(_pipe[0]); sleep(10); if(waitpid(id, NULL, 0) < 0) return 3; } return 0; }4. 如果有指向管道读端的文件描述符没关闭(管道读端的引用计数大于0),而持有管道读端 进程也没有从管道中读数据,这时有进程向管道写端写数据,那么在管道被写满时再 次write会阻塞,直到管道中有空位置了才写入数据并返回。
命名管道也被称为FIFO文件,它是一种特殊类型的文件,它在文件系统中以文件名的形式存在,但是它的行为却和之前所讲的没有名字的管道(匿名管道)类似。
我们可以使用两下函数之一来创建一个命名管道,他们的原型如下:
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *filename, mode_t mode);
#include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> #include<string.h> #define _PATH_ "/tmp/file.tmp" #define _SIZE_ 100 int main() { int ret = mkfifo(_PATH_, 0777); if( ret < -1) { printf(" mkfile error!\n"); return 1; } int fd = open(_PATH_, O_WRONLY); if(fd < 0) { printf("open error!\n"); } char buf[_SIZE_]; memset(buf, '\0', sizeof(buf)); while(1) { fflush(stdin); scanf("%s",buf); int ret = write(fd, buf, sizeof(buf)+1); if(ret < 0 ) { printf("write error!\n"); break; } if(strncmp(buf, "quit", 4) == 0) { break; } close(fd); return 0; } }
//fifo_read.c
#include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> #include<string.h> #define _PATH_ "/tmp/file.tmp" #define _SIZE_ 100 int main() { int fd = open(_PATH_, O_RDONLY); if(fd < 0) { printf("open file error!\n"); return 1; } char buf[_SIZE_]; memset(buf, '\0', sizeof(buf)); while(1) { fflush(stdout); int ret = read(fd, buf, sizeof(buf)); if(ret <= 0 ) { printf("read end or error!\n"); break; } printf("%s\n",buf); // fflush(stdout); //sleep(2); if(strncmp(buf, "quit", 4) == 0) { break; } } close(fd); return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。