创建N个子进程

在Linux下创建N个子进程

创建进程用fork()

函数原型

SYNOPSIS
       #include <sys/types.h>
       #include <unistd.h>
       pid_t fork(void);
DESCRIPTION
       fork()  creates  a new process by duplicating the calling process.  The
       new process, referred to as the child, is an  exact  duplicate  of  the
       calling  process,  referred  to as the parent, except for the following
       points:
       *   The child has its own unique process ID,  and  this  PID  does  not
           match the ID of any existing process group (setpgid(2)).
       *   The  child’s  parent process ID is the same as the parent’s process
           ID.
       *   The child does not inherit its  parent’s  memory  locks  (mlock(2),
           mlockall(2)).
       *   Process  resource utilizations (getrusage(2)) and CPU time counters
           (times(2)) are reset to zero in the child.
       *   The child’s set of pending signals  is  initially  empty  (sigpend-
           ing(2)).
       *   The  child  does  not inherit semaphore adjustments from its parent
           (semop(2)).
       *   The child does not inherit record locks from its parent (fcntl(2)).
       *   The  child  does  not  inherit timers from its parent (setitimer(2)
           alarm(2), timer_create(3)).
       *   The child does not inherit outstanding asynchronous I/O  operations
           from its parent (aio_read(3), aio_write(3)).
       The  process  attributes  in  the  preceding  list are all specified in
       POSIX.1-2001.  The parent and child also differ  with  respect  to  the
       following Linux-specific process attributes:
       *   The child does not inherit directory change notifications (dnotify)

 from its parent (see the description of F_NOTIFY in fcntl(2)).
       *   The prctl(2) PR_SET_PDEATHSIG setting is reset so  that  the  child
           does not receive a signal when its parent terminates.
       *   Memory   mappings   that  have  been  marked  with  the  madvise(2)
           MADV_DONTFORK flag are not inherited across a fork(2).
       *   The  termination  signal  of  the  child  is  always  SIGCHLD  (see
           clone(2)).
       Note the following further points:
       *   The  child  process  is created with a single thread — the one that
           called fork(2).  The entire virtual address space of the parent  is
           replicated in the child, including the states of mutexes, condition
           variables, and other pthreads objects; the use of pthread_atfork(3)
           may be helpful for dealing with problems that this can cause.
       *   The child inherits copies of the parent’s set of open file descrip-
           tors.  Each file descriptor in the child refers to  the  same  open
           file description (see open(2)) as the corresponding file descriptor
           in the parent.  This means that the two descriptors share open file
           status flags, current file offset, and signal-driven I/O attributes
           (see the description of F_SETOWN and F_SETSIG in fcntl(2)).
       *   The child inherits copies of the parent’s set of open message queue
           descriptors  (see  mq_overview(7)).   Each  descriptor in the child
           refers to the same open message queue  description  as  the  corre-
           sponding  descriptor  in  the  parent.   This  means  that  the two
           descriptors share the same flags (mq_flags).
RETURN VALUE
       On success, the PID of the child process is returned  in  the  parent’s
       thread  of execution, and a 0 is returned in the child’s thread of exe-
       cution.  On failure, a -1 will be returned in the parent’s context,  no
       child process will be created, and errno will be set appropriately.


这里特别注意的一点是,不是fork()函数返回值有两个,而是fork()后,fork()函数一个变为两个,父子各自需要返回一个。


一次fork()函数可以创建一个子进程,那么创建N个子进程应该怎么实现?

简单的想,for(i=0;i<n;i++),但是这样真的可以创建n个子进程吗?

答案是否定的,如下图:


这里很明显的创建了(2^5-1)个子进程.


创建N个子进程的代码如下

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
	int i;
	pid_t pid;
	for(i=0;i<5;i++)
	{
		pid=fork();
		if(-1==pid)
		{
			printf("fork:%m\n");
			exit(-1);
		}
		else if(pid==0)
		{
			break;
		}//如果不加这个判断条件,会添加2的5次方-1个进程
  	}
  	if(pid<5)
  	{
  		printf("我是子进程%d,pid=%u\n",i+1,getpid());//getpid()获得子进程号
  	}
	return 0;
}
代码运行结果:

我是子进程1,pid=22284
我是子进程2,pid=22285
我是子进程3,pid=22286
我是子进程4,pid=22287
我是子进程5,pid=22288


这里我们在循环过程中,保证子进程不再执行fork()函数,从而保证了创建了N个子进程。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值