第15章 进程间通行 15.3函数popen和pclose

<436>

(1)

FILE *popen(const char *command, const char *type);

popen函数创建一个连接到另一个进程的管道。

type是“r”,则文件指针连接到command的标准输出,可通过文件指针读取command的输出。

type是“w”,则文件指针连接到command的标准输入,可通过文件指针向command发送数据。

int pclose(FILE *stream);

//此例子为用popen向分页程序传送文件。popen创建了连接到分页程序的管道。
#include "apue.h"
#include <sys/wait.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);
}

(2)

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

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

如果shell变量PAGER已经定义,且其值非空,则使用其值,否则使用字符串more。


<437>popen函数和pclose函数的实现

#include "apue.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>

/*
 * Pointer to array allocated at run-time.
 */
static pid_t	*childpid = NULL;

/*
 * From our open_max(), {Prog openmax}.
 */
static int		maxfd;

FILE *
popen(const char *cmdstring, const char *type)
{
	int		i;
	int		pfd[2];
	pid_t	pid;
	FILE	*fp;

	/* only allow "r" or "w" */
	if ((type[0] != 'r' && type[0] != 'w') || type[1] != 0) {
		errno = EINVAL;
		return(NULL);
	}

	if (childpid == NULL) {		/* first time through */
		/* allocate zeroed out array for child pids */
		maxfd = open_max();
		if ((childpid = calloc(maxfd, sizeof(pid_t))) == NULL)
			return(NULL);
	}

	if (pipe(pfd) < 0)
		return(NULL);	/* errno set by pipe() */
	if (pfd[0] >= maxfd || pfd[1] >= maxfd) {
		close(pfd[0]);
		close(pfd[1]);
		errno = EMFILE;
		return(NULL);
	}

	if ((pid = fork()) < 0) {
		return(NULL);	/* errno set by fork() */
	} else if (pid == 0) {							/* child */
		if (*type == 'r') {
			close(pfd[0]);
			if (pfd[1] != STDOUT_FILENO) {
				dup2(pfd[1], STDOUT_FILENO);
				close(pfd[1]);
			}
		} else {
			close(pfd[1]);
			if (pfd[0] != STDIN_FILENO) {
				dup2(pfd[0], STDIN_FILENO);
				close(pfd[0]);
			}
		}

		/* close all descriptors in childpid[] */
		for (i = 0; i < maxfd; i++)
			if (childpid[i] > 0)
				close(i);

		execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
		_exit(127);
	}

	/* parent continues... */
	if (*type == 'r') {
		close(pfd[1]);
		if ((fp = fdopen(pfd[0], type)) == NULL)
			return(NULL);
	} else {
		close(pfd[0]);
		if ((fp = fdopen(pfd[1], type)) == NULL)
			return(NULL);
	}

	childpid[fileno(fp)] = pid;	/* remember child pid for this fd */
	return(fp);
}

popen函数步骤。

(1)判断行参type是否正确。

(2)若第一此调用popen函数则创建数组childpid。文件描述符做数组下标,数组元素保存子进程ID,数组大小为最大打开文件数。

void *calloc(size_t nmemb, size_t size);

The  calloc()  function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory.   The memory  is  set  to zero.  If nmemb or size is 0, then calloc() returns either NULL, or a unique pointer value that can later  be  successfully passed to free().

open_max()函数确定文件描述符个数,即最大可打开文件数。

(3)创建管道,不使用值大于(或等于)open_max函数返回值的管道文件描述符。


(4)子进程中调用dup2,使其标准输入或输出成为管道的读或写端。

(5)一个进程可能调用popen多次,所以关闭那些以前调用popen打开的/现在仍然在子进程中打开着的I/O流。

(6)子进程中执行命令字符串。


(4)父进程调用fdopen函数产生popen函数的返回值。

FILE *fdopen(int fd, const char *mode);

The  fdopen()  function  associates  a  stream  with  the existing file descriptor, fd. 

(5)父进程保存子进程ID。

int fileno(FILE *stream);

The function fileno() examines the  argument  stream  and  returns  its integer descriptor.

int
pclose(FILE *fp)
{
	int		fd, stat;
	pid_t	pid;

	if (childpid == NULL) {
		errno = EINVAL;
		return(-1);		/* popen() has never been called */
	}

	fd = fileno(fp);
	if (fd >= maxfd) {
		errno = EINVAL;
		return(-1);		/* invalid file descriptor */
	}
	if ((pid = childpid[fd]) == 0) {
		errno = EINVAL;
		return(-1);		/* fp wasn't opened by popen() */
	}

	childpid[fd] = 0;
	if (fclose(fp) == EOF)
		return(-1);

	while (waitpid(pid, &stat, 0) < 0)
		if (errno != EINTR)
			return(-1);	/* error other than EINTR from waitpid() */

	return(stat);	/* return child's termination status */
}


<441>

(1)此程序演示在,应用程序和输入之间插入一个程序以便对输入进行变换处理。

//调用大小写过滤程序读取命令。
#include "apue.h"
#include <sys/wait.h>

int
main(void)
{
	char	line[MAXLINE];
	FILE	*fpin;

	if ((fpin = popen("./myuclc", "r")) == NULL)
		err_sys("popen error");
	for ( ; ; ) {
		fputs("prompt> ", stdout);
		fflush(stdout);//标准输出通常是行缓冲,而提示符不包含换行符,所以在写了提示之后,调用fflush
		if (fgets(line, MAXLINE, fpin) == NULL)	/* read from pipe */
			break;
		if (fputs(line, stdout) == EOF)
			err_sys("fputs error to pipe");
	}
	if (pclose(fpin) == -1)
		err_sys("pclose error");
	putchar('\n');
	exit(0);
}

(2)过滤程序,将大写字符变换为小写字符。

#include <ctype.h>

int
main(void)
{
	int		c;

	while ((c = getchar()) != EOF) {
		if (isupper(c))
			c = tolower(c);
		if (putchar(c) == EOF)
			err_sys("output error");
		if (c == '\n')
			fflush(stdout);
	}
	exit(0);
}



转载于:https://my.oschina.net/u/2463708/blog/516591

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值