Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Would WIFEXITED Return True on Running Process?

Tags:

c

system-calls

When I wait on a specific running process group that is a child process, WIFEXITED returns true saying the process exited? Is this the way it works? Seems there is something I am not understanding....

if ( waitpid(-pgid, &pstatus, WUNTRACED|WNOHANG ) == -1)
    perror("Wait error");

if ( WIFEXITED(pstatus) ) {
    strncpy(buf,  "Exited", buf_size);
    return 0;
like image 958
Kyle Brandt Avatar asked Oct 29 '09 11:10

Kyle Brandt


People also ask

What is Wifexited status?

WIFEXITED and WEXITSTATUS are two of the options which can be used to know the exit status of the child. WIFEXITED(status) : returns true if the child terminated normally. WEXITSTATUS(status) : returns the exit status of the child.

What does Waitpid return in C?

Returned value If successful, waitpid() returns a value of the process (usually a child) whose status information has been obtained. If WNOHANG was given, and if there is at least one process (usually a child) whose status information is not available, waitpid() returns 0.

What does Waitpid 1 mean?

From the linux manual : The pid parameter specifies the set of child processes for which to wait. If pid is -1, the call waits for any child process.

How does Waitpid work?

It blocks the calling process until a nominated child process exits (or makes some other transition such as being stopped.) Typically you will use waitpid rather than generic wait when you may have more than one process and only care about one.


2 Answers

As you specified WNOHANG I think waitpid is returning 0 and pstatus has the value it had before so WIFEXITED is not working with updated data.

if WNOHANG was specified and one or more child(ren) specified by pid exist, but have not yet changed state, then 0 is returned.

like image 107
Arkaitz Jimenez Avatar answered Sep 28 '22 03:09

Arkaitz Jimenez


waitpid returns the reaped child pid if it successfully reaps a child. When used with WNOHANG, it immediately returns 0 if no children have exited. Thus, you need to check whether the return value is 0 or a pid before you inspect status. See here for details:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/waitpid.html

like image 21
R.. GitHub STOP HELPING ICE Avatar answered Sep 28 '22 02:09

R.. GitHub STOP HELPING ICE