谁能告诉我这有什么问题?我仍然是分叉的新手。计算机执行第1和第2,但第3个是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);
}
}
}
}
答案 0 :(得分:0)
由于对mark a comment as an answer的功能请求仍然被拒绝,我在此处复制上述解决方案。
你忘记了路径中的主要斜线。 - Joachim Pileborg
&#34; USR /斌/ WC&#34;应该是&#34; / usr / bin / wc&#34; - Basile Starynkevitch