先說一下有名管道和無名管道用的函數:
無名管道使用的是 pipe()
有名管道使用的是fifo()
無名管道主要用於有血緣關系的兩個進程間通信,是內核使用環形隊列機制實現,借助內核緩沖區實現的。
有名管道主要用於兩個不相干的進程間通信,我認為之所以叫有名管道是因為他們借助mkfifo()函數創建的偽文件利用內核緩沖區進行通信,因為創建文件可以指定文件名所以操作和使用文件幾乎一樣。
首先關於無名管道 pipe()函數 需要指定兩個文件描述符,通過pipe()函數創建一個管道使其一端讀文件一端寫
1 int fd[2]; 2 pipe(fd);
其中默認 fd[0]讀文件,fd[1]寫文件。
#include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> int main(void) { int fd[2]; int ret = pipe(fd); if (ret == -1) { perror("pipe error!"); exit(-1); } pid_t pid = fork(); if (pid == -1) { perror("fork error!"); exit(-1); } else if (pid == 0)//子進程讀數據 { close(fd[1]); char buf[1024]; ret = read(fd[0], buf, sizeof(buf)); if (ret == 0) printf("------\n"); write(STDOUT_FILENO, buf, ret); } else { close(fd[0]);// 父進程寫數據 char* str = "Hello pipe\n"; write(fd[1], str, strlen(str)); sleep(2); } }
而fifo()函數是一個進程寫數據,一個進程讀數據,兩個進程不能結束
一端讀數據
#include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> int main(void) { int fd = open("abc", O_RDONLY); char buf[1024]; while (1) { sleep(1); int ret = read(fd, buf, sizeof(buf)); if (ret == -1) { perror("read error"); exit(-1); } if (ret != 0) //如果ret == 0 那么內存緩沖區就是空的 printf("%s\n", buf); } close(fd); return 0; }
一個進程寫數據
#include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> int main(void) { int ret = access("abc", F_OK); int fd; if (ret != 0) { ret = mkfifo("abc", 0777); if (ret == -1) //如果返回值是-1說明文件不存在 我們就創建一個文件 { perror("mkfifo"); exit(-1); } } fd = open("abc", O_WRONLY); while(1) { sleep(1); char *str = "myname"; write(fd, str, sizeof(str)); } close(fd); return 0; }