一、pipe管道的特点
1)内核借助环形队列机制,使用内核缓冲区实现
2)管道中的数据只能读取一次,不可反复读
3)半双工通信,数据在管道中,只能单向流动,即一端进行读操作时另一端只能进行写操作,反之,一端进行写操作是另一端只能进行读操作
4)只能用于具有血缘关系的进程,如父子进程、兄弟进程等
二、pipe函数
功能:创建并打开管道
int pipe(int fd[2])
其中fd[0]表示读端,fd[1]表示写端,成功返回0,失败返回-1。
三、管道的读写行为
1.读端
1)若管道有数据,read返回实际读到的字节数;
2)若管道无数据
I. 无写端,read返回0(类似读到文件尾)
II. 有写端,read阻塞等待
2.写端
1)无读端,则异常终止;
2)有读端
I. 管道已满,阻塞等待
II. 管道未满,返回写出字节数
四、父子进程通信实现案例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
void sys_err(const char *str)
{
perror(str);
exit(1);
}
int main(void)
{
int ret;
int fd[2];
pid_t pid;
char *str = "hello pipe\n";
char buf[1024];
ret = pipe(fd);
if(ret == -1) sys_err("pipe error");
pid = fork();
if(pid > 0)//父进程
{
close(fd[0]);//close read
write(fd[1], str, strlen(str));//将str的内容写入管道
sleep(1);
close(fd[1]);
}
else//子进程
{
close(fd[1]);//close write
ret = read(fd[0], buf, sizeof(buf));//从管道中读取并存入buf
write(STDOUT_FILENO, buf, ret);//将读取存入buf的内容写入标准输出
close(fd[0]);
}
return 0;
}
程序执行后会在终端显示如下内容
hello pipe
五、兄弟进程通信实现案例
该案例使用管道实现如下命令:
vim --version | grep python
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/wait.h>
void sys_err(const char *str)
{
perror(str);
exit(1);
}
int main(int argc, char *argv[])
{
int i;
int fd[2];
int ret;
pid_t pid;
ret = pipe(fd);
if(ret == -1)
{
sys_err("pipe error");
}
for(i=0; i<2; i++)
{
pid = fork();
if(pid == -1)
{
sys_err("fork error");
}
if(pid == 0)
{
break;
}
}
if(i == 2)//父
{
//为保证管道中数据的单向流动,需在父进程中将读端和写端都关闭
close(fd[0]);
close(fd[1]);
wait(NULL);
wait(NULL);
}
else if(i == 0)//兄
{
close(fd[1]);//关闭写端
//将标准输入内容重定向至管道读端,wc命令默认从标准输入中读取内容,
//则重定向后wc命令会从管道中读取内容
dup2(fd[0], STDIN_FILENO);
execlp("grep", "grep", "python", NULL);
sys_err("execlp grep error");
}
else if(i == 1)//弟
{
close(fd[0]);//关闭读端
//将标准输出内容重定向至管道写端,则vim --version得到的结果会写入管道
dup2(fd[1], STDOUT_FILENO);
execlp("vim", "vim", "--version", NULL);
sys_err("execlp ls error");
}
return 0;
}
程序执行结果:
+comments +libcall -python +vreplace
+conceal +linebreak +python3 +wildignore
链接方式: gcc -Wl,-E -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lSM -lICE -lXpm -lXt -lX11 -lXdmcp -lSM -lICE -lm -ltinfo -lnsl -lselinux -lacl -lattr -lgpm -ldl -L/usr/lib -llua5.2 -Wl,-E -fstack-protector-strong -L/usr/local/lib -L/usr/lib/x86_64-linux-gnu/perl/5.26/CORE -lperl -ldl -lm -lpthread -lcrypt -L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu -lpython3.6m -lpthread -ldl -lutil -lm -L/usr/lib/x86_64-linux-gnu -ltcl8.6 -ldl -lz -lpthread -lm