为什么我不能通过管道传输两个 execl 调用的输出?
why can't I pipe output from both execl calls?
可能重复:
- 如何调用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
一旦成功调用execl
或exec
家族中的任何其他函数,原始进程将被新进程完全覆盖。这意味着新进程永远不会"返回"到旧进程。如果您在一行中有两个execl
调用,则执行第二个调用的唯一方法是如果第一个调用失败。
为了在一行中运行两个不同的命令,您必须fork
一个子子来运行第一个命令,wait
, fork
第二个子子来运行第二个命令,然后(可选)wait
为第二个子。
On read
read
系统调用不追加一个终止null,所以通常您需要查看返回值,它告诉您实际读取的字节数。然后将以下字符设置为null以获得C字符串,或使用std::string
的范围构造函数。
现在您正在使用waitpid
等待,直到子进程已经完成,然后从管道读取。这样做的问题是,如果子进程产生大量输出,那么它将阻塞,因为管道已满,父进程没有从中读取。结果将导致死锁,因为子进程等待父进程读取,父进程等待子进程终止。
你应该做的是使用select
等待输入到达孩子的stdout
或孩子的stderr
。当输入到达时,读取它;这将允许孩子继续。当子进程死亡时,您将知道,因为您将获得两个进程的文件结束。然后可以安全地调用wait
或waitpid
。
exec
函数族用新的进程映像替换当前进程映像。执行时,
execl("/bin/bash", "/bin/bash", "foo", NULL);
当前进程的代码不再执行。这就是为什么你永远看不到执行
的结果execl("/bin/ls", "ls", "-1", (char *)0);
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 两个文件使用彼此的功能-如何解决
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 将fold表达式与std::一起用于两个元组
- 如何在C++中比较两个char数组
- 给定两个偶数,求出它们之间所有偶数的平方和
- 比较两个大小不等的映射c++
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 如何在for循环中包含两个索引值的测试条件
- 在声明中合并两个常量"std::set"(不是在运行时)
- 为什么我不能通过管道传输两个 execl 调用的输出?