Home > Back-end >  About waitpid function, the problem of process status
About waitpid function, the problem of process status

Time:09-22

Waitpid function following
 # include & lt; Sys/types. H> 
#include

pid_t waitpid(pid_t pid, int *status, int options);
Function:
Wait for the child process terminates, if the child process terminates, this function will be recycling the resources of the child process,

Parameters:
Pid, parameters of pid values has the following several types:
Pid & gt; 0 to wait for the process ID is equal to the pid of the child,
Pid=0 wait for any child process in the same process group, if the child has joined other process group, waitpid will not wait for it,
Pid=1 waiting for Ren Yizi process, waitpid and wait at this time,
Pid & lt; 1 wait for any child process in the specified process group, the process group ID is equal to the absolute value of pid,

Status: when the process exits state information, and wait () usage,

Options: the options provided some additional options to control the waitpid (),
0: with the wait (), blocking the parent process, wait for the child process exits,
WNOHANG: no child has come to an end, return immediately,
WUNTRACED: if the child has suspended, this function returns immediately, and not to ignore the child to the end of the state, (because involves some debug aspects knowledge, combined with the rarely used)

The return value:
Waitpid () returns a value than wait () a little more complicated, there are 3 kinds of circumstances:
1) when the normal return, waitpid () returns has collected the process of recycling the child;
2) if set the options WNOHANG, and call waitpid () found no have withdrawn from the child processes to wait for, it returns 0.
3) if the error in the call, it returns 1, then the errno is set to the corresponding value to indicate error, such as: when the pid of the child process does not exist, or the process, but not the calling process of the child, waitpid () will return error, then the errno is set to ECHILD;



Questions about WUNTRACED parameters, if the child has suspended the returns immediately,
question 1:
What is the status when the process was suspended, the ready state? , blocking state? , pending state?
Question 2:
Process in the I/O requests the executing state into a blocking state, the scanf function to calculate the I/O requests?
Question 3:
Waitpid under setting WUNTRACED parameters, the child process stops when the return value is what?
The test code is as follows:

 # include "is basic. H" 

Int main ()

{

Pid_t PC, pr;
Int the status;
Int the enter;
PC=fork ();

If (pc<0)/* if the fork error */
{

Perror (" Error occured on forking ");
}

Else if (0==PC)/* if it is the child */
{
Sleep (3);/* sleep 10 seconds */
Printf (" this is son process \ n ");
The fflush (stdout);
The scanf (" % d ", & amp; Enter);
Printf (" enter is % d \ n ", enter);
Printf (" to mean the process end ");
exit(0);
}

/* if it is the parent process */

Do {

Pr=waitpid (- (getpid ()), & amp; The status, WUNTRACED);

If (pr==0)/* if not collected from the child */
{

Perror (" No child exited ");

Sleep (1);

}
If (1==pr)
{
Perror (" waitpid fail ");
}

} while (pr==0);/* not collected from the child, go back to try */

If (pr==PC)
{
Printf (" successfully get the child % d \ n ", pr);
If (WIFEXITED (status)!=0)
{
Printf (" son process return: % d \ n ", WEXITSTATUS (status));
}
}
The else
{
Printf (" some error occured \ n ");
}

}

Please bosses can help to improve implementation of WUNTRACED parameters testing,

Please bosses, thanks
  • Related