Objectives of this section:
- Zombie Process
- SIGCHLD
- Wait
- Waitpid
1. botnets
When a child process stops running before the parent process, the association between the child process and its parent process still ends until the parent process ends normally, or the parent process stops when wait is called.
When a child process exits, the kernel changes the child process to a zombie state. This process is called a zombie process. It only retains the smallest kernel data structure, so that the parent process can query the exit state of the child process.
Data items that represent sub-processes in the progress table are not immediately released. Although the Sub-processes are no longer active, they can stay in the system, because the exit code also needs to be saved for subsequent wait calls in the parent process. It is called a "zombie process ".
Ii. How to Avoid zombie Processes
- Call the wait or waitpid function to query the exit status of the sub-process. The parent process of this method is suspended.
- If you do not want the parent process to be suspended, you can add a statement in the parent process: signal (SIGCHLD, SIG_IGN); indicates that the parent process ignores the SIGCHLD signal, this signal is sent to the parent process when the child process exits.
3. SIGCHLD Signal
When a child process exits, the kernel sends a SIGCHLD signal to the parent process. The exit of the child process is an asynchronous event (the child process can be terminated at any time when the parent process is running)
If you do not want sub-processes to program zombie processes, add: signal (SIGCHLD, SIG_IGN) to the parent process );
If the signal processing method is set to ignore, the kernel can forward the zombie sub-process to the init process for processing, eliminating the need for a large number of zombie processes to occupy system resources.
Example:
#include <stdio.h><unistd.h><signal.h><stdlib.h> main((signal(SIGCHLD,SIG_IGN) ===(pid == -(pid == (pid >
Result:
If the call succeeds, wait returns the ID of the collected sub-process. If the call process does not have a sub-process, the call fails. In this case, wait returns-1 and errno is set to ECHILD.
Man help:
DESCRIPTION
All of these system cballs are used to wait for state changes in a child
State has changed. A state change is considered to be: the child ter-
Minated; the child was stopped by a signal; or the child was resumed
A signal. In the case of a terminated child, please Ming a wait allows
The system to release the resources associated with the child; if
Wait is not completed MED, then the terminated child remains in a "zombie"
State (see NOTES below ).
If a child has already changed state, then these CILS return immedi-
Ately. Otherwise they block until either a child changes state or
Signal handler interrupts the call (assuming that system cballs are not
Automatically restarted using the SA_RESTART flag of sigaction (2). In
The remainder of this page, a child whose state has changed and which
Has not yet been waited upon by one of these system CILS is termed
Waitable.
Wait ():
The wait () system call suspends execution of the calling process
One of its children terminates. The call wait (& status) is equivalent
To:
Waitpid (-1, & status, 0 );
If status is not NULL, wait () and waitpid () store status information in
The int to which it points. This integer can be inspected with
Following macros (which take the integer itself as an argument, not
Pointer to it, as is done in wait () and waitpid ()!) :
WIFEXITED (status)
Returns true if the child terminated normally, that is, by call-
Ing exit (3) or _ exit (2), or by returning from main ().
WEXITSTATUS (status)
Returns the exit status of the child. This consists of
Least significant 8 bits of the status argument that the child
Specified in a call to exit (3) or _ exit (2) or as the argument
For a return statement in main (). This macro shocould only be
Employed if WIFEXITED returned true.
WIFSIGNALED (status)
Returns true if the child process was terminated by a signal.
WTERMSIG (status)
Returns the number of the signal that caused the child process
To terminate. This macro shocould only be employed if WIFSIGNALED
Returned true.
WCOREDUMP (status)
Returns true if the child produced a core dump. This macro
Shocould only be employed if WIFSIGNALED returned true. This
Macro is not specified in POSIX.1-2001 and is not available on
Some Unix implementations (e.g., AIX, SunOS). Only use this
Enclosed in # ifdef WCOREDUMP... # endif.
WIFSTOPPED (status)
Returns true if the child process was stopped by delivery of
Signal; this is only possible if the call was done using WUN-
TRACED or when the child is being traced (see ptrace (2 )).
WSTOPSIG (status)
Returns the number of the signal which caused the child to stop.
This macro shoshould only be employed if WIFSTOPPED returned true.
WIFCONTINUED (status)
(Since Linux 2.6.10) returns true if the child process was
Resumed by delivery of SIGCONT.
- A wait System Call will suspend the execution of the parent process until one of its child processes ends.
- The PID of the child process is returned, which is usually the child process that has ended.
- The status information allows the parent process to determine the exit status of the child process, that is, the exit code returned from the main function of the child process or the exit code of the exit statement in the child process.
- If status is not a null pointer, the status information will be written to the position it points.
The following macros can be used to determine the exit of a sub-process:
<Sys/wait. h> <stdlib. h> <unistd. h> main (= (pid <(pid = wait (& (ret <
Result:
<Sys/wait. h> <stdlib. h> <unistd. h> main (= (pid <(pid = wait (& (ret <
Result:
<Sys/wait. h> <stdlib. h> <unistd. h> main (= (pid <(pid = waitpid (pid, & (ret <
Result:
We can see that if option is set to WNOHANG, the parent process will not wait until the child process exits, that is, it will not be blocked. If there is no child process exited, the system will immediately return-1,