Linux under C programming: about process waiting

Source: Internet
Author: User
Tags exit include printf terminates linux
#include <sys/types.h> #include <stdio.h> #include <sys/wait.h> void Chec      
           
K_exit (int status);      
    Main () {pid_t pid;      
    int status; if ((PID = fork ()) < 0) {printf ("fork error!!      
    \ n ");      
    Exit (0);      
  
    else if (PID = = 0) {printf ("Child process exit\n");      
    Exit (0);      
        else {if (&status)!= pid) {printf ("Wait error!!");      
        Exit (0);      
    } check_exit (status); } void Check_exit (int status) {if (wifexited (status)) printf ("Eixt      
               
    \ n ");      
    else if (wifsignaled (status)) printf ("Killed by signal\n");      
    else if (wifstopped (status)) printf ("Stopped by signal\n"); else if (wifcontinued (status))      
        printf ("Continued"); }

Waits for the process to change its state. All of the following calls are used to wait for a change in the state of the child process to obtain information about the child process that the state has changed. The state change can be considered to be: 1. The child process has been terminated. 2. The signal causes the child process to stop executing. 3. Implementation of signal recovery sub process. In the case of a child process termination, the wait call will allow the system to release the resources associated with the child process. If you do not perform a wait, the aborted subprocess will remain in the "zombie" state.

If the child process is found to have changed state, the calls are returned immediately. Conversely, the call is blocked until the child process state changes or is interrupted by the signal processing handle (if the system call is not restarted through the Sigaction Sa_restart flag).

The wait system call suspends the process in the current execution until one of its child processes terminates. Waitpid suspends execution of the current process until the specified child process state changes. By default, Waitpid only waits for the aborted state's subprocess, but this behavior can be changed by an option. Waitid system calls provide more granular control over which child process state changes are waiting for.

The child process has been terminated, and the parent process has not yet performed a wait operation on it, and the child process is transferred to the zombie state. A process in which the kernel has a "zombie" state retains the least amount of information (process identity, termination status, Resource usage information), and then the parent process can get child process information when it executes wait. As long as the zombie process is not removed from the system by a wait, it will occupy a field in the kernel process table. If the process table is filled, the kernel will no longer be able to generate new processes. If the parent process terminates, its zombie process is adopted by the INIT process and automatically executes the wait to remove them.

Wait (waits for child process to break or end)

#include <sys/types.h>      
           
#include <sys/wait.h>      
           
pid_t Wait (int * status);

Function description

, wait () temporarily stops execution of the current process (suspending the parent process) until a signal arrives or the child process ends. If the child process has ended when the wait () is invoked, wait () returns the child process end state value immediately. The end state value of the subprocess is returned by the parameter status, and the process identifier for the child process is returned as soon as it is. If you do not want to end the state value, the parameter status can be set to NULL. If the process that calls wait does not have a subprocess, the call fails, and the end status value of the child process is referred to Waitpid ()

Returns a subprocess identifier (PID) if the execution succeeds, or returns 1 if an error occurs. The reason for the failure exists in errno.

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.