为什么我不能通过管道传输两个 execl 调用的输出?

why can't I pipe output from both execl calls?

本文关键字:execl 两个 调用 输出 不能 管道 传输 为什么      更新时间:2023-10-16

可能重复:

  • 如何调用execl()在C与适当的参数?
  • 从exec获取输出
  • Linux管道作为输入和输出
  • 使用dup2管道
  • 输入/输出管道

在过去的3天里,我一直在尝试使用dup/dup2和fork学习Linux中的管道。我想我掌握了窍门,但是当我从子进程调用两个不同的程序时,似乎我只捕获了第一个调用的输出。我不明白这是为什么和/或我做错了什么。这是我的首要问题。

编辑:我认为一个可能的解决方案是分叉另一个孩子,并设置管道与dup2,但我更想知道为什么下面的代码不工作。我的意思是,我希望从第一次execl调用捕获标准错误,从第二次execl调用捕获标准输出。这似乎不会发生。

我的第二个问题是我是否正确打开和关闭管道。如果没有,我想知道我需要添加/删除/更改什么。

下面是我的代码:
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <sys/wait.h>
#define READ_END 0
#define WRITE_END 1
void parentProc(int* stdoutpipe, int* stderrpipe);
void childProc(int* stdoutpipe, int* stderrpipe);
int main(){
    pid_t pid;
    int status;
    int stdoutpipe[2]; // pipe
    int stderrpipe[2]; // pipe
    // create a pipe
    if (pipe(stdoutpipe) || pipe(stderrpipe)){
        std::cerr << "Pipe failed." << std::endl;
        return EXIT_FAILURE;
    }
    // fork a child
    pid = fork();
    if (pid < 0) {
        std::cerr << "Fork failed." << std::endl;
        return EXIT_FAILURE;
    } 
    // child process
    else if (pid == 0){
        childProc(stdoutpipe, stderrpipe);  
    }
    // parent process
    else {
        std::cout<< "waitpid: " << waitpid(pid, &status, 0)
             <<'n'<<std::endl;
        parentProc(stdoutpipe, stderrpipe);
    }
    return 0;
}

void childProc(int* stdoutpipe, int* stderrpipe){
    dup2(stdoutpipe[WRITE_END], STDOUT_FILENO);
    close(stdoutpipe[READ_END]);
    dup2(stderrpipe[WRITE_END], STDERR_FILENO);
    close(stderrpipe[READ_END]);
    execl("/bin/bash", "/bin/bash", "foo", NULL);
    execl("/bin/ls", "ls", "-1", (char *)0);
    //  execl("/home/me/outerr", "outerr", "-1", (char *)0);
    //char * msg = "Hello from stdout";
    //std::cout << msg;
    //msg = "Hello from stderr!";
    //std::cerr << msg << std::endl;
    // close write end now?
}
void parentProc(int* stdoutpipe, int* stderrpipe){
    close(stdoutpipe[WRITE_END]);
    close(stderrpipe[WRITE_END]);
    char buffer[256];
    char buffer2[256];
    read(stdoutpipe[READ_END], buffer, sizeof(buffer));
    std::cout << "stdout: " << buffer << std::endl;
    read(stderrpipe[READ_END], buffer2, sizeof(buffer));
    std::cout << "stderr: " << buffer2 << std::endl;
    // close read end now?
}

当我运行它时,我得到以下输出:

yfp> g++ selectTest3.cpp; ./a.out 
waitpid: 21423
stdout: hB�(6
stderr: foo: line 1: -bash:: command not found

"outerr"二进制文件(上面注释掉了)的源代码很简单:

#include <iostream>
int main(){
    std::cout << "Hello from stdout" << std::endl;
    std::cerr << "Hello from stderr!" << std::endl;
    return 0;
}

当我调用"outerr "而不是" ls "或"foo"时,我得到以下输出,这是我所期望的:

yfp> g++ selectTest3.cpp; ./a.out 
waitpid: 21439
stdout: Hello from stdout
stderr: Hello from stderr!

On execl

一旦成功调用execlexec家族中的任何其他函数,原始进程将被新进程完全覆盖。这意味着新进程永远不会"返回"到旧进程。如果您在一行中有两个execl调用,则执行第二个调用的唯一方法是如果第一个调用失败。

为了在一行中运行两个不同的命令,您必须fork一个子子来运行第一个命令,wait, fork第二个子子来运行第二个命令,然后(可选)wait为第二个子。

On read

read系统调用不追加一个终止null,所以通常您需要查看返回值,它告诉您实际读取的字节数。然后将以下字符设置为null以获得C字符串,或使用std::string的范围构造函数。

<<p> 在管道/strong>

现在您正在使用waitpid等待,直到子进程已经完成,然后从管道读取。这样做的问题是,如果子进程产生大量输出,那么它将阻塞,因为管道已满,父进程没有从中读取。结果将导致死锁,因为子进程等待父进程读取,父进程等待子进程终止。

你应该做的是使用select等待输入到达孩子的stdout或孩子的stderr。当输入到达时,读取它;这将允许孩子继续。当子进程死亡时,您将知道,因为您将获得两个进程的文件结束。然后可以安全地调用waitwaitpid

exec函数族用新的进程映像替换当前进程映像。执行时,

execl("/bin/bash", "/bin/bash", "foo", NULL);

当前进程的代码不再执行。这就是为什么你永远看不到执行

的结果
execl("/bin/ls", "ls", "-1", (char *)0);