The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Determine Number of Processes Running wayne1411 Shell Programming and Scripting 4 12-17-2007 12:06 PM
How to find number of processes ? ArabOracle.com SUN Solaris 2 02-14-2006 12:29 AM
forking and killing parent processes davewilliams20 UNIX for Dummies Questions & Answers 0 10-17-2005 02:40 AM
Forking in a loop manjuWicky High Level Programming 1 10-07-2005 09:25 AM
Max number of concurrent processes SmartJuniorUnix UNIX for Dummies Questions & Answers 1 09-27-2000 01:59 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-09-2007
Registered User
 

Join Date: Jan 2007
Posts: 30
forking n number of processes in a loop and stack size of the child processes

Hi,

Am working on linux. while forking in a loop how to avoid the child process from forking..for example

int n = 5;
pid_t pid;
pid_t ch_pid[5];

/*exactly wanted to create n processes and not 2^n processes*/

for(i = 0; i < n;i++)
{
if(pid = fork())
{
/* parent process */
ch_pid[n] = pid;
waitpid(pid);
}
else if(pid == 0)
{
/*child process*/
spawn_process(ch_pid[n], n);
/*In spawn_process() i will call exec()*/
}
else
{
/*Handle error*/
}
}
1. Once the child process calls exec(), do I need to do something to restrict the child process from forking?

2. What will be the stack size allocated for the child processes?

Thanks,
Anand
Reply With Quote
Forum Sponsor
  #2  
Old 01-09-2007
Registered User
 

Join Date: Jan 2007
Posts: 30
forking n number of processes in a loop and not 2^n

Hi,

Am working on linux. while forking in a loop how to avoid the child process from forking..for example

int n = 5;
pid_t pid;
pid_t ch_pid[5];

/*exactly wanted to create n processes and not 2^n processes*/

for(i = 0; i < n;i++)
{
if(pid = fork())
{
/* parent process */
ch_pid[n] = pid;
waitpid(pid);
}
else if(pid == 0)
{
/*child process*/
spawn_process(ch_pid[n], n);
/*In spawn_process() i will call exec()*/
}
else
{
/*Handle error*/
}
}
1. Once the child process calls exec(), do I need to do something to restrict the child process from forking?

2. What will be the stack size allocated for the child processes?

Thanks,
Anand
Reply With Quote
  #3  
Old 01-10-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
This is how to control the number of children. I'll let you work out calling execl()
Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int foo(const char *whoami)
{
	printf("I am a %s.  My pid is:%d  my ppid is %d\n",
			whoami, getpid(), getppid() );
	return 1;
}

int main(int argc, char **argv)
{
	int n=atoi(argv[1]);
	int i=0;
	int status=0;

	if (n==0) n=2;
	printf("Creating %d children\n", n);
	foo("parent");
	for(i=0;i<n;i++)
	{
		pid_t pid=fork();
		if (pid==0) /* only execute this if child */
		{
			foo("child");
			exit(0);
		}
		wait(&status);  /* only the parent waits */
	}
	return 0;
}
Reply With Quote
  #4  
Old 01-10-2007
elzalem's Avatar
Registered User
 

Join Date: Nov 2006
Location: Lebanon
Posts: 33
to prevent the child from spawning another child, kill that child before it loops!
in other words,don't ever let it loop the for( ; ; ), or add if (pid !=0 ) before the fork () call
Reply With Quote
  #5  
Old 01-17-2007
Registered User
 

Join Date: Jan 2007
Posts: 30
thnk u

Hi guys ,

thnk u for ur help
i don't want to kill the child processess
"if (pid !=0 )" helped me a lot..
thnk u once again..
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0