C++管道,dup2,execlp悬挂

发布于 2025-01-21 13:46:04 字数 1969 浏览 2 评论 0原文

因此,我试图以一种顺序运行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫竹語嫣☆ 2025-01-28 13:46:04

谢谢 barmar ishoney 您的宝贵讨论。将它们发布为帮助其他社区成员的答案。

通过在第三叉之后添加2条线修复,关闭pipe2的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);
}

Thank you Barmar and ItsHoney for your valuable discussions. Posting them as answer to help other community members.

Fixed by adding 2 more lines after the third fork, closing FDs for pipe2 (for parent).

#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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文