Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

fork() -> defunct

Posted on 1998-02-17
1
Medium Priority
?
1,523 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 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Suggested Courses

719 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