user15687371
user15687371

Reputation:

C waitpid, is this a contradiction?

In my previous question: How to check if a process has been finished?

you showed me how to check if a child process is finished or not yet, the code was something like this:

pid_t r = waitpid(pid, &status, WNOHANG)//if r==pid then process finished

what if I want to wait for the process to finish (supposing it's still running)?

I read in the internet that the solution is the same, ie:

pid_t r = waitpid(pid, &status, WNOHANG)

does that mean we have same command for 2 different jobs? that doesn't make sense to me.

Upvotes: 0

Views: 150

Answers (1)

ikegami
ikegami

Reputation: 385565

I don't know where you read that waitpid(pid, &status, WNOHANG) blocks, but this is wrong. To block until the process ends*, use

waitpid(pid, &status, 0)

From the man page for waitpid:

All of these system calls are used to wait for state changes in a child of the calling process

WNOHANG overrides this.

WNOHANG return immediately if no child has exited.


* If you have signal handlers, it can also return (with error EINTR) when interrupted by a signal.

Upvotes: 1

Related Questions