forking n number of processes in a loop and not 2^n


 
Thread Tools Search this Thread
Top Forums Programming forking n number of processes in a loop and not 2^n
# 1  
Old 01-09-2007
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
# 2  
Old 01-09-2007
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
# 3  
Old 01-10-2007
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;
}

# 4  
Old 01-10-2007
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
# 5  
Old 01-17-2007
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Maxuproc parameter and number of processes

Hi there, I am having a problem on an AIX server running a WebSphere MQ instance. The problem is that sometimes it seems to reach process limit, but I do not find the processes themselves. What I see: succeed to log in (as root from console os as nonpriviliged user via ssh). Trying to run... (19 Replies)
Discussion started by: trifo75
19 Replies

2. Shell Programming and Scripting

Controlling the Number of Child processes

I am trying to implement the below using Ksh script on a Lx machine. There is a file(input_file) with 100K records. For each of these records, certain script(process_rec) needs to be called with the record as input. Sequential processing is time-consuming and parallel processing would eat up... (2 Replies)
Discussion started by: APT_3009
2 Replies

3. Shell Programming and Scripting

Keep up constant number of parallel processes

Hi guys, I am struggling with adapting my script to increase the performance. I created a ksh script to process a lot of files in parallel. I would like to know how can I do in such a way that a constant number of processes is always up (until all is finished). What I have is (not actual... (8 Replies)
Discussion started by: lurkerro
8 Replies

4. UNIX for Dummies Questions & Answers

For loop for checking processes

Hi Gang, I need part of my script to be able to loop, check for processes running and if they aren't running, start them. It needs to loop 5 times, do a check each time, and make sure a process starts, and if its running; skip it. I've worked with loops and checking for processes before,... (8 Replies)
Discussion started by: jeffs42885
8 Replies

5. Shell Programming and Scripting

Forking a bunch of processes and filling up the process table

I have a bash script that has been used for months here at work for doing an SSH into other machines both Linux and Solaris and running a script on the remote machine. Recently I have started to noticed that things are being left being on the maching doing the SSH. For example.... tivoli ... (1 Reply)
Discussion started by: LRoberts
1 Replies

6. Shell Programming and Scripting

Keep a certain number of background processes running

I've got a bit of code I'm trying to work on... What i want to happen is ... at all times have four parallel mysql dump and imports running. I found the follow code snippet on the forum and modified it to work by starting four concurrent processes but it waits until all four are done before... (7 Replies)
Discussion started by: dgob123
7 Replies

7. Shell Programming and Scripting

Determine Number of Processes Running

I am pretty new to unix, and I have a project to do. Part of the project asks me to determine the number of processes running and assign it to a variable. I know how to every part of the project but determine the number of processes running. How can I get just the number of processes... (4 Replies)
Discussion started by: wayne1411
4 Replies

8. Solaris

How to find number of processes ?

Hi , I need to count all processes contains the pattren "FND" For Example: I was reteriving the details of all processes related to "FND" by this command $ ps -ef | grep FND but now I just wanna count them . Regards Adel (2 Replies)
Discussion started by: ArabOracle.com
2 Replies

9. UNIX for Dummies Questions & Answers

forking and killing parent processes

Hi everybody, I'm having some problems wiriting a program in UNIX using the "fork" and "kill" system calls. I have to create a C program P0, which creates 9 other processes P1, P2, ..., P9, where P0 is the father of P1, P1 the father of P2, and so on. All the processes contain an infinite... (0 Replies)
Discussion started by: davewilliams20
0 Replies

10. Programming

Forking in a loop

When I compile this C programme I get different outputs each time I run it Please explain to me whats happening in the code if you can give me a detailed explanation. Because I am stuck with this. #include <stdio.h> main(){ int i = 0; printf("I am the... (1 Reply)
Discussion started by: manjuWicky
1 Replies
Login or Register to Ask a Question