unix popen()的输出不同于执行“sh -c字符串”

8i9zcol2  于 5个月前  发布在  Unix
关注(0)|答案(1)|浏览(53)

说明

我测试了一个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


我的推断有什么问题吗?

o7jaxewo

o7jaxewo1#

那么,popen()的逐步工作原理如下(我们忽略一些无关的检查):
首先:货币进程调用pipe()创建一个管道

int pfd[2];  
pipe(pfd);

字符串
第二步:当前进程调用fork()创建一个子进程。

if (fork() == 0)
    /* child process work */
else if(fork() > 0)
    /* parent process work */


第三:子进程和父进程关闭它们自己的文件eno以创建一个单向管道。
e.d. popen(cmdstring, "r"),父进程关闭其写文件eno,子进程关闭其读文件eno。

/* child process work */
    close(pfd[0]);  // pfd[0] is read port
/* parent process work */
    close(pfd[1]);  // pfd[0] is write port


第四:父进程和子进程重定向自己的文件。
e.d. popen(cmdstring, "r"),父进程将其STDIN_FILENO重定向到读端口,子进程将其STDOUT_FILENO重定向到写端口。

/* child process work */
    dup2(pfd[1], STDOUT_FILENO);  
    close(pfd[1]);  
/* parent process work */
    dup2(pfd[0], STDIN_FILENO);  
    close(pfd[0]);


然后,子进程可以将其输出写入管道,父进程可以通过其stdin从其子进程读取消息。
返回到问题。popen({PAGER:-more}, "w")将货币进程的stdout连接到more进程的stdin。
这样,fgets(line, MAXLINE, fpin)argv[1]读取字符串,并通过管道将字符串传输到moremore将其打印到终端。

cat $argv1 | ${PAGER:-more}

相关问题