Ç管,叉,DUP,和exec() [英] C pipe, fork, dup, and exec()

查看:157
本文介绍了Ç管,叉,DUP,和exec()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过管道传递的字符串列表到子进程,它应该 /斌/猫使用 EXECL()显示通过。我是有工作的前面除了管道没有关闭使程序一直在等待。不知道我做了什么,现在它不工作。有人可以看到我的code,并告诉我,我做错了 STR 数据没有在子过程中显示由猫?

  INT主(INT ARGC,字符** argv的){    字符*海峡[] = {的,快速,棕色,狐狸精,跳楼,过,了,懒惰,狗};
    INT FDS [TOTAL_CHILDREN]
    INT writeFds;
    INT catPID;
    INT状态;    FILE * write_to_child;    //创建管
    如果(管(FDS)== -1){
        PERROR(创建管道:失败);
        出口(EXIT_FAILURE);
    }
    管(FDS);
    //创建猫子子    开关(catPID){
        案件0://创建成功的孩子
            关闭(FDS [1]); //家长密切写入端
            关闭(0); //关闭标准输入
            DUP(FDS [0]); //从EXECL猫连接管标准输入            EXECL(/斌/猫,猫,(字符*)0);
            PERROR(EXEC失败!);
            出口(20);
            打破;        案例-1://失败
            PERROR(叉失败:cat进程);
            出口(EXIT_FAILURE);        默认://父进程
            关闭(FDS [0]);            writeFds = FDS [1];
            write_to_child = fdopen(FDS [1],W);            如果(write_to_child == NULL){
                PERROR(写管道失败);
                出口(EXIT_FAILURE);
            }
            打破;
    }    INT I;
    对于(i = 0; I< 9;我++){
        fprintf中(write_to_child%s \\ n,海峡[I]);
    }    FCLOSE(write_to_child);
    关闭(writeFds);    等待(安培;状态);    回报(EXIT_SUCCESS);
}


解决方案

您可能要添加的行

  catPID = fork()的;

和我不知道为什么你有管(FDS)两次

I'm trying to pass list of strings through pipe to child process where it should display through /bin/cat using execl(). I had it working earlier except that the pipe did not close so the program kept waiting. Don't know what I did and now it is not working at all. Can someone see my code and tell me what am I doing wrong that the str data is not being displayed by cat in child process?

int main(int argc, char** argv) {

    char *str[] = {"The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"};
    int fds[TOTAL_CHILDREN];
    int writeFds;
    int catPID;
    int status;

    FILE * write_to_child;

    //create pipe
    if (pipe(fds) == -1) {
        perror("creating pipe: failed");
        exit(EXIT_FAILURE);
    }
    pipe(fds);
    //create subprocess for cat child

    switch (catPID) {
        case 0: // successful creation of child
            close(fds[1]); //close write side from parents
            close(0); //close stdin
            dup(fds[0]); //connect pipe from execl cat to stdin

            execl("/bin/cat", "cat", (char *) 0);
            perror("exec failed!");
            exit(20);
            break;

        case -1: //failure
            perror("fork failed: cat process");
            exit(EXIT_FAILURE);

        default: //parent process
            close(fds[0]);

            writeFds = fds[1];
            write_to_child = fdopen(fds[1], "w");

            if (write_to_child == NULL) {
                perror("write to pipe failed");
                exit(EXIT_FAILURE);
            }
            break;


    }

    int i;
    for (i = 0; i < 9; i++){
        fprintf(write_to_child, "%s\n", str[i]);
    }

    fclose(write_to_child);
    close(writeFds);

    wait(&status);

    return (EXIT_SUCCESS);
}

解决方案

You probably want to add the line

catPID = fork();

and I'm not sure why you've got pipe(fds) twice

这篇关于Ç管,叉,DUP,和exec()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆