Solved

fork() -> defunct

Posted on 1998-02-17
1
1,521 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

632 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