How to get rid of <defunct> child processes

This is my code. I fork Y times to get Y childs and it does fine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 while(index<Y) // Y is the number of childs to be created
           {
        	   if(p_id>0){/
        	
              		   p_id=fork();

        		   if(p_id>0){//Parent
        			   
        			   pid_list[index]=p_id;
            		   	   index++;

        	   	   }else if (p_id==0) {//Si es el nuevo hijo
        		   
        		   anotherclass::do_some_stuff_and_stay_there();
        		  
        		   exit(EXIT_SUCCESS); 
        	   	   }
        	   	   else if(p_id<0){
        	   		   printf("Error creating child");        	   	   }
        	  }
           }


Every child should call do_some_stuff_and_stay_there() , that function at some point will end, but they are executed independently.

Later I have to check every while whether those processes have ended, which should happen when the function ended.

1
2
3
4
5
6
7
8
9
processfolder="/proc/"+pid_number;
if (access(processfolder.c_str(), F_OK) != -1){
        			printf(" Process is running\n");
        			act=true;
        			}
        			else{
        				act=false;
        				printf(" Process  is NOT running\n");
        			}


But it always detects "process is running" because the process gets stuck in a <defunct> status, and I can't kill the parent so I run out of ideas how to detect that and kill it. ¿Any clue ?
Last edited on
You need to wait for the child processes. See the waitpid() man page.
Great, thats what I needed, problem solved :) Thank you

I found this particular topic very useful :
http://www.cplusplus.com/forum/unices/925/
Topic archived. No new replies allowed.