java fork子进程_linux中fork同时创建多个子进程的方法(一)

Fork同时创建多个子进程方法

第一种方法:验证通过

特点:同时创建多个子进程,每个子进程可以执行不同的任务,程序 可读性较好,便于分析,易扩展为多个子进程

#include

#include

int main(void)  {

printf("before fork(), pid = %d\n", getpid());

pid_t p1 = fork();

if ( p1 == 0 ) {

printf("in child 1, pid = %d\n", getpid());

return 0;

//若此处没有return 0 p1 进程也会执行 pid_t p2=fork()语句

}

pid_t p2 = fork();

if ( p2 == 0 )

{

printf("in child 2, pid = %d\n", getpid());

return 0;

//子进程结束,跳回父进程

Printf("hello world\n"); //没有打印

}

int st1, st2;

waitpid( p1, &st1, 0);

waitpid( p2, &st2, 0);

printf("in parent, child 1 pid = %d\n", p1);

printf("in parent, child 2 pid = %d\n", p2);

printf("in parent, pid = %d\n", getpid());

printf("in parent, child 1 exited with %d\n", st1);

printf("in parent, child 2 exited with %d\n", st2);

return 0;

}

[root@localhost ~]# gcc -o thread -pthread t.c

[root@localhost ~]# ./thread

before fork(), pid = 2629

in child 1, pid = 2630

in child 2, pid = 2631

in parent, child 1 pid = 2630

in parent, child 2 pid = 2631

in parent, pid = 2629

in parent, child 1 exited with 0

in parent, child 2 exited with 0

第二种方法: 验证通过

特点:同时创建两个子进程,结构比较繁琐,程序可读性不好,不易扩展

#include

#include

#include //这个头文件不能少,否则pid_t没有定义

main()

{

printf("This is parent process%d\n",getpid());

pid_t p1,p2;

if((p1=fork())==0)

{

printf("This is child_1 process%d\n",getpid());

}else

{

if((p2=fork())==0)

{

printf("This is child_2 process%d\n",getpid());

}else

{

wait(p1,NULL,0);

wait(p2,NULL,0);

printf("This is parent process%d\n",getpid());

}

}

}

第三种方法:for 循环方法

特点:其实每次循环只是创建了单个进程,并没有同时创建多个进程

#include

#include

#include

main()

{

printf("This is parent process%d\n",getpid());

pid_t p1,p2;

int i;

for(i=0;i<=2;i++)

{

if((p1=fork())==0)

{

printf("This is child_1 process%d\n",getpid());

return 0;//这个地方非常关键

}

wait(p1,NULL,0); //父进程等待p1子进程执行后才能继续fork其他子进程

printf("This is parent process%d\n",getpid());

}

}

注意:标注的 return 0 对程序结果影响很大

无 return 0 情况

#include

#include

#include

main()

{

printf("This is parent process%d\n",getpid());

pid_t p1,p2;

int i;

for(i=0;i<=2;i++)

{

if((p1=fork())==0)

{

printf("This is child_1 process%d\n",getpid());

//return 0;//这个地方非常关键

}

wait(p1,NULL,0);

printf("This is parent process%d\n",getpid());

}

}

结论:父进程会生成 n(n+1)/2+1个子进程,N 为循环次数,本例中共有 7 个子进程, 但实际上只有 3 个是父进程产生的,其余都为子进程 fork()出来的。父进程fork了3个进程,第一个子进程执行完之后又fork了2个进程,第2个子进程fork了1个进程。

正确的使用Linux中的用fork()由一个父进程创建同时多个子进程 的格式如下:

int status,i;

for (i = 0; i 

{

status = fork();

if (status == 0 || status == -1) break;//每次循环时,如果发现是子进程就直接从创建子进程的循环中跳出来,不让你进入循环,这样就保证了每次只有父进程来做循环创建子进程的工作

}

if (status == -1)

{

//error

}

else if (status == 0) //每个子进程都会执行的代码

{

//sub process

}

else

{

//parent process

}

怎么创建多个进程呢?我说那还不容易,看下边代码:

//省略必要头文件

int main()

{

pid_t pid[2];

int i;

printf("This is %d\n",getpid());

for(i = 0;i 

if((pid[0] = fork()) 

printf("Fork() Error!");

exit(-1);

}

if(pid[0] == 0)

printf("This is parent %d,child is %d\n",getppid(),getpid());

else

wait(5);

}

return 0;

}

好,这段代码还是挺简单的,我们的意思是:主线程通过循环创建2个子进程,这时系统中的总进程数应该是3,看看输出结果吧:

eb0d98a8ae172c55401baf00b13277e0.png

这个结果图看的效果不好,我们看个直接点的:

e32133218f7119ab3031ee9f8178fe4c.png

这下你明白了吧,问题没有想象中的那样简单,父进程现在标号为1的循环中创了一个子进程,然后第二次循环,前边的第一个子线程又创建一个子进程,这时明显系统中有四个进程,还是不懂?在下边的时序图吧:

56e5b7e4e0c2baeb651cd7e8eb59fb78.png

这下你应该明白了吧,好了问题知道了,怎么解决,方法有二;

方法一:直接看代码  for循环

void createsubprocess(int num)

{

pid_t pid;

int i;

for(i=0;i

{

pid=fork();

if(pid==0||pid==-1)  //子进程或创建进程失败均退出,这里是关键所在

{

break;

}

}

if(pid==-1)

{

perror("fail to fork!\n");

exit(1);

}

else if(pid==0)

{

printf("子进程id=%d,其对应的父进程id=%d\n",getpid(),getppid());

exit(0);

}

else

{

printf("父进程id=%d\n",getpid());

exit(0);

}

}

这种方法的关键就在于每次循环时,如果发现是子进程就直接从创建子进程的循环中跳出来,不让你进入循环,这样就保证了每次只有父进程来做循环创建子进程的工作。

方法二:直接看代码 递归函数

void createsubprocess(int num,int max)

{

if(num>=max)return;

pid=fork();

if(pid<0)

{

perror("fork error!\n");

exit(1);

}

//子进程

else if(pid==0)

{

sleep(3);

printf("子进程id=%d,父进程id=%d\n",getpid(),getppid());

}

//父进程

else

{

num++;

if(num==1)printf("父进程id=%d\n",getpid());

if(num

//此处加sleep是为了防止父进程先退出,从而产生异常

sleep(5);

}

}

这里的关键在于递归操作,只有父进程才进入递归创建子进程,子进程不进行这样的操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值