Solved

fork() -> defunct

Posted on 1998-02-17
1
1,519 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 56
Detect CR LF to each line 12 185
Smart Camera scanning and reading information 3 126
Example code 13 42
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Suggested Courses

734 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