Solved

fork() -> defunct

Posted on 1998-02-17
1
1,515 Views
Last Modified: 2013-12-26
With a fork command I can create a child process.
But when the child process "finish", it's not realy terminated, why it go in state defunct.
How can I realy terminate a child process without this state?

The problems still in the number of childs defunct that can I have. I maked a par test and the number is a number terminated. I my situation I need create some child that make some jobs in "background", but with this situation I reach at one point that I can't create more child (about 8000 time).
0
Comment
Question by:pedimina
1 Comment
 
LVL 3

Accepted Solution

by:
dhm earned 100 total points
ID: 1296018
When a process fork()s off a child and the child exits, it goes into defunct (also known as "zombie") state until the the parent retrieves the child's exit code with the wait(2) system call.  (There are several flavors of wait() which vary in the amount of information they return and in the way you specify which child process you're interested in.)  If the programmer has forgotten to wait() for child processes he starts, you'll see these defunct process start to accumulate.  They get cleaned up when the parent finishes because the defunct children (all the parent's children, actually) are adopted by process 1, the init process.  One of init's jobs is to wait() for any processes that become its children.

So, in direct answer to your question, if you execute this section of code in your parent process, you'll collect your children's exit codes and clean up the defunct processes:

pid_t       child;
int       exit_status;

while ((child = waitpid( (pid_t)-1, &exit_status, WNOHANG )) != (pid_t)-1) {
      printf( "child %d exited with status %d\n",
            child, WEXITSTATUS(exit_status) );
}

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Complete beginner needs help making a cron job 9 116
EvenOdd challenge 10 107
matchUp  challenge 9 105
noX challenge 17 115
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question