进程间通信-管道

1.什么是管道

管道是最初的Unix IPC(进程间通信)形式,局限于没有名字,从而只能由有亲缘关系的进程使用。

2.管道接口描述

#include<unistd.h>
int pipe(int fd[2]);
//返回:若成功则为0,若出错则为-1
该函数返回两个文件描述符:fd[0]和fd[1]。前者打开来读,后者打开来写。

管道的典型用途:为两个不同进程(父进程和子进程)间提供通信手段。


(1)提供一个单向数据流的方式:由一个进程(它将成为父进程)创建一个管道后调用fork派生一个自身的副本,父进程关闭管道的读出端,子进程关闭同一管道的写入端。


(2)提供一个双向数据流的方式:

步骤1:创建管道1(fd1[0]和fd1[1])和管道2(fd2[0]和fd2[1])

步骤2:fork;

步骤3:父进程关闭管道1的读出端(fd1[0])

步骤4:父进程关闭管道2的写入端(fd2[1])

步骤5:子进程关闭管道1的写入端(fd1[1])

步骤6:子进程关闭管道2的读出端(fd2[0])



程序实例:

#include	"unpipc.h"

void	client(int, int), server(int, int);

int
main(int argc, char **argv)
{
	int		pipe1[2], pipe2[2];
	pid_t	childpid;

	Pipe(pipe1);	/* create two pipes */
	Pipe(pipe2);

	if ( (childpid = Fork()) == 0) {		/* child */
		Close(pipe1[1]);
		Close(pipe2[0]);

		server(pipe1[0], pipe2[1]);
		exit(0);
	}
		/* 4parent */
	Close(pipe1[0]);
	Close(pipe2[1]);

	client(pipe2[0], pipe1[1]);

	Waitpid(childpid, NULL, 0);		/* wait for child to terminate */
	exit(0);
}

#include    "unpipc.h"

void
client(int readfd, int writefd)
{
    size_t    len;
    ssize_t    n;
    char    buff[MAXLINE];

        /* 4read pathname */
    Fgets(buff, MAXLINE, stdin);
    len = strlen(buff);        /* fgets() guarantees null byte at end */
    if (buff[len-1] == '\n')
        len--;                /* delete newline from fgets() */

        /* 4write pathname to IPC channel */
    Write(writefd, buff, len);

        /* 4read from IPC, write to standard output */
    while ( (n = Read(readfd, buff, MAXLINE)) > 0)
        Write(STDOUT_FILENO, buff, n);
}

#include    "unpipc.h"

void
server(int readfd, int writefd)
{
    int        fd;
    ssize_t    n;
    char    buff[MAXLINE+1];

        /* 4read pathname from IPC channel */
    if ( (n = Read(readfd, buff, MAXLINE)) == 0)
        err_quit("end-of-file while reading pathname");
    buff[n] = '\0';        /* null terminate pathname */

    if ( (fd = open(buff, O_RDONLY)) < 0) {
            /* 4error: must tell client */
        snprintf(buff + n, sizeof(buff) - n, ": can't open, %s\n",
                 strerror(errno));
        n = strlen(buff);
        Write(writefd, buff, n);

    } else {
            /* 4open succeeded: copy file to IPC channel */
        while ( (n = Read(fd, buff, MAXLINE)) > 0)
            Write(writefd, buff, n);
        Close(fd);
    }
}
 

3.popen和pclose函数

#include<stdio.h>
FILE *popen(const char *command,const char *type);
//返回:若成功则为文件指针,若出错则为NULL
int pclose(FILE *stream);
//返回:若成功则为shell的终止状态,若出错则为-1
标准I/O函数库提供了popen函数,它 创建一个管道启动另外一个进程,该进程要么从该管道读出标准输入,要么往该管道写入标准输出。

pclose函数关闭由popen创建的标准I/O流,等待其中的命令终止,然后返回shell的终止状态


代码实例:

 

#include	"unpipc.h"

int
main(int argc, char **argv)
{
	size_t	n;
	char	buff[MAXLINE], command[MAXLINE];
	FILE	*fp;

		/* 4read pathname */
	Fgets(buff, MAXLINE, stdin);
	n = strlen(buff);		/* fgets() guarantees null byte at end */
	if (buff[n-1] == '\n')
		n--;				/* delete newline from fgets() */

	snprintf(command, sizeof(command), "cat %s", buff);
	fp = Popen(command, "r");

		/* 4copy from pipe to standard output */
	while (Fgets(buff, MAXLINE, fp) != NULL)
		Fputs(buff, stdout);

	Pclose(fp);
	exit(0);
}

popen()函数实际上执行以下操作:

1.使用pipe()系统调用创建一个新管道

2.创建一个新进程,该进程又执行以下操作

       a.如果type是r,就把与管道的写通道相关的文件描述符拷贝到文件描述符1(标准输出);否则,如果type是w,就把与管道的读通道相关的文件描述符拷贝到文件描述符   0(标准输入)

       b.关闭pipe()返回的文件描述符

       c.调用execve()系统调用执行filename所指定的程序

3.如果type是r,就关闭与管道的写通道相关的文件描述符;否则,如果type是w,就关闭与管道的读通道相关的文件描述符

4.返回FILE文件指针所指向的地址,这个指针指向仍然打开的管道所涉及的任一文件描述符。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值