Solved

fork() -> defunct

Posted on 1998-02-17
1
1,511 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 57
deburging in oracle form 12 76
iSeries DB2 Query 2 83
sum13 challenge 24 77
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now