include
#include
#include
int main(int argc,char * argv[])
{
pid_t childpid=0;
int i,n;
if(argc !=2)
{
fprintf(stderr,"Usage:%s process\n",argv[0]);
return 1;
}
n=atoi(argv[1]);
for(i=1;i {
childpid=fork();
printf("i is :%d,childpid value is :%d and pid is :%d\n",i,childpid,getpid());
if(childpid)
break;
}
/* sleep(2);*/
fprintf(stderr,"i:%d process ID: %ld parent ID:%ld child ID: %ld\n",i,(long)getpid(),(long)getppid(),(long)childpid);
/* sleep(120);*/
return 0;
}
/*执行结果, fork 之后,是父进程先执行还是子进程先行,取决于内核的调度。
[root@localhost code]# ./t_fork3 5
i is :1,childpid value is :0 and pid is :11526
i is :2,childpid value is :0 and pid is :11527
i is :3,childpid value is :0 and pid is :11528
i is :4,childpid value is :0 and pid is :11529
i:5 process ID: 11529 parent ID:11528 child ID: 0
i is :4,childpid value is :11529 and pid is :11528
i:4 process ID: 11528 parent ID:11527 child ID: 11529
i is :3,childpid value is :11528 and pid is :11527
i:3 process ID: 11527 parent ID:11526 child ID: 11528
i is :2,childpid value is :11527 and pid is :11526
i:2 process ID: 11526 parent ID:11525 child ID: 11527
i is :1,childpid value is :11526 and pid is :11525
i:1 process ID: 11525 parent ID:11352 child ID: 11526
阅读(1431) | 评论(0) | 转发(0) |