文章目录
1、管道介绍
管道的设计目的是实现进程间通信,并且管道是最早的一种进程间通信方式
这种方式实现的进程间通信有一定局限性:
- 管道是以半双工方式工作的
- 参与通信的进程之间应具有父祖关系
两个进程为了实现通信,两个进程应该拥有共享的资源-----这里就是使用pipe函数创建的管道设备
父进程通过fork,子进程会继承父进程的文件描述符(也就意味着子进程和父进程可以读写同一个管道设备)
2、管道接口
管道在内核中的表示是一种管道类型的文件!它像极了普通文件那样,可以读,可以写。唯一区别在于普通文件使用一个fd文件描述符既可以读,又可以写;但是管道需要两个文件描述符,一个用于读,一个用于写
2.1 管道的一般用法(演示原理)
在一个进程中,管道像普通文件那样读写:典型的数据流如下
主进程发一个Hello pipe!给管道设备,然后主进程自己读取
#include <iostream>
#include <cstring>
using namespace std;
extern "C"
{
#include <unistd.h>
/*
POSIX.1-2001, POSIX.1-2008
int pipe(int pipefd[2]);
RETURN VALUE:
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
*/
}
int main(void)
{
int pipes[2];
int res = pipe(pipes);
if(-1 == res)
{
cout << "pipe creation failed" << endl;
return -1;
}
char const* str = "Hello Pipe!";
res = write(pipes[1],str,strlen(str) + 1);
if(-1 == res)
{
cout << "pipe write failed" << endl;
return -1;
}
char read_buff[1024];
res = read(pipes[0],read_buff,1024);
if(-1 == res)
{
cout << "pipe read failed" << endl;
return -1;
}
cout << "Got you! " << read_buff << endl;
return 0;
}
输出:Got you! Hello Pipe!
2.2 管道实现通信(演示原理)
主进程发一个Hello son!给管道设备
子进程读取这个信息
备注:主进程用不到fd[0],子进程用不到fd[1],最好把他们关闭(也可以不这样)
#include <iostream>
#include <cstring>
using namespace std;
extern "C"
{
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
POSIX.1-2001, POSIX.1-2008
int pipe(int pipefd[2]);
RETURN VALUE:
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
*/
}
int main(void)
{
int pipes[2];
int res = pipe(pipes);
if(-1 == res)
{
cout << "pipe creation failed" << endl;
return -1;
}
pid_t pid = fork();
if(pid == -1)
{
cout << "Err: fork failed,exiting!" << endl;
return -1;
}
if(pid == 0)
{
//child
char read_buff[1024];
close(pipes[1]);
res = read(pipes[0],read_buff,1024);
if(-1 == res)
{
cout << "pipe read failed" << endl;
return -1;
}
cout << "Got you! " << read_buff << endl;
close(pipes[0]);
}
else
{
//father
close(pipes[0]);
char const* str = "Hello Son!";
res = write(pipes[1],str,strlen(str) + 1);
if(-1 == res)
{
cout << "pipe write failed" << endl;
return -1;
}
close(pipes[1]);
int status = 0;
/*回收子进程*/
pid = wait(&status);
if(-1 == pid)
{
cout << "wait for child exit failed" << endl;
}
}
return 0;
}
输出:Got you! Hello Son!
2.3 应用设计注意
- 1、上述pipes[0]称为读端,pipes[1]称为写端
当与管道设备关联的所有读端都已经关闭,写端进行写操作会产生SIGPIPE信号给写进程(如果进程忽略或者捕获此信号,write返回-1,errno设置为EPIPE)
----------------------个人觉得设计应当避免出现上述情况---------------------------
当与管道关联的所有写端都已经关闭,读端进行读操作会返回0,即什么也读取不到 - 2、管道设备的内核缓冲是有限制的 PIPE_BUFF用来标识其大小
POSIX规定,对管道的一次写操作的字节个数n<PIPE_BUFF应当是原子的
但是n > PIPE_BUFF,数据可能会其它进程写入的数据交差(这是不可接受的),代码设计应当尽量使用n < PIPE_BUFF
获取最大的缓冲SIZE:我的系统上是4096
#include <iostream>
#include <cstring>
using namespace std;
extern "C"
{
#include <unistd.h>
/*
POSIX.1-2001, POSIX.1-2008.
long fpathconf(int fd, int name);
long pathconf(const char *path, int name);
*/
}
int main()
{
int pipes[2];
int res = pipe(pipes);
if(-1 == res)
{
c*斜体样式*out << "pipe creation failed" << endl;
return -1;
}
cout << fpathconf(pipes[0],_PC_PIPE_BUF) << endl;
cout << fpathconf(pipes[1],_PC_PIPE_BUF) << endl;
return 0;
}
3、管道应用
3.1 SHELL 的管道线
下图是管道进程间通信的一种应用
ls命令读取当前目录的文件列表,并把数据打印到标准输出 STDOUT_FILENO
但是shell非常取巧的把STDOUT_NO的文件描述符指向一个管道设备
并且把more命令的标准输入STDIN_FILENO指向同一个管道设备,最终实现了ls进程的输出递送个more进程!!!
3.2 设计一个管道线支持shell
架构如下:
ls命令标准输出重定向到管道设备,more命令标准输入重定向到管道设备
设计图下:
设计有两个缺陷:
其一、shell进程应当读取用户键入的命令行,然后执行对应命令,这里在code里写死了执行ls和cat的组合
其二、shell进程设置left cmd 进程为一个独立的进程组和前台进程组,left cmd进程应该等待shell 进程完成设置再继续执行,这个例子中简单使用sleep函数同步是不严谨的…
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
extern "C"
{
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
/*
POSIX.1-2001, POSIX.1-2008
int pipe(int pipefd[2]);
RETURN VALUE:
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
*/
}
int main(int argc, char* argv[])
{
pid_t pid = fork();
if(pid == -1)
{
cout << "Err: fork failed,exiting!" << endl;
return -1;
}
if(pid == 0)
{
//left cmd process
sleep(5);
int pipes[2];
int res = pipe(pipes);
if(-1 == res)
{
cout << "pipe creation failed" << endl;
return -1;
}
pid = fork();
if(-1 == pid)
{
cout << "fork failed" << endl;
}
if(pid == 0)
{
// right cmd process as father and front group
dup2(pipes[0],STDIN_FILENO);
close(pipes[0]);
close(pipes[1]);
execlp("cat","cat",NULL);
int status;
wait(&status);
}
else
{ // left cmd process as child
dup2(pipes[1],STDOUT_FILENO);
close(pipes[0]);
close(pipes[1]);
execlp("ls","ls",NULL);
}
}
else
{
/*shell process*/
/*set child as another group*/
setpgid(pid,pid);
/*set child as front gruop*/
if(-1 == tcsetpgrp(STDIN_FILENO,pid))
{
cout << "set child as front failed" << endl;
}
int status = 0;
/*回收子进程*/
pid = wait(&status);
if(-1 == pid)
{
cout << "wait for child exit failed" << endl;
}
}
return 0;
}
3.3 popen pclose函数
来源:CSDN
作者:Johhny Rade
链接:https://blog.csdn.net/uncle103/article/details/103527198