WC isn';我不会执行任何事情
谁能告诉我这有什么问题吗?我还是个菜鸟。计算机执行第一个和第二个,但第三个wc不工作。我急需帮助。终端返回多个子进程已完成但没有wcWC isn';我不会执行任何事情,c,fork,C,Fork,谁能告诉我这有什么问题吗?我还是个菜鸟。计算机执行第一个和第二个,但第三个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
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);
}
}
}
pid\t-son;
int i;
对于(i=0;i,由于特性请求被拒绝,我在这里复制上述解决方案
你忘了前面的斜杠了
“usr/bin/wc”应该是“/usr/bin/wc”-Basile Starynkevitch您忘记了路径中的前导斜杠。您忘记了在printf
格式字符串的末尾添加换行符\n
,并调用fflush
(至少就在fork
之前)。另外,用gcc-Wall-g
编译你的程序,并用gdb
或至少strace
调试它,你的前斜杠是什么意思?“usr/bin/wc”
应该是“/usr/bin/wc”
调用fflush(NULL)
,请阅读