简体   繁体   中英

WC isn't executing anything

Can anyone tell me what's wrong with this? I'm still a newbie with forking. The computer executes the 1st and 2nd but the 3rd which is wc doesn't work. Need help badly. The terminal returns multiple child process completed but no 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);
                }
        }
}

}

Since feature requests to mark a comment as an answer remain declined, I copy the above solution here.

You forgot the leading slash in the path. – Joachim Pileborg

"usr/bin/wc" should be "/usr/bin/wc" – Basile Starynkevitch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM