如何让fork的几个子进程并行运行?
我有一个程序叫printnum,在当前路径下。
我又另外写了一个主程序,fork()几个子进程,然后让子进程去调用printnum这个程序,代码如下:
××××××××××××××××××××××××××××××××××××××××××××
int main(int argc, char* argv[])
{
while(1)
{
pid_t pid;
int status;
if ((pid = fork())<0)
status=-1;
else if (0==pid)
{
system("./printnum");
_exit(127);
}
else
{
waitpid(pid,&status,WNOHANG);
}
sleep(5);
}
exit(EXIT_SUCCESS);
}
××××××××××××××××××××××××××××××××××××××××××××
我想让fork的子进程能够同时并行工作,不要做成阻塞式的那种(非得一个子进程结束,另外一个子进程才能够调用),同时在运行过程中,又不能够出现僵尸进程。
请问,应当如何改造我的程序??
谢谢!