wait, waitpid of learning to use

the wait man
man waitpid


from clear

Function prototype

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

Function Description

After the parent process creates a child process, the ability to use the wait and waitpid with a child process of monitoring the operating state of the parent process. These two functions are used to wait for state child process changes callback and access to information state changes, the state could acquire changes include: child process to finish, the child process is suspended semaphore, child process will return to operate the semaphore.

After the parent process wait function, the parent process will be blocked here, if the child process status changes, the wait function returns the result immediately; otherwise wait function blocks until the child process status changes.

In the usual sense, that if the child process state has changed, but not its parent or other system or callback function execution wait, this time the child is referred to as the wait.

After the parent process child process to run wait function can promote the release system resources associated with the child; otherwise the child in the state will be maintained at a zombie process (child process has ended, and the parent process is still running) has always existed.

Return Value Description

The above function return value -1, 0> 0 three cases. Back correspond to the following three cases.

-1: call error, then an error message in errno
0: If the waitpid of options set WNOHANG, and call no child process exits immediately return 0

0: If greater than 0, returns an exit process pid.

Parameter Description

pid: ID of the process to be monitored (<-. 1, = -1, = 0,> 0)
Status: storing a signal value change and start code state value (code) in the exit.
options to provide some additional options control waitpid, currently only supports linux and WUNTRACED WNOHANG two options, which are two constants macros, you can use the | connection.
pid Details:

pid <-1: listening range for the process of all sub-group -pid processes
pid = -1: listening range call wait / waitpid all sub-processes
pid = 0: listening range of the sub-process group ID (the parent process equal)
pid> 0: pid monitor specific process

status detail

status for storing a signal value starting wait time or exit code values ​​Exit (code)

options detailed

WNOHANG: Use this parameter to call waitpid, even if the child process does not quit, he would return immediately, rather than wait wait forever
WUNTRACED: for debugging, rarely with
the use of a value of 0 to under normal circumstances.

wait and waitpid relationship

substantially in wait waitpid pid = -1, options = 0 the sealing facilities i.e.

wait (& status) and waitpid (-1, & status, 0) is identical, which is the most essential functions and relationships waitpid the wait.

Function Call Example:
#include <SYS / types.h>

#include <sys/wait.h>

#include <unistd.h>

#include <stdlib.h>

main()

{

    pid_t pc,pr;
int a;

    pc=fork();



    if(pc<0) 

           printf("error ocurred!/n");

   else if(pc==0){ 

           printf("This is child process with pid of %d\n",getpid());



        }

    else{ 
    printf("this is parent process\n");

           pr=wait(&a); 

          printf("I catched a child process with pid of %d\n",pr);

    }

   exit(0);

}

Execution can be found, the parent process to wait when the blocking function, then the system performs the child to return after the end of the process by pr process number, as well as a pointer to an int a, the situation can view the process.

Because the wait function Essentially, the package waitpid given reference function, to achieve the same internal

Guess you like

Origin www.cnblogs.com/besti-20199303/p/11829673.html