How to fork multiple children

forking multiple children

Page 1 of 1

11 Replies - 24869 Views - Last Post: 22 April 2011 - 10:45 AM Rate Topic: -----

#1 Lbegginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-February 09

How to fork multiple children

Post icon  Posted 17 February 2009 - 01:48 PM

Hi, I am a beginner. I wish to create 9 child processes to a parent process. but I am not sure how to do it. Can someone help?
Is This A Good Question/Topic? 0
  • +

Replies To: How to fork multiple children

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: How to fork multiple children

Posted 17 February 2009 - 02:04 PM

View PostLbegginner, on 17 Feb, 2009 - 07:48 PM, said:

Hi, I am a beginner. I wish to create 9 child processes to a parent process. but I am not sure how to do it. Can someone help?

each time you call fork()
 int pid=fork();

it creates a clone (an identical copy of all code and data) of the current process - it is usual to call the clone the child and fork() returns 0 and the orginal process is the parent and fork returns the PID of the child (if fork() fails it returns -1). Thus the process can determine if they are the parent or child by using the value returned by fork. the parent can continue and call fork() again and/or the child can call fork(). So you can create as many clones as you wish.
Was This Post Helpful? 0
  • +
  • -

#3 Lbegginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-February 09

Re: How to fork multiple children

Posted 17 February 2009 - 03:47 PM

View Posthorace, on 17 Feb, 2009 - 01:04 PM, said:

View PostLbegginner, on 17 Feb, 2009 - 07:48 PM, said:

Hi, I am a beginner. I wish to create 9 child processes to a parent process. but I am not sure how to do it. Can someone help?

each time you call fork()
 int pid=fork();

it creates a clone (an identical copy of all code and data) of the current process - it is usual to call the clone the child and fork() returns 0 and the orginal process is the parent and fork returns the PID of the child (if fork() fails it returns -1). Thus the process can determine if they are the parent or child by using the value returned by fork. the parent can continue and call fork() again and/or the child can call fork(). So you can create as many clones as you wish.


so , would the code below create 9 children ?
thanks for the help.

for(i=0;i<9;i++)
if((result=fork()==-1)
{
printf("Error occoured with fork()\n");
exit(99);
}
if(resul
t=0)
{
//in child
}
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: How to fork multiple children

Posted 17 February 2009 - 04:01 PM

Looks like you're missing some code so :

 pid = fork(); /* creates a new process, you only want the parent to spawn children? */
for (i = 0; i < 9; i++)
{
	switch (pid)
   {
		 case -1:
			/* fork error */
		 case 0:
			/* child process */
		 default:
			 /* parent process*/
			pid = fork(); /* fork new children here */
   }
}


Was This Post Helpful? 0
  • +
  • -

#5 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: How to fork multiple children

Posted 18 February 2009 - 06:57 AM

View PostLbegginner, on 17 Feb, 2009 - 09:47 PM, said:

so , would the code below create 9 children ?
thanks for the help.

yes, but remember that fork() creates a clone which is an exact copy except for the value returned by fork() therefore each child process also contains the loop calling fork() and will start creating its own child processes, e.g.
#include <stdio.h>

int main()
{
int parent=0, child=0, i, result;
for(i=0;i<9;i++)
  {
   if((result=fork())==-1)
	  {
	   printf("Error occoured with fork()\n");
	   exit(99);
	  }
   if(result==0) 
	  printf("child %d %d\n",parent++, child);
   else
	  printf("parent %d %d\n", parent, child++);
  }
}


when run the output was
parent 0 0
child 0 0
child 0 1
parent 0 1
child 1 0
parent 1 0
parent 0 2
parent 1 1
child 0 2
child 1 1
parent 1 1
child 1 1
child 2 0
parent 2 0
parent 1 2
child 1 2
parent 1 2
child 1 2
child 0 3
parent 2 1
child 2 1
parent 0 3
parent 2 1
parent 1 2
parent 2 2
child 2 2
child 1 3
child 2 1
child 1 2
parent 1 3
parent 3 1
...
....


it then goes on for page after page until the virtual memory starts thrashing

you have to look at the value returned by fork() and decide if the current process is the parent and child and take appropriate action. Below is a slightly extended version of the code from the post by KYA
#include <stdio.h>

int main()
{
int parent=0, child=0, i, result, pid;
 pid = fork(); /* creates a new process, you only want the parent to spawn children? */
  for (i = 0; i < 9; i++)
  {
	switch (pid)
   {
		 case -1:
			/* fork error */
			printf("Error occoured with fork()\n");
			exit(99);
		 case 0:
			/* child process */
			printf("child %d %d\n",parent, child++); exit(1); 
		 default:
			 /* parent process*/
			{
			printf("parent %d %d\n", parent++, child);
			pid = fork(); /* fork new children here */
			}
   }
  }
}


in this case the child process stop after the fork() only the parent carries on creating child processes. A run gave
parent 0 0
child 0 0
parent 1 0
child 1 0
parent 2 0
child 2 0
child 3 0
parent 3 0
parent 4 0
child 4 0
parent 5 0
child 5 0
child 6 0
parent 6 0
parent 7 0
child 7 0
parent 8 0
child 8 0


This post has been edited by horace: 18 February 2009 - 06:58 AM

Was This Post Helpful? 1

#6 marky_r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-March 11

Re: How to fork multiple children

Posted 10 March 2011 - 09:39 AM

Hi, Horace,
I'm also a beginner fork()-er.
In your recommended code, line 06 is a fork(). Why doesn't the child process also execute this, create another child (grandchild of parent?),etc, and fork() to oblivion?
Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#7 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: How to fork multiple children

Posted 10 March 2011 - 10:06 AM

View Postmarky_r, on 10 March 2011 - 04:39 PM, said:

Hi, Horace,
I'm also a beginner fork()-er.
In your recommended code, line 06 is a fork(). Why doesn't the child process also execute this, create another child (grandchild of parent?),etc, and fork() to oblivion?
Thanks for your help!

the first program in my last post
#include <stdio.h>

int main()
{
int parent=0, child=0, i, result;
for(i=0;i<9;i++)
  {
   if((result=fork())==-1)
	  {
	   printf("Error occoured with fork()\n");
	   exit(99);
	  }
   if(result==0) 
	  printf("child %d %d\n",parent++, child);
   else
	  printf("parent %d %d\n", parent, child++);
  }
}


does exactly that! The children contain the loop of line 6, so it runs for ever creating multiple children under the system crashes when it runs out of virtual memory.

In the second program only the parent creates more children (the default in line 18 of the switch statement)

This post has been edited by horace: 10 March 2011 - 10:08 AM

Was This Post Helpful? 0
  • +
  • -

#8 marky_r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-March 11

Re: How to fork multiple children

Posted 10 March 2011 - 12:13 PM

Hi, Horace, Thank you for your quick reply. But I was referring to line 06 in the "improved" code. Line 6 is pid = fork(); and appears just before the for-loop. Doesn't the child process run this fork as well, creating a new child, etc?
Thanks!
-Mark
Was This Post Helpful? 0
  • +
  • -

#9 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: How to fork multiple children

Posted 10 March 2011 - 11:20 PM

View Postmarky_r, on 10 March 2011 - 07:13 PM, said:

Hi, Horace, Thank you for your quick reply. But I was referring to line 06 in the "improved" code. Line 6 is pid = fork(); and appears just before the for-loop. Doesn't the child process run this fork as well, creating a new child, etc?
Thanks!
-Mark

consider line 6
#include <stdio.h>

int main()
{
int parent=0, child=0, i, result, pid;
 pid = fork(); /* creates a new process, you only want the parent to spawn children? */
  for (i = 0; i < 9; i++)
  {
	switch (pid)
   {
		 case -1:
			/* fork error */
			printf("Error occoured with fork()\n");
			exit(99);
		 case 0:
			/* child process */
			printf("child %d %d\n",parent, child++); exit(1); 
		 default:
			 /* parent process*/
			{
			printf("parent %d %d\n", parent++, child);
			pid = fork(); /* fork new children here */
			}
   }
  }
}



the parent executes the fork() in line 6 and a child process is created (it is a clone of the parenet, an identical copy except for the value of pid)
1. the parent returns from fork() with the PID of the child, continues execution of the loop and calling fork() to create more children.
2. the child returns from fork() with a PID value of 0, it continues execution, entering the loop and executes case 0 of the switch
case 0:
	/* child process */
	printf("child %d %d\n",parent, child++); exit(1); 

the message is display and the child exits.
thus the child never executes line 6 itself.
Was This Post Helpful? 1
  • +
  • -

#10 marky_r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-March 11

Re: How to fork multiple children

Posted 11 March 2011 - 01:13 AM

View Posthorace, on 10 March 2011 - 11:20 PM, said:

View Postmarky_r, on 10 March 2011 - 07:13 PM, said:

Hi, Horace, Thank you for your quick reply. But I was referring to line 06 in the "improved" code. Line 6 is pid = fork(); and appears just before the for-loop. Doesn't the child process run this fork as well, creating a new child, etc?
Thanks!
-Mark

consider line 6
#include <stdio.h>

int main()
{
int parent=0, child=0, i, result, pid;
 pid = fork(); /* creates a new process, you only want the parent to spawn children? */
  for (i = 0; i < 9; i++)
  {
	switch (pid)
   {
		 case -1:
			/* fork error */
			printf("Error occoured with fork()\n");
			exit(99);
		 case 0:
			/* child process */
			printf("child %d %d\n",parent, child++); exit(1); 
		 default:
			 /* parent process*/
			{
			printf("parent %d %d\n", parent++, child);
			pid = fork(); /* fork new children here */
			}
   }
  }
}



the parent executes the fork() in line 6 and a child process is created (it is a clone of the parenet, an identical copy except for the value of pid)
1. the parent returns from fork() with the PID of the child, continues execution of the loop and calling fork() to create more children.
2. the child returns from fork() with a PID value of 0, it continues execution, entering the loop and executes case 0 of the switch
case 0:
	/* child process */
	printf("child %d %d\n",parent, child++); exit(1); 

the message is display and the child exits.
thus the child never executes line 6 itself.

Was This Post Helpful? 0
  • +
  • -

#11 marky_r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-March 11

Re: How to fork multiple children

Posted 11 March 2011 - 01:19 AM

Ah!!!!! So the child process continues execution at the point just after it was created (the fork()), not at the beginning of the program. Brilliant! I grok!

Thank you so much for your clear explanation!

-M
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: How to fork multiple children

Posted 22 April 2011 - 10:45 AM

New topic created for further discussion.

Closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1