Unix Programming - How to wait *all* children processes to return?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2007 > How to wait *all* children processes to return?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to wait *all* children processes to return?
loudking

2007-05-17, 1:18 pm

Question: write a program which creates 5 processes (in addition to
itself). One of these processes must display 1, another must display
2 ... the last one displays 5. The parent process waits until all
other
processes are finished, then returned.

My solution is that

/* Header files omitted */

#define NUMBER_PROCESS 5

void sig_chld(int sig)
{
pid_t pid;
int stat;

while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
{
;
}
signal(SIGCHLD, sig_chld);

}

int main(int argc, char *argv[])
{
pid_t pid = getpid();
int i;

signal(SIGCHLD, sig_chld);

for (i = 0; i < NUMBER_PROCESS && pid > 0; i++)
{
pid = fork();
if (pid < 0)
{
perror("Error fork");
exit(-1);
}
else if (pid == 0)
{
printf("%d\n", i+1);
}
}/* for i */

return 0;

}

Should I add a for loop outside the while loop in sig_chld function
to
make sure that exactly 5 child termination are captured?

Thanks!

Rainer Weikusat

2007-05-17, 1:18 pm

loudking <loudking@gmail.com> writes:
> Question: write a program which creates 5 processes (in addition to
> itself). One of these processes must display 1, another must display
> 2 ... the last one displays 5. The parent process waits until all
> other
> processes are finished, then returned.


[...]


> int main(int argc, char *argv[])
> {
> pid_t pid = getpid();
> int i;
>
> signal(SIGCHLD, sig_chld);
>
> for (i = 0; i < NUMBER_PROCESS && pid > 0; i++)
> {
> pid = fork();
> if (pid < 0)
> {
> perror("Error fork");
> exit(-1);
> }
> else if (pid == 0)
> {
> printf("%d\n", i+1);
> }
> }/* for i */
>
> return 0;
>
> }


This program will create a lot more than five processes.
sail0r@creepjoint.net

2007-05-18, 7:17 pm

Rainer Weikusat wrote:
[code snipped]
> This program will create a lot more than five processes.

Why do you say that? I copied and pasted that code and, after adding the
necessary header of course!, it created exactly five processes.
sail0r@creepjoint.net

2007-05-18, 7:17 pm

sail0r@creepjoint.net wrote:
> Rainer Weikusat wrote:
> [code snipped]
> Why do you say that? I copied and pasted that code and, after adding the
> necessary header of course!, it created exactly five processes.

Of course, the OP *does* have the printf's all being done by the parent
for some reason...
Anyway, here is some code that does the trick I believe
-------------------------
#define NUMBER_PROCESS 5
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
int main(int argc, char *argv[])
{
pid_t pid = getpid();
int i=0;

pid=fork();
while(i<NUMBER_PROCESS && pid>0){
printf("PID:%d i:%d\n",pid,i);
pid=fork();
i++;
}
}
-------------
[user@host~]$ ./a.out
PID:23950 i:0
PID:23951 i:1
PID:23952 i:2
PID:23953 i:3
PID:23954 i:4

ok, I am not sure if this is *exactly* the OPs problem...
The parent isn't spawning all the child processes and I printed 0
through 4 and not 1 through 5 ;)
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com