我已经看过关于这个的一些主题后的等待(),但未能解决这个问题 -憋屈fork()的
我分叉两次,但等待最后PROC当它卡住上等待() ,如果我删除的wait(),它移动,但显然它不会等待:
这里是有关的代码:
/*Fork #1*/
if((pid=fork())==0)
{
if(command -> inputRedirect != NULL)
{
fclose(stdin);
fopen(command -> inputRedirect,"r");
}
fclose(stdout);
dup(pipefd[1]);
close(pipefd[1]);
i=execvp(command->arguments[0],command->arguments);
}
if(i == -1)
{
perror("Error");
return 1;
}
printf("\nParent: %d , Child: %d\n",getpid(),pid);
command = command->next;
/*Fork #2*/
if((pid=fork())==0)
{
fclose(stdin);
dup(pipefd[0]);
close(pipefd[0]);
if(command -> outputRedirect != NULL)
{
fclose(stdout);
fopen(command ->outputRedirect,"w");
}
i=execvp(command->arguments[0],command->arguments);
_exit(0);
}
if(i == -1)
{
perror("Error");
return 1;
}
if(command->blocking == 1)
{
int test=0;
printf("\nParent: %d , Child: %d\n",getpid(),pid);
printf("\ntest1\n");
while((test=wait(NULL)) != pid)
{
printf("\n %d \n ", test);
}
printf("\ntest2\n");
}
return 0;
}
我加了一些测试,看看一些PID输出和最新内部运行,它等待第一个chil进程完成,并打印正确的ID,但第二个只是阻止。
但是我在屏幕上看到第二个过程完成(从过程输出)。
任何有关该主题的帮助将是最受欢迎的。
谢谢。
听起来像第二个孩子不会退出。 –
流程输出不一定是它是否退出的可靠指标。改为通过'ps'命令检查过程列表。 –
不过,我不认为是这样,有两个原因 - 如果一个孩子退出,它仍然是一个“僵尸”,直到等待被调用,其次,如果没有孩子procsess调用wait()什么也不做,我有尝试不同的代码变化,不用一段时间,仍然阻止:/ –