关于linux c调用子进程
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
void main()
{
int n,j=0,q,p;
pid_t pid;
n = getpid();
printf("i'm father%d\n",n);
for(j=0;j<4;j++)
{
// printf("%d",j);
pid = fork();
if(pid==0)
{
q=getpid();
printf("i'm children %d\n",q);
// exit(0);
}
/* if(pid>0)
{
pid= wait(NULL);
// p=getpid();
// printf("i'm father%d\n",p);
}*/
}
}
i'm children 7129
i'm children 7127
i'm children 7124
i'm children 7126
i'm children 7130
i'm children 7131
i'm children 7134
i'm children 7135
i'm children 7136
i'm children 7133
i'm children 7132
i'm children 7137
为什么注释掉子进程的exit(0)之后会输出这么多行。而且父进程却没有输出,我想,也许是,每个子进程都跑去循环了。。。麻烦各位给点自己的看法
[解决办法]
输出的问题请看我的验证,输出是把第六行的printf注掉了
root@ubuntu:~# ./mytest
i'm children 11888
i'm children 11889
i'm children 11890
i'm children 11891
i'm father11890
i'm father11889
i'm children 11892
i'm father11889
i'm father11888
i'm children 11893
i'm children 11894
i'm father11893
i'm father11888
i'm children 11895
i'm father11888
i'm father11887
i'm children 11896
i'm children 11897
i'm children 11898
i'm father11897
i'm father11896
i'm children 11899
i'm father11896
i'm father11887
i'm children 11900
i'm children 11901
i'm father11900
i'm father11887
i'm children 11902
i'm father11887