管道:分为有名管道和无名管道;
区别:
有名管道在任意两个进程间通信,
无名管道在父子进程间通信;
1.有名管道 有名管道也成为命名管道;
(1)创建有名管道: mkfifo 管道名
打开管道:open();
关闭管道:close();
读数据:read();
写入数据:write();
(2)有名管道演示进程间通信
a.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <assert.h>
5 #include <fcntl.h>
6 #include <string.h>
7 int main()
8 {
9 int fd=open("fifo",O_WRONLY);
10 assert(fd!=-1);
11
12 printf("fd=%d\n",fd);
13
14 while(1)
15 {
16 printf("input:\n");
17 char buff[128];
18 fgets(buff,127,stdin);
19 if(strncmp(buff,"end",3)==0)
20 {
21 break;
22 }
23 write(fd,buff,strlen(buff));
24 }
25 close(fd);
26