linux管道一

25 篇文章 0 订阅

管道分为匿名管道和有名管道,平时我们说的管道通常指匿名管道,有名管道又叫FIFO,我们先介绍匿名管道,然后介绍有名管道,下文中的管道没特别说明都指的是匿名管道。

管道有已下两种局限性:
        1.管道是半双工的,只支持数据的单向流动,即管道的一端只能支持读(fd[0]),另一端支持写(fd[1])。
        2.管道只能在具体公共祖先之间的进程间使用,通常用户父子进程。通常父进程创建管道,接着父进程fork子进程,然后在父子进程间共享管道。

1.创建管道:
#include<unistd.h>
int pipe(fd[2]); //创建管道成功,函数返回0,否则返回-1。注意fd[0]只能用来读,fd[1]只能用来写

fd为值结果参数,pipe会返回数组fd的值,然后我们用fd[0]来读管道,用fd[1]来写管道。我们先在父进程调用pipe创建管道,创建后父进程中管道结构如下图中左边的样子,然后我们fork子进程,子进程会复制父进程中的所有打开文件描述符,然后我们会得到如下图右的结构。

fork后做什么,取决于我们想要的数据流动方向,如果我们想把数据从父进程传递给子进程,那么我们可以在父进程中关闭读端(fd[0]),在子进程中关闭写端(fd[1]),然后我们得到下图中的结构,进而实现消息的单向传递,反之如果想把数据从子进程传给父进程,则在子进程中关闭读fd[0],在父进程关闭写fd[1].

#include	"ourhdr.h"

int main(void)
{
	int		n, fd[2];
	pid_t	pid;
	char	line[MAXLINE];

	if (pipe(fd) < 0)
		err_sys("pipe error");

	if ( (pid = fork()) < 0)
		err_sys("fork error");

	else if (pid > 0) {		/* parent */
		close(fd[0]);
		write(fd[1], "hello world\n", 12);

	} else {				/* child */
		close(fd[1]);
		n = read(fd[0], line, MAXLINE);
		write(STDOUT_FILENO, line, n);
	}

	exit(0);
}

上面代码是一个简单的例子,如上所说父进程先创建管道,然后fork子进程,然后在父进程中关闭读,子进程关闭写,并把“hello,world”从父进程传递给子进程。

 

当管道的一端被关闭后,下面的两条规则起作用:
        1.当读一个写端已经关闭的管道时,在所有数据读取完后,read返回0.
        2.当写一个读端已经关闭的管道时,产生SIGPIPE信号。根据对该信号的处理,我们分为如下情况:
              如果我们有注册该信号的处理函数,则先执行信号处理函数,然后write直接返回-1,errno=SIGPIPE。
              如果信号处理函数=SIG_IGN,系统忽略该信号,write直接返回-1,errno=SIGPIPE
              如果我们没有对该信号的处理函数做任何处理,或信号处理函数=SIG_DFL,则执行系统默认动作,程序直接终止

下面我们再给一个linux使用管道的常见例子,程序执行时,输入一个文件名,父进程按行读取该文件,并将该文件写入管道,传给子进程,子进程通过exec调用more程序,more程序的作用是从标准输入读取文件,对文件内容做分页处理后,写到标准输出,该程序做了一个巧妙处理:在子进程中将标准输入复制到管道的读端:dup2(fd[0],STDIN_FILENO),这样子进程的标准输入实际上就是管道的读端,从标准输入读就是从管道读:
 

#include	<sys/wait.h>
#include	"ourhdr.h"

#define	DEF_PAGER	"/usr/bin/more"		/* default pager program */

int
main(int argc, char *argv[])
{
	int		n, fd[2];
	pid_t	pid;
	char	line[MAXLINE], *pager, *argv0;
	FILE	*fp;

	if (argc != 2)
		err_quit("usage: a.out <pathname>");
	if ( (fp = fopen(argv[1], "r")) == NULL)
		err_sys("can't open %s", argv[1]);

	if (pipe(fd) < 0)
		err_sys("pipe error");

	if ( (pid = fork()) < 0)
		err_sys("fork error");
	else if (pid > 0) {								/* parent */
		close(fd[0]);		/* close read end */
			/* parent copies argv[1] to pipe */
		while (fgets(line, MAXLINE, fp) != NULL) {
			n = strlen(line);
			if (write(fd[1], line, n) != n)
				err_sys("write error to pipe");
		}
		if (ferror(fp))
			err_sys("fgets error");

		close(fd[1]);	/* close write end of pipe for reader */
		if (waitpid(pid, NULL, 0) < 0)
			err_sys("waitpid error");
		exit(0);

	} else {										/* child */
		close(fd[1]);	/* close write end */
		if (fd[0] != STDIN_FILENO) {
			if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO)
				err_sys("dup2 error to stdin");
			close(fd[0]);	/* don't need this after dup2 */
		}

			/* get arguments for execl() */
		if ( (pager = getenv("PAGER")) == NULL)
			pager = DEF_PAGER;
		if ( (argv0 = strrchr(pager, '/')) != NULL)
			argv0++;		/* step past rightmost slash */
		else
			argv0 = pager;	/* no slash in pager */

		if (execl(pager, argv0, (char *) 0) < 0)
			err_sys("execl error for %s", pager);
	}
}

       #include <stdio.h>
      FILE *popen(const char *command, const char *type);
      int pclose(FILE *stream); 

 上面的程序也可以用popen函数实现,popen先执行fork创建子进程,在子进程中调用exec,exec执行的命令通过参数command指示,popen第二个参数为"r"时,表示父进程可以从popen函数返回的文件指针中读,第二个参数为"w"时,表示父进程可以把数据写到popen函数返回的文件指针中,exec执行的命令把它作为标准输入,如下图:

#include	<sys/wait.h>
#include	"ourhdr.h"

#define	PAGER	"${PAGER:-more}" /* environment variable, or default */

int
main(int argc, char *argv[])
{
	char	line[MAXLINE];
	FILE	*fpin, *fpout;

	if (argc != 2)
		err_quit("usage: a.out <pathname>");
	if ( (fpin = fopen(argv[1], "r")) == NULL)
		err_sys("can't open %s", argv[1]);

	if ( (fpout = popen(PAGER, "w")) == NULL)
		err_sys("popen error");

		/* copy argv[1] to pager */
	while (fgets(line, MAXLINE, fpin) != NULL) {
		if (fputs(line, fpout) == EOF)
			err_sys("fputs error to pipe");
	}
	if (ferror(fpin))
		err_sys("fgets error");
	if (pclose(fpout) == -1)
		err_sys("pclose error");
	exit(0);
}

 

 



 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值