Detail the wait of function and waitpid of function in C language

  • 2020-04-02 03:18:34
  • OfStack

C language wait() function: end (interrupt) process function (common)
The header file:


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

Definition function:


pid_t wait (int * status);

Function description: the wait () will temporarily stop the current process of implementation, until the end of the signal to or child processes. If the calling wait () the child process has ended, the wait () will immediately return the child end state values. The child to the end of the status value will return by parameter status, and the child process identification number will also be a quick return. If you don't care end state value, status, parameter can be set to NULL. The child to the end of the status value refer to waitpid ().

Return value: returns the child process identifier (PID) on success and -1 if an error occurs. The reason for the failure is in errno.

sample


#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 is the child process. pid =%dn", getpid());
    exit(5);
   }
  else
  {
    sleep(1);
    printf("This is the parent process, wait for child...n";
    pid = wait(&status);
    i = WEXITSTATUS(status);
    printf("child's pid =%d . exit status=^dn", pid, i);
  }
}

Perform:


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

C waitpid() function: interrupt (end) process function (or wait for child process interrupt)
The header file:


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

Definition function:


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

Function description: waitpid () will temporarily stop the current process of implementation, until the end of the signal to or child processes. If the calling wait () the child process has ended, the wait () will immediately return the child end state values. The child to the end of the status value will return by parameter status, and the child process identification number will also be a quick return. If you don't care end state value, status, parameter can be set to NULL. The parameters of pid is to wait for the child process identification number, other numerical significance as follows:

1, the pid < -1 waits for any child process whose process group identifier is the pid absolute value.
2, pid=-1 wait for any child process, equivalent to wait().
3. Pid =0 wait for any child process whose group identifier is the same as the current process.
4, the pid > Wait for any child process whose id is pid.

The option can be 0 OR the following OR combination:

WNOHANG: if there are no completed child processes then return immediately without waiting.
WUNTRACED: returns immediately if a child process enters a pause, but the end state is ignored
WIFEXITED(status) : non-zero if the child process ends normally.
WEXITSTATUS(status) : gets the end code returned by the child process exit(). Usually, the end code is determined by WIFEXITED before using this macro.
WIFSIGNALED (status) : if the child is because of the signal and end this macro value is true
WTERMSIG (status) : get the child to due to signal to suspend the signal code, tend to use first judging WIFSIGNALED after using this macro.
WIFSTOPPED(status) : this macro value is true if the child process is in suspension. This is generally only possible with WUNTRACED.
WSTOPSIG(status) : gets the code that causes the child to pause, usually using WIFSTOPPED before using this macro.

Return value: returns the child process identifier (PID) on success and -1 if an error occurs. The reason for the failure is in errno.

Example: see wait().


Related articles: