void execute(char **argv,int num)
{
int i;
pid_t pid;
int status;
int child_status;
if ((pid = fork()) < 0)
{ /* fork a child process*/
printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0)
{ /* for the child process: */
int c;
if (c==execvp(argv[0], argv) < 0)
{ /* execute the command */
printf("%d\n", c);
printf("*** ERROR: exec failed\n");
perror(" ");
exit(1);
}
}
else if(bg!=1){
while (waitpid(pid,&status,WNOHANG|WUNTRACED));
}
else if(bg==1)
{
wait (&child_status);
if (WIFEXITED (child_status))
{
printf("the child process exited normally, with exit code %d\n",
WEXITSTATUS (child_status));
}
else
printf ("the child process exited abnormally\n");
}
}
这是我的自定义shell中的执行函数。当我执行gedit &
之类的操作时,将在打印下一个提示之前打印退出状态。我该如何解决?
我做错了什么?
答案 0 :(得分:0)
无论变量bg设置为什么,execute()都会阻塞等待函数完成。换句话说,无论您是否想在后台运行流程,都会获得相同的行为。
一些建议: