View Full Version : Running Multiple Process
nebu
November 10th, 2009, 04:21 PM
Say i create a c program like
#include <stdlib.h>
int main(){
int i;
for(i=0;i<5;i++)
fork();
//Some code
return 0;
}
how do i make the parent process finish only after all the 5 children have terminated?
Arndt
November 10th, 2009, 05:11 PM
Say i create a c program like
#include <stdlib.h>
int main(){
int i;
for(i=0;i<5;i++)
fork();
//Some code
return 0;
}
how do i make the parent process finish only after all the 5 children have terminated?
You use 'wait' or one of the other functions described on the same manual page.
nebu
November 10th, 2009, 05:57 PM
could u elaborate with some code....
i am not getting very far with the manual pages
dwhitney67
November 10th, 2009, 06:42 PM
It amounts to something as simple as...
man 2 wait
In case you find yourself dazed/confused/deer-in-headlights, here's a basic example...
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
void childDoSomething();
int main()
{
pid_t chld_pid[5] = {0};
int num_chld = 0;
for (int i = 0; i < 5; ++i)
{
chld_pid[i] = fork();
if (chld_pid[i] == 0)
{
// we are the child; do something very unconstructive
childDoSomething();
exit(0);
}
else if (chld_pid[i] > 0)
{
++num_chld;
}
}
while (num_chld--)
{
sleep(1);
printf("Waiting for children to die...\n");
for (int i = 0; i < 5; ++i)
{
int status;
waitpid(chld_pid[i], &status, 0);
}
}
printf("All children are dead... dead I tell you!\n");
return 0;
}
void childDoSomething()
{
for (int i = 0; i < 5; ++i)
{
sleep(1);
if (i == 0)
printf("The child is running.\n");
else
printf("The child is still running.\n");
}
printf("The child is done.\n");
}
MindSz
November 10th, 2009, 09:24 PM
You can also use threads.
http://www.cs.cf.ac.uk/Dave/C/node32.html
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.