0
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");
}
}
这是我的自定义外壳程序中的执行功能。当我执行诸如gedit &
之类的操作时,会在打印下一个提示之前打印退出状态。我该如何解决? 我在做什么错?该过程的退出状态在退出之前已打印
你是什么意思“打印之前”?你想要它打印后? –
当子进程退出时,它应该显示退出状态的权利?像gedit&在后台打开文本编辑器。当我点击关闭该过程时,然后才显示退出状态。在此之前,这个过程还在继续......好吗? –
好吧,如果有一个已经运行的gedit实例,并且你再次运行它,新实例将立即退出。许多GUI程序都这样做,所以尝试使用非GUI界面程序。 – redneb