一个最简单的进程调用学习出现的问题
我的程序大致如下:
×××××××××××××××××××××××××××××××××××××××
void printnum()
{
int i;
for (i=0;i<2;i++)
printf("%d\n",i);
}
int main(int argc, char* argv[])
{
pid_t pid;
int status;
for(int i=0;i<2;i++)
{
if ((pid = fork())<0)
status=-1;
else if (0==pid)
{
system("./printnum");
_exit(127);
}
else
{
while (waitpid(pid,&status,WNOHANG)<0)
if (errno!=EINTR)
{
status=-1;
break;
}
}
}
return status;
}
×××××××××××××××××××××××××××××××××××××××
运行该程序testpid,为什么有时候的运行结果如下:
[root@SIG vincent]# ./testpid
0
1
[root@SIG vincent]# 0 (为什么正常输出的字符串跑到这里了???)
1
(为什么光标停留在此处????)
×××××××××××××××××××××××××××××××××××××××××××
有时候的运行结果如下:
[root@SIG vincent]# ./testpid
[root@SIG vincent]# 0 (为什么正常输出的字符串又跑到这里了???)
1
0
1
(为什么光标又停留在此处????)
真的很诡异。