0

谁能告诉我这有什么问题?我仍然是分叉的新手。计算机执行第一个和第二个,但第三个是 wc 不起作用。非常需要帮助。终端返回多个子进程完成但没有 wc。

pid_t son;
int i;

for (i=0; i<=3; i++){
        switch (i){
            case 0:
            son = fork();
                if (son<0){
                    fprintf(stderr, "Fork failed!");
                    //exit(-1);
                }else if (son == 0){
                    execlp("/bin/cat", "cat", "wctrial.txt", NULL);
                    exit(0);
                }else{
                wait(NULL);
                printf("Child process completed!");

                }
            case 1:
            son = fork();
                if (son<0){
                    fprintf(stderr, "Fork failed!");
                    //exit(-1);
                }else if (son == 0){
                    execlp("/bin/mkdir", "mkdir", "mydirectory", NULL);
                    exit(0);
                }else{
                wait(NULL);
                printf("Child process completed!");
                }
            case 2:
            son = fork();
                if (son<0){
                    fprintf(stderr, "Fork failed!");
                    //exit(-1);
                }else if (son == 0){
                    printf("Work!");
                    execlp("usr/bin/wc","wc","-w","wctrial.txt", NULL);
                    exit(0);
                }else{
                wait(NULL);
                printf("Work!");
                printf("Child process completed!");
                exit(0);
                }
        }
}

}

4

1 回答 1

0

由于将评论标记为答案的功能请求仍然被拒绝,因此我在此处复制上述解决方案。

您忘记了路径中的前导斜杠。——约阿希姆·皮勒伯格

“usr/bin/wc”应该是“/usr/bin/wc”——Basile Starynkevitch

于 2014-06-06T12:30:02.557 回答