The difference between wait and waitpid in "Turn" Linux

Source: Internet
Author: User
Tags define function



Original URL: http://blog.163.com/libo_5/blog/static/15696852010324287748/


Zombie does not consume memory or CPU, and on the surface we can not care about their existence, but in fact the UNIX system limits the maximum number of processes that can exist at a time. If the program does not clean up the zombie in the system in time, it will eventually result in too many processes, and when a new process needs to be generated again, an error occurs.
For the reasons above, we need to call the wait or waipid in the parent input after the child process calls exit
#include <sys/types.h>
#include <sys/wait.h>
pid_t Wait (int &statloc);
pid_t waitpid (pid_t pid,int *statloc, int options);
Both return:process ID if ok,-1 on Error
They are called by the parent process to get the child process end information, clear the zombie. When the parent is called in to call these two functions
A block (if its child process is still running)
C Return Error (if no child process exists)

the difference between the two functions is that wait causes the caller to block until a child process terminates and waitpid can be set to non-blocking by setting an option, and waitpid is not waiting for the first end of the process but waiting for the PID-specified process in the parameter.
The variable statloc in two functions is a pointer to data of type int. If this variable is not NULL, then the termination status of the end process is saved in the area of memory pointed to by Statiloc, and if we do not care about termination status, Statloc can be set to null.
The traditional implementation of these two functions returns an integer in which the specific bit is given a specific meaning. POSIX.1 specifies some of the files included in the header <sys/wait.h> macro to view these termination status
Macro Description
wifexited (status) If the status is generated by a normally ended process, the value is true, at which point we can continue to use the macro Wexit Status to get the arguments for exit or _exit
wifsignaled (status) If the status is generated by a process that ends with an exception (received to a signal), the value is true and the macro WT is used at this time Ermsig (status) to get the number of signals.
wifstopped (status) If the status is generated by a process that receives a signal that is currently terminated, the value is true and you can continue calling the macro ws Topsig (status) to see which signal caused the process to terminate.

Option constants for Waitpid
Wnohang Waitpid will not block if the specified PID does not end
wuntraced If a child process enters a paused execution, it returns immediately, but the end state is ignored.
The meaning of PID in Waitpid is changed according to its specific value.
Pid==-1 waits for any child process, at which time the Waitpid function is the same as the wait
PID >0 waiting for the process ID to be the same as the PID value of the child process
Pid==0 waits for any child process that is the same as the caller's process group ID
Pid<-1 any sub-process that waits for the process group ID to be equal to the PID absolute value

The Waitpid provides three features that wait does not have:
1 Waitpid allows us to wait for the specified process
2 Waitpid provides a non-blocking wait
3 Waitpid Support Work control
You can view Apue page202





===========================================================================



Once the process has called wait, it immediately blocks itself, and the wait automatically parses if a child process of the current process has exited, and if it finds such a child process that has become a zombie, wait will collect information about the child process and destroy it and return it, if no such child process is found. Wait will always be stuck here until one appears.


Wait (waits for the child process to break or end)
Correlation function Waitpid,fork
Table header File
#include <sys/types.h>
#include <sys/wait.h>
Defines the function pid_t wait (int * status);
Function description
Wait () temporarily stops execution of the current process until a signal arrives or the child processes a knot
Beam. If the child process has ended when you call Wait (), then wait () returns immediately
Returns the child process end status value. The end state value of the child process is returned by the parameter status,
The process identifier of the child process is also returned quickly. If the end state value is not
The parameter status can be set to null. Please refer to Waitpid () for the end status value of the child process.
return value
Returns the child process identifier (PID) if the execution succeeds, or returns if an error occurs
-1. The reason for failure is stored in errno.
Additional Instructions
Example One
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main ()
{
pid_t pid;
int status,i;
if (fork () = =0) {
printf ("This was the child process. PID =%d\n", Getpid ());
Exit (5);
}else{
Sleep (1);
printf ("This was 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
The child process.pid=1501
The the parent process. Wait for a child ...
Child ' s PID =1501,exit status =5


Example Two


#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
using namespace Std;
int main (void)
{
pid_t pid;
PID =fork ();
if (pid<0)
Exit (0);
else if (PID = = 0)
{
If the child process sleeps for 20 seconds
cout<< "Children:" <<getpid () <<endl;
Sleep (20);
}
Else
{cout<< "hello! i ' m parent process!" <<endl;
If it is the parent process waiting here
pid_t PR = wait (NULL);
cout<<pr<<endl;
}
return 0;
}
Waitpid (Waiting for the child process to break or end)
Correlation function Wait,fork
Table header File
#include <sys/types.h>
#include <sys/wait.h>
Define function pid_t waitpid (pid_t pid,int * status,int options);
Function description
Waitpid () will temporarily stop the execution of the current process until a signal arrives or a child process
End. If the child process has ended when calling Waitpid (), then Waitpid () will immediately
Returns the child process end status value. The end state value of the child process is returned by the parameter status,
The process identifier of the child process is also returned quickly. If the end state value is not
The parameter status can be set to null. Parameter PID is to wait for the child process identification Code,
Other numerical meanings are as follows:
Pid<-1 waits for the process group identifier to be the PID absolute value of any child processes.
Pid=-1 waits for any child process, equivalent to wait ().
Pid=0 waits for the process group identifier to be the same as the current process for any child processes.
Pid>0 waits for any child process identification code to be a child of PID.
The parameter option can be 0 or the following or combination:
Wnohang If there are no completed sub-processes then return immediately, do not
Wait.
Wuntraced returns immediately if the child process enters a paused execution, but ends
State is disregarded.
The end state of the child process is returned and stored in status, with several macros at the bottom to determine the end
Condition:
Wifexited (status) is a non-0 value if the child process ends normally.
Wexitstatus (status) Gets the end code returned by the child process exit (), a
The wifexited will be used to determine if the normal end is complete before using this macro.
wifsignaled (status) If the child process ends because of a signal, this macro value is
Really
Wtermsig (status) Gets the signal code that the child process aborts due to the signal, generally
This macro is used only after the wifsignaled is used to determine the first.
wifstopped (status) If the child process is in a paused execution, this macro value is
Really. This is generally the case only if you are using wuntraced.
Wstopsig (status) Gets the signal code that causes the child process to pause, which is usually first
Use this macro only after you have used wifstopped to judge.
return value
Returns the child process identifier (PID) if the execution succeeds, or returns if an error occurs
-1. The reason for failure is stored in errno.
Example
Refer to Wait ().








The difference between wait and waitpid in "Turn" Linux


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.