Linux process waits for wait, waitpid

Source: Internet
Author: User
Tags function prototype

Waitpid () is similar to the Wait () feature, where the user master process waits for the child process to end or break. Can be used for synchronization between processes

Wait function prototype

wait(int*status);

function Description
Wait () temporarily stops the process from running until a signal arrives or the child process ends. Assuming that the child process has ended when the wait () is called, Wait () returns the child process end status value immediately. The end state value of the child process is returned by the parameter status. The process identifier of the child process is also returned. Assuming that the end-of-state value is not the case, the parameter ststus can be set to NULL. The end status value of the child process please refer to the following waitpid ().

return value
Assuming a successful run, the child process identifier (PID) is returned, assuming that there is an error that returns-1, and the reason for the failure is stored in errno.

Demo Sample code:

#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h>intMainintARGC, Char*ARGV[]) {pid_t pid;intstatus, I;if(Fork() ==0) {printf("This was the child process pid = %d\ n", Getpid ());Exit(5); }Else{Sleep(1);printf("This is the parent process, and wait for child...\n"); PID =wait(&status); i = wexitstatus (status);printf("Child ' s PID = %d. Exit status = %d\ n ", PID, i); }return 0;}

Waitpid function prototypes

waitpidint*statusint options);

function Description
Waitpid () Temporarily stops the process from running until a signal arrives or the child process ends. Assuming that the child process has ended when calling Waitpid (), Waitpid () returns the child process end status value immediately. The end state value of the child process is returned by the parameter status, and the process identifier of the child process is returned. Assuming the end-of-state value is absent, the parameter ststus can be set to NULL. The parameter PID is the child process identification code to wait for. Its numerical significance is as follows:

0 时,仅仅等待进程id等于pid的子进程,无论其他已经有多少子进程运行结束退出,仅仅要指定的子进程还没有结束,waitpid就会一直等下去.pid = -1waitpidwait0 时,等待统一进程组中的不论什么子进程,假设子进程已经增加了别的进程组,waitpid 不会对它做不论什么理睬.pid < -1 时, 等待一个指定进程组中的不论什么子进程,这个进程组的ID等于pid的绝对值。

The parameters options have the following types of values:

WNOHANG 假设没有不论什么已经结束的子进程则立即返回, 不予以等待。  WUNTRACED 假设子进程进入暂停运行情况则立即返回,但结束状态不予以理会。假设不用以上两个宏。还能够用 0 作为第三个參数传入。

Note: the wait () function is the wrapped waitpid (), view < kernel source code folder >/include/unistd.h file to see such as the following program segment

wait(int*wait_stat){    returnwaitpid(-1,wait_stat,0);}

return value
WAITPID returns the ID of the child process that was collected when normal return;
Assuming that Wnohang is set, and the call Waitpid discovers that no child process that has exited can be collected, it returns 0;
If there is an error in the call, return-1 and reset the value of errno.

after the end of the child process is returned and stored in status, there are several macros underground to determine the end of the situation
Wnohang assumes that no matter what has ended, the child process returns immediately, not waiting.

  

Wuntraced assumes that the child process enters a paused operation and returns immediately, but the end state is ignored.

  

The end state of the child process is returned and stored in status, with a few macros at the bottom to determine the end condition:

Wifexited (status) assumes that the child process normally ends with a value other than 0.

Wexitstatus (status) Gets the end code returned by the child process exit (), usually preceded by the

Wifexited to infer whether the ability to end gracefully uses this macro.

Wifsignaled (status) assumes that this macro value is true when the child process is terminated by a signal

Wtermsig (status) Gets the signal code that the child process has aborted because of the signal, and usually uses wifsignaled to infer it before using the macro.

wifstopped (status) assumes that the child process is in a paused state when the macro value is true.

This is usually only the case when using wuntraced.

Wstopsig (status) obtains the signal code that causes the child process to pause, usually using wifstopped to infer the macro before use.

Demo Sample code:

#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>Main () {pid_t pc, PR; Pc=Fork();if(pc<0)printf("Error occured on forking.\n");Else if(pc==0) {Sleep(Ten);Exit(0); } Do{pr=Waitpid(PC, NULL, Wnohang);if(pr==0) {printf("No Child exited\n");Sleep(1); }    } while(pr==0);if(PR==PC)printf("Successfully get child %d\ n", PR);Else        printf("Some error occured\n");}

Linux process waits for wait, waitpid

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.