C++管道,dup2,execlp悬挂
因此,我试图以一种顺序运行3个命令。
ls -l / | grep a | sort -r
但是,我的代码似乎挂起了过程。我知道这通常表明没有手持管道。但是我似乎找不到问题,我已经关闭了我能找到的所有可能的文件描述符。
代码可能具有一些毫无意义的行,例如关闭相同的FD两次,我在尝试修复它时添加了
#include <iostream>
#include<unistd.h>
#include <sys/wait.h>
using namespace std;
#define pipeWrite 1
#define pipeRead 0
// Main Function
int main() {
//Command to run: ls -l / | grep a | sort -r | wc > count.txt
// Total 4 commands to run, so 4 child processes.
pid_t c1,c2,c3,c4;
// File Descriptors
int pipeFD_1[2];
int pipeFD_2[2];
pipe(pipeFD_1);
// To execute ls -l
c1 = fork();
// Child
if (c1 == 0) {
// Closing input end
close(1);
dup(pipeFD_1[1]);
close(pipeFD_1[0]);
close(pipeFD_1[1]);
// Running command
execl("/bin/ls", "ls", "-l", "/",NULL);
}
pipe(pipeFD_2);
c2 = fork();
if (c2 == 0) {
// Closing output end
close(0);
dup(pipeFD_1[0]);
close(1);
dup(pipeFD_2[1]);
close(pipeFD_1[0]);
close(pipeFD_1[1]);
close(pipeFD_2[0]);
close(pipeFD_2[1]);
// Running command
execl("/bin/grep", "grep", "a", NULL);
}
//Closing pipe 1.
close(pipeFD_1[0]);
close(pipeFD_1[1]);
c3 = fork();
if (c3 == 0) {
// Closing out end
close(0);
dup(pipeFD_2[0]);
close(pipeFD_2[0]);
close(pipeFD_2[1]);
// Running command
execl("/bin/sort", "sort", "-r", NULL);
}
// Since exec auto exits the child process. No need to put else.
// Closing output end
waitpid(-1, NULL, 0);
waitpid(-1, NULL, 0);
waitpid(-1, NULL, 0);
exit(0);
}
编辑:通过在第三叉之后添加2行修复的情况下添加了这些fd,请关闭Pipe2(propent)的FDS。
So I am trying to run 3 commands in a sequential way.
ls -l / | grep a | sort -r
However, my code seems to hang the process. I know that this usually indicated an unhandled pipe. but I can't seem to find the problem, I have closed all possible file descriptors that I could find.
Code may have some meaningless lines like closing the same FD twice, which I added while trying to fix it
#include <iostream>
#include<unistd.h>
#include <sys/wait.h>
using namespace std;
#define pipeWrite 1
#define pipeRead 0
// Main Function
int main() {
//Command to run: ls -l / | grep a | sort -r | wc > count.txt
// Total 4 commands to run, so 4 child processes.
pid_t c1,c2,c3,c4;
// File Descriptors
int pipeFD_1[2];
int pipeFD_2[2];
pipe(pipeFD_1);
// To execute ls -l
c1 = fork();
// Child
if (c1 == 0) {
// Closing input end
close(1);
dup(pipeFD_1[1]);
close(pipeFD_1[0]);
close(pipeFD_1[1]);
// Running command
execl("/bin/ls", "ls", "-l", "/",NULL);
}
pipe(pipeFD_2);
c2 = fork();
if (c2 == 0) {
// Closing output end
close(0);
dup(pipeFD_1[0]);
close(1);
dup(pipeFD_2[1]);
close(pipeFD_1[0]);
close(pipeFD_1[1]);
close(pipeFD_2[0]);
close(pipeFD_2[1]);
// Running command
execl("/bin/grep", "grep", "a", NULL);
}
//Closing pipe 1.
close(pipeFD_1[0]);
close(pipeFD_1[1]);
c3 = fork();
if (c3 == 0) {
// Closing out end
close(0);
dup(pipeFD_2[0]);
close(pipeFD_2[0]);
close(pipeFD_2[1]);
// Running command
execl("/bin/sort", "sort", "-r", NULL);
}
// Since exec auto exits the child process. No need to put else.
// Closing output end
waitpid(-1, NULL, 0);
waitpid(-1, NULL, 0);
waitpid(-1, NULL, 0);
exit(0);
}
EDIT: Fixed by adding 2 more lines after the third fork, closing FDs for pipe2 (for parent).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢 barmar 和 ishoney 您的宝贵讨论。将它们发布为帮助其他社区成员的答案。
Thank you Barmar and ItsHoney for your valuable discussions. Posting them as answer to help other community members.