标签:管道
进程是一个独立的单元,每个进程各自有不同的地址空间,任何一个进程的信息在另一个进程都是不可见的,所以两个进程间要想交换数据就必须通过内核。
而管道(pipe)就是一种实现进程间通信的一个中间介质。它是指用于连接一个读进程和一个写进程,以实现它们之间通信的一个文件。所以管道用于进程间单向通信。
管道分为匿名管道和命名管道。
匿名管道:主要用于父子进程间的通信或者兄弟进程等有亲戚关系的进程间的通信。因为只有有亲戚关系的两个进程才可以共享一个管道。存在于内存中。
函数:
#include <unistd.h>
int pipe(int filedes[2]);
特点:
1.是单向的,即两个进程有且只有一个读端和写端,不可以同时既是读端又是写端。
2.管道通信是面向流服务的。即它是以字符流的方式写入的。
3.数据被进程从管道读出后,在管道中该数据就不存在了。
4.当进程去读空管道时,进程会阻塞。
5.当进程往满管道里写数据时,进程会阻塞。
6.生命周期随进程。
7.只能用于本地,不可用于网络。
8.不支持异步读写操作。
命名管道(fifo):主要用于两个没有亲戚关系的两个进程之间的通信。它提供了一个文件的路径名。只要两个进程能访问这个路径,就能实现通信。 存在于硬盘中
主要用到的函数:
#include <sys/types.h>
#include <sys/stat.h>
int mknod(const char *path,mode_t mod,dev_t dev);
int mkfifo(const char *path,mode_t mode);//创建一个命名管道。
特点:
1.既可以单向通信又可以实现双向通信。
2.可以通过它的路径被引用。
3.支持多客户机连接。
4.既可以用于本地,也可以用于网络。
5.支持异步重叠I/O操作。
6.既可以以流形式传输数据,还可以将数据集合到称为消息的数据块中。
1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 #include<unistd.h>
5 int main()
6 {
7 int filedes[2]={0};
8 if(pipe(filedes)==-1)
9 {
10 perror("pipe");
11 exit(1);
12 }
13 pid_t pid = fork();
14 if(pid==-1)
15 {
16 perror("fork");
17 exit(1);
18 }
19 else if(pid == 0)
20 {
21 //child
22 close(filedes[0]);
23 int count = 10;
24 while(count--)
25 {
26 char buf[]="hello world";
27 ssize_t _size = write(filedes[1],buf,sizeof(buf));
28 if(_size<0)
29 {
30 perror("write");
31 exit(1);
32 }
33 sleep(1);
34 }
35 }
36 else
37 {
38 //father;
39 close(filedes[1]);
40 char buf[1024];
41 int count = 5;
42 while(1)
43 {
44 memset(buf,‘\0‘,sizeof(buf));
45 ssize_t _size = read(filedes[0],buf,sizeof(buf));
46 if(_size<=0)
47 {
48 perror("read");
49 exit(1);
50 }
51 else
52 {
53 buf[_size]=‘\0‘;
54 printf("%s\n",buf);
55 sleep(1);
56 }
57
58 }
59
60 }
61 return 0;
62 }
client.c
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<sys/types.h>
4 #include<sys/stat.h>
5 #include<fcntl.h>
6 #include<string.h>
7 #include<unistd.h>
8 int main()
9 {
10 umask(0);
11 if(mkfifo("./.tmp",S_IFIFO | 0666)==-1)
12 {
13 perror("mkfifo");
14 exit(1);
15 }
16 int op = open("./.tmp",O_WRONLY);
17 if(op<0)
18 {
19 perror("open");
20 exit(1);
21 }
22 char buf[1024];
23 while(1)
24 {
25 memset(buf,‘\0‘,sizeof(buf));
26 fflush(stdout);
27 printf("please say# ");
28 gets(buf);
29
31 ssize_t _size = write(op,buf,strlen(buf)+1);
32 if(_size<0)
33 {
34 perror("write");
35 exit(1);
36 }
37 }
38 return 0;
39 }
server.c
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<sys/types.h>
4 #include<sys/stat.h>
5 #include<fcntl.h>
6 #include<string.h>
7 int main()
8 {
9 int op = open("./.tmp",O_RDWR);
10 if(op<0)
11 {
12 perror("open");
13 exit(1);
14 }
15 char buf[1024];
16 while(1)
17 {
18 memset(buf,‘\0‘,sizeof(buf));
19 fflush(stdout);
20 ssize_t _size = read(op,buf,sizeof(buf));
21 if(_size<=0)
22 {
23 printf("read end or error\n");
24 break;
25 }
26 else
27 {
28 buf[_size]=‘\0‘;
29 printf("client say# %s\n",buf);
30 }
31 }
32 return 0;
33 }
运行结果:
[fbl@localhost fifo]$ ./client
please say# nihao
please say# hello
please say# abhuuh
[fbl@localhost fifo]$ ./server
client say# nihao
client say# hello
client say# abhuuh
标签:管道
原文地址:http://fengbaoli.blog.51cto.com/10538178/1763076