谁能告诉我这有什么问题?我仍然是分叉的新手。计算机执行第一个和第二个,但第三个是 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);
}
}
}
}