说明
我测试了一个APUE(UNIX环境中的高级编程,第3版),15.3 popen和plcose函数的例子。
示例代码如下
#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);
}
字符串
我的输入文件是“temp.in“,
$ cat temp.in
test sentence
型
示例的输出如下
$ ./popen2 temp.in
test sentence
型
问题
如APUE中所示,执行fpout = popen(cmdstring, "w")
类似于其子进程执行sh -c cmdstring
。因此,上一节中显示的popen
代码应执行
sh -c "${PAGER:-more} test sentence"
型
我的操作系统(Ubuntu 22.04.3 LTS)没有名为PAGER
的环境值,因此它应该执行
sh -c "more test sentence"
型
然而,据我所知,order more
的选项应该是filename。在我的系统中测试下面的order并获得输出,这与我上面推断的不同:
$ ${PAGER:-more} temp.in
test sentence
$ ${PAGER:-more} test sentence
more: can't open test: no such file or directory
more: can't open sentence: no such file or directory
型
我的推断有什么问题吗?
1条答案
按热度按时间o7jaxewo1#
那么,
popen()
的逐步工作原理如下(我们忽略一些无关的检查):首先:货币进程调用
pipe()
创建一个管道字符串
第二步:当前进程调用
fork()
创建一个子进程。型
第三:子进程和父进程关闭它们自己的文件eno以创建一个单向管道。
e.d.
popen(cmdstring, "r")
,父进程关闭其写文件eno,子进程关闭其读文件eno。型
第四:父进程和子进程重定向自己的文件。
e.d.
popen(cmdstring, "r")
,父进程将其STDIN_FILENO
重定向到读端口,子进程将其STDOUT_FILENO
重定向到写端口。型
然后,子进程可以将其输出写入管道,父进程可以通过其stdin从其子进程读取消息。
返回到问题。
popen({PAGER:-more}, "w")
将货币进程的stdout连接到more
进程的stdin。这样,
fgets(line, MAXLINE, fpin)
从argv[1]
读取字符串,并通过管道将字符串传输到more
,more
将其打印到终端。型