LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   A program which exists only after all its children have exited.. plz comment (http://www.linuxquestions.org/questions/programming-9/a-program-which-exists-only-after-all-its-children-have-exited-plz-comment-321405/)

tuxfood 05-08-2005 04:56 PM

A program which exists only after all its children have exited.. plz comment
 
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..\n");
perror("waitpid");
break;
}

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

default: //i.e somechild has exited. so remove child from wait_list
printf("process with pid - %d exited..removing from list\n",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

tuxfood 05-08-2005 04:59 PM

Sorry abt the heading.... its

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

Can this be edited in the forum page anyway????

thnks and sorry

tuxfood


All times are GMT -5. The time now is 07:02 PM.