A program which exits only after all its children have exited.. plz c

hi...

This is just a trial to write a program in which the parent quits only after all its children have finished executin..

so heres what i did

Now i case i am talking abt here is that the parent creates all the child process and then only starts to wait for them


1) so for each process forked().. i put the pid of that process into a list.
2) after creating the process the parent waits all children have exited..
the code for this is something like this


wait_list is the list to which the pids are added
Addtolist() adds a value pid to the wait_list
RemovefromList () removes the node with value pid from the wait_list

/**************************************************************

for(;..........)
{
.......

switch ( pid = fork ())
{
case -1 : perror("fork");
break;
case 0:
//child
execl( ........ );

default:
//parent
printf("starting executing of process with pid.. %d",pid);
AddtoList(pid);
break;
}
}

while(wait_list!=(List *)0)
{

pid = waitpid(0,&wait_val,__WALL);
if(pid == -1)
{
//printf("do we reach here..
");
perror("waitpid");
break;
}

switch ( pid ){
case 0: //printf("no child exited..
");
break;

default: //i.e somechild has exited. so remove child from wait_list
printf("process with pid - %d exited..removing from list
",pid);
RemovefromList(pid);
break;
}
}


/***************************************************************

One questions about this piece of code


Sometimes in waitpid() call .. it gives an error and prints out... "No Child Processes "

But in each case i tried all child processes had finished execution before this error came... does anyone know why?


thnks

tuxfood



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories