How to wait *all* children processes to return?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > How to wait *all* children processes to return?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-17-07 06: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!






[ Post a follow-up to this message ]



    Re: How to wait *all* children processes to return?  
Rainer Weikusat


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-17-07 06: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.





[ Post a follow-up to this message ]



    Re: How to wait *all* children processes to return?  
sail0r@creepjoint.net


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-19-07 12:17 AM

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.





[ Post a follow-up to this message ]



    Re: How to wait *all* children processes to return?  
sail0r@creepjoint.net


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-19-07 12:17 AM

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 ;)





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:27 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register