Explain the wait () function and Waitpid () function _c language in C language

Source: Internet
Author: User

C-language Wait () function: End (break) process function (Common)
header file:

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

To define a function:

pid_t Wait (int * status);

Function Description: Wait () will temporarily stop the execution of the current 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. The end status value of the child process is referred to Waitpid ().

Return value: Returns the child process identification Number (PID) If the execution succeeds, or returns 1 if an error occurs. The reason for the failure exists in errno.

Example

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ wait.h>
Main ()
{
  pid_t pid;
  int status, I;
  if (fork () = = 0)
  {
    printf ("This are the child process.") PID =%d\n ", Getpid ());
    Exit (5);
   }
  else
  {sleep
    (1);
    printf ("This are the parent process, wait for child...\n";
    PID = Wait (&status);
    i = wexitstatus (status);
    printf ("Child ' s PID =%d.) Exit status=^d\n ", PID, I);
  }
}

Perform:

This is the child process. pid=1501 This
are the parent process, wait for child ...
Child ' s PID =1501, exit status =5

C language Waitpid () function: interrupt (end) process function (or wait child process interrupt)
header file:

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

To define a function:

pid_t waitpid (pid_t pid, int * status, int options);

Function Description: Waitpid () Temporarily stops the execution of the current 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. Parameter PID is the child process identifier to wait for, and other numerical meanings are as follows:

1, pid<-1 waiting for process Group identification code for the PID absolute value of any child processes.
2, Pid=-1 waiting for any child process, equivalent to wait ().
3. Pid=0 waits for Process group identification code to be the same as any subprocess of the current process.
4, pid>0 waiting for any child process recognition code for the PID of the child process.

Parameter option can be a combination of 0 or below:

Wnohang: If there are no child processes that are already closed, return immediately and do not wait.
Wuntraced: If the child process enters a suspend execution, it returns immediately, but the end state is ignored. The end state of the subprocess is returned and stored in status, with several macros below to determine the end condition
wifexited (status): A value other than 0 if the child process ends normally.
Wexitstatus (status): Gets the end code returned by the subprocess exit (), typically using the wifexited to determine whether the macro will be used before it ends normally.
Wifsignaled (status): This macro value is true if the child process ends because of a signal
Wtermsig (status): The signal to the child process due to the signal to abort the code, the general will use the wifsignaled to judge before using this macro.
wifstopped (status): This macro value is true if the child process is in a paused execution. This is generally the case only if you are using wuntraced.
Wstopsig (status): Obtains the signal code that raises the child process pause, generally uses the wifstopped to judge before using this macro.

Return value: Returns the child process identification Number (PID) If the execution succeeds, or returns 1 if an error occurs. The reason for the failure exists in errno.

Example: Reference wait ().

Related Article

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.