linux匿名管道 :
管道是一个进程连接数据流到另一个进程的通道,它通常是用作把一个进程的输出通过管道连接到另一个进程的输入。
pipe调用:
pipe函数的原型如下:
int pipe(int filedes[2]);
int pipe(int filedes[2]);
调用pipe函数时在内核中开辟一块缓冲区(称为管道)用于通信,它有一个读端一个写端,然后通过filedes参数传出给用户程序两个文件描述符,filedes[0]指向管道的读端,filedes[1]指向管道的写端(很好记,就像0是标准输入1是标准输出一样)。所以管道在用户程序看起来就像一个打开的文件,通过read(filedes[0]);或者write(filedes[1]);向这个文件读写数据其实是在读写内核缓冲区。pipe函数调⽤用成功返回0,调用失败返回-1。
特点:
(1)管道依赖于文件系统,只可以单向传递;
(2)用于血缘关系,常用于父子进程;
(3)面向字节流的一套服务;
(4)通信双方进程退出,管道也就退出;(生命周期:随进程)
(5)保证管道同步机制,保证文件一致性和完整性。
注意:pipe是基于文件描述符工作的,所以在使用pipe后,数据必须要用底层的read和write调用来读取和发送。
数据只能从filedes[0]中读取,数据也只能写入到filedes[1]。
实现两个进程间的通信的步骤:
(1)父进程创建管道;
(2)父进程fork子进程;
(3)父进程关闭fd[0],子进程关闭fd[1];
1. 父进程调用pipe开辟管道,得到两个文件描述符指向管道的两端。
2. 父进程调用fork创建子进程,那么子进程也有两个文件描述符指向同一管道。
3. 父进程关闭管道读端,子进程关闭管道写端。父进程可以往管道里写,子进程可以从管道里读,管道是用环形队列实现的,数据从写端流入从读端流出,这样就实现了进程间通信。
3. 父进程关闭管道读端,子进程关闭管道写端。父进程可以往管道里写,子进程可以从管道里读,管道是用环形队列实现的,数据从写端流入从读端流出,这样就实现了进程间通信。
首先,我们在原先的进程中创建一个管道,然后再调用fork创建一个新的进程,最后通过管道在两个进程之间传递数据。源文件名为pipe.c,代码如下:
运行结果:
使用管道需要注意以下4种特殊情况(假设都是阻塞I/O操作,没有设置O_NONBLOCK标志):
1.如果所有指向管道写端的文件描述符都关闭了(管道写端的引用计数等于0),而仍然有进程从管道的读端读数据,那么管道中剩余的数据都被读取后,再次read会返回0,就像 读到文件末尾一样。
测试代码:
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
int main() {
int _pipe[2];
int ret = pipe(_pipe);
if(ret == -1){
printf("create pipe error! errno code is : %d\n",errno);
}
pid_t id = fork();
if( id < 0 ){
printf("fork error!");
return 2;
}else if( id == 0 )
{ //child
close(_pipe[0]);
int i =0;
char *_mesg_c=NULL;
while(i<10)
{
_mesg_c="i am child!";
write(_pipe[1], _mesg_c, strlen(_mesg_c)+1);
sleep(1);
i++;
}
close(_pipe[1]);
}
else
{ //father
close(_pipe[1]);
char _mesg[100];
int j = 0;
while(j<100)
{
memset(_mesg, '\0', sizeof(_mesg));
int ret = read(_pipe[0], _mesg, sizeof(_mesg));
printf("%s : code is : %d\n",_mesg, ret);
j++;
}
if ( waitpid(id, NULL, 0)< 0)
{
return 3;
}
}
return 0;
}
2. 如果有指向管道写端的文件描述符没关闭(管道写端的引用计数大于0),而持有管道写端的进程也没有向管道中写数据,这时有进程从管道读端读数据,那么管道中剩余的数据都被读取后,再次read会阻塞,直到管道中有数据可读了才读取数据并返回。
测试代码:
#include <stdio.h>
#include <unistd.h>
##include <errno.h>
##include <string.h>
##include <sys/wait.h>
#int main()
{
int _pipe[2];
int ret = pipe(_pipe);
if(ret == -1)
{
printf("create pipe error! errno code is : %d\n",errno);
return 1;
}
pid_t id = fork();
if( id < 0 )
{
printf("fork error!");
return 2;
}
else if( id == 0 )
{ //child
close(_pipe[0]);
int i =0;
char *_mesg_c=NULL;
while(i<20)
{
if( i < 10 )
{
_mesg_c="i am child!";
write(_pipe[1], _mesg_c, strlen(_mesg_c)+1);
sleep(1);
}
close(_pipe[1]);
}
else
{
char _mesg[100];
int j = 0;
while(j<20)
{
memset(_mesg, '\0', sizeof(_mesg));
int ret = read(_pipe[0], _mesg, sizeof(_mesg));
printf("%s : code is : %d\n",_mesg, ret);
j++;
}
if ( waitpid(id, NULL, 0)< 0)
{
return 3;
}
}
}
return 0;
}
3.如果所有指向管道读端的文件描述符都关闭了(管道读端的引用计数等于0),这时有进程向管道的写端write,那么该进程会收到信号SIGPIPE,通常会导致进程异常终止。
测试代码:
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
int main()
{
int _pipe[2];
int ret = pipe(_pipe);
if(ret == -1)
{
printf("create pipe error! errno code is : %d\n",errno);
return 1;
}
pid_t id = fork();
if( id < 0 )
{
printf("fork error!");
return 2;
}
else if( id == 0 )
{ //child
close(_pipe[0]);
int i =0;
char *_mesg_c=NULL;
while(i<20)
{
if( i < 10 )
{
_mesg_c="i am child!";
write(_pipe[1], _mesg_c, strlen(_mesg_c)+1);
}
sleep(1);
i++;
}
}
else
{ //father
close(_pipe[1]);
char _mesg[100];
int j = 0;
while(j<3)
{
memset(_mesg, '\0', sizeof(_mesg));
int ret = read(_pipe[0], _mesg, sizeof(_mesg));
printf("%s : code is : %d\n",_mesg, ret);
j++;
}
close(_pipe[0]);
sleep(10);
if ( waitpid(id, NULL, 0)< 0)
{
return 3;
}
}
return 0;
}
4.如果有指向管道读端的文件描述符没关闭(管道读端的引用计数大于0),而持有管道读端的进程也没有从管道中读数据,这时有进程向管道写端写数据,那么在管道被写满时再次write会阻塞,直到管道中有空位置了才写入数据并返回。
最后,再补充一点小知识(管道容量的查询):
通过代码查询:
结果:
这样你就知道你的管道容量了。