进程间通信-管道
#include <unistd.h> int pipe(int [2]);
参数说明
返回值
原理
示例
1 #include<stdio.h> 2 #include<unistd.h> 3 #include<stdlib.h> 4 #include<string.h> 5 #include<signal.h> 6 #include<errno.h> 7 8 void childWrite(int fds[2]) 9 { 10 close(fds[0]); 11 char buf[1024]; 12 int i = 0; 13 while(1) 14 { 15 sprintf(buf, "Child Data%d", i++); 16 int nRet = write(fds[1], buf, strlen(buf)); 17 if(nRet == -1) 18 { 19 perror("write"); 20 exit(1); 21 } 22 23 printf("child write %d bytes\n", nRet); 24 sleep(1); 25 } 26 close(fds[1]); 27 } 28 29 void parentRead(int fds[2]) 30 { 31 close(fds[1]); 32 char buf[1024]; 33 while(1) 34 { 35 int nRet = read(fds[0], buf, sizeof(buf) - 1); 36 if(nRet == -1) 37 { 38 perror("read"); 39 exit(1); 40 } 41 buf[nRet] = '\0'; 42 printf("parent read : %s\n", buf); 43 sleep(1); 44 } 45 } 46 47 int main(int argc, char** argv) 48 { 49 50 int fds[2]; 51 if(pipe(fds) == -1) 52 { 53 perror("pipe"); 54 return -1; 55 } 56 57 pid_t pid = fork(); 58 if(pid == -1) 59 { 60 perror("fork"); 61 return -1; 62 } 63 else if(pid == 0) 64 childWrite(fds); 65 else 66 parentRead(fds); 67 68 return 0; 69 }
child write 11 bytes parent read : Child Data0 child write 11 bytes parent read : Child Data1 child write 11 bytes parent read : Child Data2 ...
获取管道最大容量
1 void childWrite(int fds[2]) 2 { 3 close(fds[0]); 4 char buf[1024]; 5 int i = 0; 6 while(1) 7 { 8 write(fds[1], "1", 1); 9 printf("write count : %d\n",++i); 10 } 11 close(fds[1]); 12 } 13 14 void parentRead(int fds[2]) 15 { 16 close(fds[1]); 17 while(1); 18 }
write count : 1 ... write count : 65536 //阻塞
$ ulimit -a ... pipe size (512 bytes, -p) 8 ...
总结
mkfifo [选项] <管道名称>
#include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode);
参数说明
返回值
$ stat myFifo 文件:myFifo 大小:0 块:0 IO 块:4096 先进先出 设备:805h/2053d Inode:1192546 硬链接:1 权限:(0664/prw-rw-r--) Uid:( 1000/ test) Gid:( 1000/ test) ... $ ls -l myFifo prw-rw-r-- 1 test test 0 8月 21 14:17 myFifo
1 #include<stdio.h> 2 #include<fcntl.h> 3 #include<sys/stat.h> 4 #include<unistd.h> 5 #include<errno.h> 6 7 int main(int argc, char** argv) 8 { 9 int nRet = mkfifo("./myfifo", 0666); 10 if(nRet == -1) 11 { 12 if(errno != EEXIST) 13 { 14 perror("mkfifo"); 15 return -1; 16 } 17 } 18 19 printf("create fifo success!\n"); 20 21 int fd = open("./myfifo", O_WRONLY); 22 if(fd == -1) 23 { 24 perror("open"); 25 return -1; 26 } 27 28 printf("open fd success!\n"); 29 return 0; 30 }
create fifo success! //阻塞
1 #include<stdio.h> 2 #include<fcntl.h> 3 #include<sys/stat.h> 4 #include<unistd.h> 5 #include<errno.h> 6 #include<string.h> 7 #include<signal.h> 8 9 void sigHandler(int sig) 10 { 11 printf("signal : %d\n", sig); 12 } 13 14 int main(int argc, char** argv) 15 { 16 signal(SIGPIPE, sigHandler); 17 18 if(mkfifo("./myfifo", 0666) == -1) 19 { 20 if(errno != EEXIST) 21 { 22 perror("mkfifo"); 23 return -1; 24 } 25 } 26 27 int fd = open("./myfifo", O_WRONLY); 28 char buf[32] = {0}; 29 int i = 0; 30 while(1) 31 { 32 sprintf(buf, "Data%d", ++i); 33 int nRet = write(fd, buf, strlen(buf)); 34 if(nRet == -1) 35 { 36 perror("write"); 37 return -1; 38 } 39 sleep(1); 40 } 41 42 return 0; 43 }
1 #include<stdio.h> 2 #include<fcntl.h> 3 #include<sys/stat.h> 4 #include<unistd.h> 5 #include<errno.h> 6 #include<string.h> 7 8 int main(int argc, char** argv) 9 { 10 if(mkfifo("./myfifo", 0666) == -1) 11 { 12 if(errno != EEXIST) 13 { 14 perror("mkfifo"); 15 return -1; 16 } 17 } 18 19 int fd = open("./myfifo", O_RDONLY); 20 char buf[32] = {0}; 21 while(1) 22 { 23 int nRet = read(fd, buf, sizeof(buf) - 1); 24 if(nRet == -1) 25 { 26 perror("read"); 27 return 0; 28 } 29 else if(nRet == 0) 30 { 31 printf("pipe has closed!\n"); 32 return 0; 33 } 34 printf("read : %s\n", buf); 35 sleep(1); 36 } 37 38 return 0; 39 }