首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用dup2创建管道

使用dup2创建管道
EN

Stack Overflow用户
提问于 2017-05-14 17:36:23
回答 1查看 317关注 0票数 0
代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(void) {
    int pfd1[2];
    int pfd2[2];
    pid_t pid1, pid2, pid3;
    if(pipe(pfd1)==-1) {
        perror("Creazione pipe");
        exit(EXIT_FAILURE);
        }
    if(pipe(pfd2)==-1) {
        perror("Creazione pipe");
        exit(EXIT_FAILURE);
        }
    printf("Sono il padre\n");
    switch(pid1=fork()) {
        printf("%d\n", pid1);
        case -1: {
            perror("Creazione figlio 1");
            exit(EXIT_FAILURE);
            }
        case 0: { //figlio 1
            printf("Sono il figlio 1\n");
            if(dup2(pfd1[1],1)==-1) { //redirige lo stdout sul descrittore scrittura
                perror("Prima redirezione");
                exit(EXIT_FAILURE);
                }
            close(pfd1[0]);
            close(pfd1[1]); //lo chiudo perchè sto redirigendo lo stdout
            close(pfd2[0]);
            close(pfd2[1]);
            execlp("ps", "ps", "-A", "-ostat,pid", (char*) NULL);
            }
        }
    waitpid(pid1,NULL,0);
    switch(pid2=fork()) {
        case -1: {
            perror("Creazione figlio 2");
            exit(EXIT_FAILURE);
            }
        case 0: { //figlio 2
            printf("Sono il figlio 2\n");
            if(dup2(pfd1[0],0)==-1) { //redirige lo stdin sul descrittore lettura
                perror("Seconda redirezione");
                exit(EXIT_FAILURE);
                }
            printf("Prima redirezione figlio 2\n");
            if(dup2(pfd2[1],1)==-1) {
                perror("Terza redirezione");
                exit(EXIT_FAILURE);
                }
            close(pfd1[1]);
            close(pfd1[0]);
            close(pfd2[0]);
            close(pfd2[1]);
            execlp("grep", "grep", "-e", "[zZ]", (char*) NULL);
            }
    waitpid(pid2, NULL,0);
    switch(pid3=fork()) {
        case -1: {
            perror("Creazione terzo figlio");
            exit(EXIT_FAILURE);
            }
        case 0: { //figlio 3
            printf("Sono il figlio 3\n");
            if(dup2(pfd2[0],0)==-1) {
                perror("Quarta redirezione");
                exit(EXIT_FAILURE);
                }
            close(pfd1[0]);
            close(pfd1[1]);
            close(pfd2[0]);
            close(pfd2[1]);
            execlp("awk", "awk", "'{print $2}'", (char*) NULL);
            }
        }
    /*padre*/
    //waitpid(pid1, NULL, 0);
    //waitpid(pid2, NULL, 0);
    waitpid(pid3, NULL, 0);
    close(pfd2[0]);
    close(pfd2[1]);
    close(pfd1[0]);
    close(pfd1[1]);
    return 0;
    }

}

您好,我正在尝试使用系统调用dup2创建一个shell bash命令管道。我期望的输出应该与

代码语言:javascript
复制
bash $> ps -A -ostat,pid | grep -e [zZ] | awk '{print $2}'

我所做的是派生3个孩子,并让他们通过两个管道进行通信。每个子进程都执行命令的一部分。问题是我的程序在第二个孩子身上卡住了,显然这个孩子甚至不能执行exec。我确信我的代码有一些问题,但由于这是我第一次尝试使用dup2,所以我有点困惑。另外,不要介意printfs,它们只是用于调试。非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2017-05-14 19:35:00

基本上,您必须在正确的位置关闭管道,并且在switch语句中关闭括号失败。代码如下:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h> /*lib for waitpid()*/

int main(void) {
    int pfd1[2];
    int pfd2[2];
    pid_t pid1, pid2, pid3;
    if(pipe(pfd1)==-1) {
        perror("Creazione pipe");
        exit(EXIT_FAILURE);
        }
    if(pipe(pfd2)==-1) {
        perror("Creazione pipe");
        exit(EXIT_FAILURE);
        }
    printf("Sono il padre\n");
    switch(pid1=fork()) {
        printf("%d\n", pid1);
        case -1: {
            perror("Creazione figlio 1");
            exit(EXIT_FAILURE);
            }
        case 0: { //figlio 1
            printf("Sono il figlio 1\n");
            if(dup2(pfd1[1],1)==-1) { //redirige lo stdout sul descrittore scrittura
                perror("Prima redirezione");
                exit(EXIT_FAILURE);
                }
            close(pfd1[0]);
            close(pfd1[1]); //lo chiudo perchè sto redirigendo lo stdout
            close(pfd2[0]);
            close(pfd2[1]);
            execlp("ps", "ps", "-A", "-ostat,pid", (char*) NULL);
            }
        }
    waitpid(pid1,NULL,0);
    switch(pid2=fork()) {
        case -1: {
            perror("Creazione figlio 2");
            exit(EXIT_FAILURE);
            }
        case 0: { //figlio 2
            printf("Sono il figlio 2\n");
            if(dup2(pfd1[0],0)==-1) { //redirige lo stdin sul descrittore lettura
                perror("Seconda redirezione");
                exit(EXIT_FAILURE);
                }
            printf("Prima redirezione figlio 2\n");
            if(dup2(pfd2[1],1)==-1) {
                perror("Terza redirezione");
                exit(EXIT_FAILURE);
                }
            close(pfd1[0]);
            close(pfd1[1]);
            close(pfd2[0]);
            close(pfd2[1]);
            execlp("grep", "grep", "-e", "[Ss]", (char*) NULL);
            }

    } /*You forgot that bracket*/
    /*
     * Close the pipe before waitpid because 2rd child
     * will wait until you close it. Check pipe theory
     */
    close(pfd1[1]);
    close(pfd1[0]);
    waitpid(pid2, NULL,0);
    switch(pid3=fork()) {
        case -1: {
            perror("Creazione terzo figlio");
            exit(EXIT_FAILURE);
            }
        case 0: { //figlio 3
            printf("Sono il figlio 3\n");
            if(dup2(pfd2[0],0)==-1) {
                perror("Quarta redirezione");
                exit(EXIT_FAILURE);
                }
            close(pfd2[0]);
            close(pfd2[1]);
            /*' ' removed from the "{print $2}"*/
            execlp("awk", "awk", "{print $2}", (char*) NULL);
            }
    }
    /*
     * Close the pipe before waitpid because 3rd child
     * will wait until you close it. Check pipe theory
     */
    close(pfd2[0]);
    close(pfd2[1]);
    waitpid(pid3, NULL, 0);


    return 0;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43962433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档