当前位置: 技术问答>linux和unix
如何递归创建n个子进程
来源: 互联网 发布时间:2017-01-05
本文导语: 一个父进程,创建一个子进程,子进程再创建一个子进程,······,这样重复n次,还要显示出每一个进程的相关信息,该怎么编程呢?(用C) | 在递归函数里fork | 有三个...
一个父进程,创建一个子进程,子进程再创建一个子进程,······,这样重复n次,还要显示出每一个进程的相关信息,该怎么编程呢?(用C)
|
在递归函数里fork
|
有三个进程只是有一个子进程什么也没做就退出了。 程序逻辑的问题.
if(pid==0)
{
printf("Child Process:n");
printf("pid = %dn", getpid());
printf("ppid = %dn", getppid());
printf("gid = %dn", getpgrp());
deep++;
if (deep 0) { /* parent process */
/* something */
}
}
if(pid==0)
{
printf("Child Process:n");
printf("pid = %dn", getpid());
printf("ppid = %dn", getppid());
printf("gid = %dn", getpgrp());
deep++;
if (deep 0) { /* parent process */
/* something */
}
}
|
child:1 is fork
child:2 is fork
child:3 is fork
child:4 is fork
child:5 is fork
child:6 is fork
child:7 is fork
child:8 is fork
child:9 is fork
child:10 is fork
child:11 is fork
child:12 is fork
child:13 is fork
child:14 is fork
child:15 is fork
child:16 is fork
child:17 is fork
child:18 is fork
child:19 is fork
child:20 is fork
child:21 is fork
child:22 is fork
child:23 is fork
child:24 is fork
child:25 is fork
child:26 is fork
child:27 is fork
child:28 is fork
child:29 is fork
child:30 is fork
child:31 is fork
child:32 is fork
child:33 is fork
child:34 is fork
child:35 is fork
child:36 is fork
child:37 is fork
child:38 is fork
child:39 is fork
child:40 is fork
child:41 is fork
child:42 is fork
child:43 is fork
child:44 is fork
child:45 is fork
child:46 is fork
child:47 is fork
child:48 is fork
child:49 is fork
child:50 is fork
child:50 is waited done
child:49 is waited done
child:48 is waited done
child:47 is waited done
child:46 is waited done
child:45 is waited done
child:44 is waited done
child:43 is waited done
child:42 is waited done
child:41 is waited done
child:40 is waited done
child:39 is waited done
child:38 is waited done
child:37 is waited done
child:36 is waited done
child:35 is waited done
child:34 is waited done
child:33 is waited done
child:32 is waited done
child:31 is waited done
child:30 is waited done
child:29 is waited done
child:28 is waited done
child:27 is waited done
child:26 is waited done
child:25 is waited done
child:24 is waited done
child:23 is waited done
child:22 is waited done
child:21 is waited done
child:20 is waited done
child:19 is waited done
child:18 is waited done
child:17 is waited done
child:16 is waited done
child:15 is waited done
child:14 is waited done
child:13 is waited done
child:12 is waited done
child:11 is waited done
child:10 is waited done
child:9 is waited done
child:8 is waited done
child:7 is waited done
child:6 is waited done
child:5 is waited done
child:4 is waited done
child:3 is waited done
child:2 is waited done
child:1 is waited done
#include
#include
#include
#include
#define NUMBER_OF_PROCS 50
void loopFork(int nProc)
{
if(nProc==NUMBER_OF_PROCS) //total 50 fork called
{
exit(0); //the last child don't fork and just exit.
}
pid_t pid;
if( (pid=fork())>0 )
{
int status;
fprintf(stderr,"child:%d is forkn",nProc+1);
wait(&status); //parent wait child's exit
fprintf(stderr,"child:%d is waited donen",nProc+1);
exit(0); //exit parent itself
}
else if(pid==0) //child continues to fork and wait and exit
{
loopFork(nProc+1);
}
else
{
exit(0); //error:just exit~
}
}
int main()
{
loopFork(0); //fork 50 procs
return 0; //never reach here
}
|
开始执行。
|
这里似乎看不到有递归的思想?
|
其实为了减少时序的不确定性,将打印语句做如下移动将会让程序更完美:
#include
#include
#include
#include
#define NUMBER_OF_PROCS 50
void loopFork(int nProc)
{
if(nProc==NUMBER_OF_PROCS) //total 50 fork called
{
exit(0); //the last child don't fork and just exit.
}
pid_t pid;
if( (pid=fork())>0 )
{
int status;
wait(&status); //parent wait child's exit
fprintf(stderr,"child:%d is waited donen",nProc+1);
exit(0); //exit parent itself
}
else if(pid==0) //child continues to fork and wait and exit
{
fprintf(stderr,"child:%d is forkn",nProc+1);
loopFork(nProc+1);
}
else
{
exit(0); //error:just exit~
}
}
int main()
{
loopFork(0); //fork 50 procs
return 0; //never reach here
}
|
终归于代码流
|
学习了,递归函数!
|
一是为了避免产生一堆孤儿进程,二是为了效果显著有序.
|
就是使劲的fork
|
学习了》1
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。