Process-(4)

Source: Internet
Author: User

Wait (), waitpid () function

Brief introduction
1) When a process is normal or abnormally terminated, the kernel sends a SIGCHLD signal to its parent process. The parent process can ignore the signal, or provide a handler for the signal. By default, this signal is ignored by the system
2) If the parent process calls wait or waitpid
If its child processes are still running, block
If a child process is terminated and is waiting for the parent process to get its terminating state, the terminating state of the child process is returned immediately
If it does not have any child processes, return immediately


The difference between two functions


Wait causes its callers to block before a child process terminates, and waitpid many choices
If a child process is terminated and is a zombie process, wait returns immediately and gets the state of the child process, otherwise blocking.

Wait () and waitpid () allow the parent process to wait for the child process to end, effect: The parent process is blocked, and the child process runs until the child process ends.
Wait () waits in a single, waitpid () wait mode with more options.

Wait () function:

pid_t Wait (int* status)
Waits for any child process to end, status stores information about the end child process (exit code, whether it exits gracefully, etc.), and the return value is the PID of the end child process. Causes the parent process to block until a child process has ended (including the zombie subprocess).
That is, the wait () function, the parameter is the exit code, the return value is the end of the child process pid;wait () can reclaim the zombie subprocess;
Macro function: wifexited (status) can determine whether the normal exit, if the normal exit returns True, and Wexitstatus (status) to take the exit code (exit value).

Waitpid () function


If a process has several child processes, wait for the specified process to terminate, using the Waitpid
pid_t waitpid (pid_t pid,int* status,int option)
Parameters: PID Parameters
==-1, waiting for any child process, equivalent to wait
>0 waiting for a child process to be specified
==0 waits for any child process whose group ID is equal to the calling process group ID
<-1 waits for any sub-process whose group ID equals PID absolute value
option can specify whether to wait by blocking mode, and Wnohang is equivalent to waiting in a non-blocking manner.
Back: Blocking mode: Returns the PID of the end subprocess, which fails back to 1.
Wnohang mode: Returns the PID of the end subprocess, returns 0 if no child process has ended, and returns 1.


Example: Call of the Wait () function
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

int main ()
{
pid_t pid = fork ();
if (PID = = 0) {//Sub-process branch
printf ("Child process%d starts running \ n", Getpid ());
Sleep (3);
printf ("Child process end run \ n");
Exit (100); }
printf ("Parent process waits for the end of the child process \ n");
int status;
pid_t wpid = Wait (&status);
printf ("Wait for the end \ n");
printf ("wpid=%d\n", wpid);
if (wifexited (status))
printf ("Child process normal end, exit code:%d\n",
Wexitstatus (status));
}

The results are as follows:
Parent process waits for end of child process
Child process 20304 starts running
Child process End Run
Wait for the end
wpid=20304
Child process normal end, exit code: 100

The above results show that: because do not know the parent-child process, the first start, so, in the child process, need to sleep, let the child process and so on;
Then, to see the effect of the parent process;
Also, the child process has a call function exit, then he will be called function, will automatically end the child process, so that the subsequent only run the parent process, view more clearly;

Example 2: Start showing the effect of waitpid ()
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main () {
pid_t Pid1,pid2;
PID1 = fork ();
if (Pid1 > 0) pid2 = fork ();//Only the parent process can execute
if (Pid1 = = 0) {//Sub-process one branch
printf ("Sub-process%d starts running \ n", Getpid ());
Sleep (3); printf ("End of child process \ n");
Exit (100); }
if (Pid2 = = 0) {//Sub-process two branch
printf ("Sub-process two%d starts \ n", Getpid ());
Sleep (1); printf ("Child process two end \ n");
Exit (200); }
int status;
Waitpid (pid1,&status,0);//Blocking wait
if (wifexited (status)) printf ("exit=%d\n",
Wexitstatus (status);//Check vfork () manual and write test code
}

[Email protected] day07]# VI waitpid3.c
[Email protected] day07]# gcc waitpid3.c-o waitpid3
[Email protected] day07]#./waitpid3
Child process 123,175 starts running
Child process 223,173 starts running
Child process two end
The end of a child process
exit=100

Example 4:
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main () {
pid_t Pid1,pid2;
PID1 = fork ();
if (Pid1 > 0) pid2 = fork ();//Only the parent process can execute
if (Pid1 = = 0) {//Sub-process one branch
printf ("Sub-process%d starts running \ n", Getpid ());
Sleep (3); printf ("End of child process \ n");
Exit (100); }
if (Pid2 = = 0) {//Sub-process two branch
printf ("Sub-process two%d starts \ n", Getpid ());
Sleep (1); printf ("Child process two end \ n");
Exit (200); }
int status;
Waitpid ( -1,&status,0);//Blocking wait
if (wifexited (status)) printf ("exit=%d\n",
Wexitstatus (status);//Check vfork () manual and write test code
}

[Email protected] day07]# VI waitpid3.c
[Email protected] day07]# gcc waitpid3.c-o waitpid3
[Email protected] day07]#./waitpid3
Child process 123,175 starts running
Child process 223,173 starts running
Child process two end
exit=100
The end of a child process
Note If you do not specify the PID parameter of Waitpid, give 1 as the parameter, it will cause the parent process to accept the end of any one child process PID end information, return;

Example 5:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main () {
pid_t Pid1,pid2;
PID1 = fork ();
if (Pid1 > 0) pid2 = fork ();//Only the parent process can execute
if (Pid1 = = 0) {//Sub-process one branch
printf ("Sub-process%d starts running \ n", Getpid ());
Sleep (3); printf ("End of child process \ n");
Exit (100); }
if (Pid2 = = 0) {//Sub-process two branch
printf ("Sub-process two%d starts \ n", Getpid ());
Sleep (1); printf ("Child process two end \ n");
Exit (200); }
int status;
Waitpid ( -1,&status,wnohang);//Blocking wait
if (wifexited (status)) printf ("exit=%d\n",
Wexitstatus (status);//Check vfork () manual and write test code
}


[Email protected] day07]# VI waitpid3.c
[Email protected] day07]# gcc waitpid3.c-o waitpid3
[Email protected] day07]#./waitpid4
Child process 123,175 starts running
Exit=0
Child process 223,173 starts running
Child process two end
The end of a child process

If the option parameter of Waitpid () is used when using Wnohang, if the child process does not return immediately, the corresponding code information, then directly past, returns 0;

Process-(4)

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.