一. 管道:
1.只能用於具有親緣關系的進程之間的通信
2.半雙工通信模式
3.一種特殊的文件,是一種只存在於內核中的讀寫函數
管道基於文件描述符,管道建立時,有兩個文件描述符:
a. fd[0]: 固定用於讀管道
b. fd[1]: 固定用於寫管道
創建管道:pipe()
一般步驟:1. pipe()創建管道 2. fork()創建子進程 3. 子進程會繼承父進程的管道
關閉管道:1. 逐個關閉文件描述符 2. close()
eg. 父子進程間的管道通信:父子進程對管道分別有自己的讀寫通道,把無關的讀端或寫段關閉。

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6
7 #define MAX_DATA_LEN 256
8 #define DELAY_TIME 1
9
10 int main() {
11 pid_t pid;
12 char buf[MAX_DATA_LEN];
13 const char *data="Pipe Test program";
14 int real_read,real_write;
15 int pipe_fd[2];
16
17 memset((void*)buf,0,sizeof(buf));
18
19 if(pipe(pipe_fd)<0){
20 perror("Pipe create error!\n");
21 exit(1);
22 }
23
24 if ((pid=fork())<0) {
25 perror("Fork error!\n");
26 exit(1);
27 } else if (pid==0) {
28 close(pipe_fd[1]);
29 sleep(DELAY_TIME*3);
30
31 if ((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0) {
32 printf("Child receive %d bytes from pipe: '%s'.\n",real_read,buf);
33 }
34
35 close(pipe_fd[0]);
36 exit(0);
37 } else {
38 close(pipe_fd[0]);
39 sleep(DELAY_TIME);
40
41 if ((real_write=write(pipe_fd[1],data,strlen(data)))>0) {
42 printf("Parent write %d bytes into pipe: '%s'.\n",real_write,data);
43 }
44
45 close(pipe_fd[1]);
46 waitpid(pid,NULL,0);
47 exit(0);
48 }
49
50 }
二. 有名管道FIFO
1. 使不相關的兩個進程彼此通信:a. 通過路徑名指出,在文件系統中可見
b. 管道建立后,兩進程可按普通文件一樣對其操作
2. FIFO遵循先進先出規則:a. 對管道讀從開始處返回數據
b. 對管道寫則把數據添加到末尾
c. 不支持如lseek()等文件定位操作
創建有名管道:mkfifo()
創建管道成功后,可使用open()、read()和write()等函數。
為讀而打開的管道可在open()中設置O_RDONLY
為寫而打開的管道可在open()中設置O_WRONLY
與普通文件不同的是阻塞問題
•普通文件的讀寫時不會出現阻塞問題
•在管道的讀寫中卻有阻塞的可能,
•非阻塞標志:在open()函數中設定為O_NONBLOCK
l
阻塞打開與非阻塞打開
對於讀進程
•若該管道是阻塞打開,且當前FIFO內沒有數據,則對讀進程而言將一直阻塞到有數據寫入
•若該管道是非阻塞打開,則不論FIFO內是否有數據,讀進程都會立即執行讀操作。即如果FIFO內沒有數據,則讀函數將立刻返回0
對於寫進程
•若該管道是阻塞打開,則寫操作將一直阻塞到數據可以被寫入
•若該管道是非阻塞打開而不能寫入全部數據,則讀操作進行部分寫入或者調用失敗
eg. 寫FIFO與讀FIFO
View Code
View Code

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9
10 #define FIFO "myfifo"
11 #define BUFF_SIZE 1024
12
13 int main(int argc,char* argv[]) {
14 char buff[BUFF_SIZE];
15 int real_write;
16 int fd;
17
18 if(argc<=1){
19 printf("Usage: ./fifo_write string\n");
20 exit(1);
21 }
22
23 sscanf(argv[1],"%s",buff);
24
25 % 測試FIFO是否存在,若不存在,mkfifo一個FIFO
26 if(access(FIFO,F_OK)==-1){
27 if((mkfifo(FIFO,0666)<0)&&(errno!=EEXIST)){
28 printf("Can NOT create fifo file!\n");
29 exit(1);
30 }
31 }
32
33 % 調用open以只寫方式打開FIFO,返回文件描述符fd
34 if((fd=open(FIFO,O_WRONLY))==-1){
35 printf("Open fifo error!\n");
36 exit(1);
37 }
38
39 % 調用write將buff寫到文件描述符fd指向的FIFO中
40 if ((real_write=write(fd,buff,BUFF_SIZE))>0) {
41 printf("Write into pipe: '%s'.\n",buff);
42 exit(1);
43 }
44
45 close(fd);
46 exit(0);
47
48 }

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9
10 #define FIFO "myfifo"
11 #define BUFF_SIZE 1024
12
13 int main() {
14 char buff[BUFF_SIZE];
15 int real_read;
16 int fd;
17
18 %access確定文件或文件夾的訪問權限。即,檢查某個文件的存取方式
19 %如果指定的存取方式有效,則函數返回0,否則函數返回-1
20 %若不存在FIFO,則創建一個
21 if(access(FIFO,F_OK)==-1){
22 if((mkfifo(FIFO,0666)<0)&&(errno!=EEXIST)){
23 printf("Can NOT create fifo file!\n");
24 exit(1);
25 }
26 }
27
28 %以只讀方式打開FIFO,返回文件描述符fd
29 if((fd=open(FIFO,O_RDONLY))==-1){
30 printf("Open fifo error!\n");
31 exit(1);
32 }
33
34 % 調用read將fd指向的FIFO的內容,讀到buff中,並打印
35 while(1){
36 memset(buff,0,BUFF_SIZE);
37 if ((real_read=read(fd,buff,BUFF_SIZE))>0) {
38 printf("Read from pipe: '%s'.\n",buff);
39 }
40 }
41
42 close(fd);
43 exit(0);
44 }