linux c wait on the process code

Here is information about the content linux c on the process of waiting, hoping to have a greater benefit to the farming code.

#include <sys/types.h>
#include <stdio.h>
#include <sys/wait.h>

void check_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 exitn");
    exit(0);
    }
    else
    {
        if(wait(&status) != pid)
        {
        printf("wait error!!");
        exit(0);
        }
        check_exit(status);
    }

}
void check_exit(int status)
{
    if(WIFEXITED(status))
        printf("eixtn");

    else if(WIFSIGNALED(status))
        printf("killed by signaln");
    else if(WIFSTOPPED(status))
        printf("stopped by signaln");
    else if(WIFCONTINUED(status))
        printf("continued");
}

Wait for the process to change its state. All of the following calls which are used to change the state of wait for the child, the child process to obtain information about the status has changed. Status change can be considered: 1. The child process has been terminated. 2. The signal causes the child process to stop executing. 3. The signal recovery sub-process execution. In the case of child process termination, wait call will allow the system to release resources associated with a child process. If you do not execute wait, terminated the child process will remain in the "zombie" state. If you find a child process to change state, these calls will return immediately. On the contrary, the call will be blocked until the child process to change state, or by a signal handler interrupted (restart if the system call sigaction no sign SA_RESTART through). wait system call suspends execution of the current process until a child process to terminate it. waitpid suspends the current process of execution until the specified child process status changes. By default, waitpid only wait for the child to terminate the state, but this behavior can be changed by the option. waitid system calls for the state to which the child waiting for change provides more precise control. Child process has been terminated, the parent had not yet wait to perform the operation, the child will be transferred to the "dead" state. Kernel process "dead" state to retain a minimum amount of information (process ID, termination status, resource usage information), you can get information after a child process parent process wait. As long as the process does not remove the dead from the system through the wait, it will occupy a field kernel process table. If the process table is full, the kernel will not be able to generate new process. If the parent process has been terminated, the process by which the dead child init process adoption, and automates wait them removed. wait (wait for the child interrupt or end)

#include<sys/types.h>

#include<sys/wait.h>

Function Description wait () will temporarily stop the implementation of the current process (parent process hangs), or until the signal came to the end of the child process. If the child process has ended when calling wait (), then wait () will immediately return the child process end status value. Ending state value is returned by the child process parameter status, while the child process identification code will be a quick return. If the end state do not care value, the status parameter can be set to NULL. If the calling process wait no child process will be called a failure, ending the state of the child process values, refer to waitpid () is executed successfully if the child process ID (PID) is returned if an error occurs -1 is returned. The reason for the failure is stored in errno.

Guess you like

Origin blog.51cto.com/14397541/2408505