C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to wait for *all* children to stop?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
loudking
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: How to wait for *all* children to stop? Reply with quote



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!
Back to top
Ian Collins
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: How to wait for *all* children to stop? Reply with quote



loudking wrote:
Quote:
Question: write a program which creates 5 processes (in addition to
itself). One of these processes must display 1, another must display
2

Try comp.unix.programmer.

--
Ian Collins.
Back to top
William Ahern
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: How to wait for *all* children to stop? Reply with quote



loudking <loudking (AT) gmail (DOT) com> wrote:
Quote:
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.

snip
while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
{
snip
pid_t pid = getpid();
int i;

<snip>
Quote:
pid = fork();
if (pid < 0)
{
snip


Repost on comp.unix.programmer, since these Unix interfaces are off-topic on
comp.lang.c, and your question involves the behavior of these interfaces,
not of the C language.

I'm inclined to comment further, but many folks here might not know the
semantics of fork() and waitpid()--e.g. many Windows programmers--and so I
might be able to get away w/ completely misleading you (accidentally, of
course).

- Bill
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.