Help me correct my confusion, I have a job running in bg with pid = 3
I want to check if it has finished every 10 sec (non blocking) and print hello once and stop;
But someone claimed the following:
this won't work as status can be consumed only once.
Even if you stopped after first run, your check isn't right. you should check returned pid_t value and status too.
and he added:
just remember that waitpid is waiting for state change, and if a previous call has already consumed this change, the second call will block for other changes
I am just confused with this status a lot! can someone help me understand, I don't find any good and clear reference online.
I think your code should work.
status
argument, so that doesn't matter.status
if you need to distinguish different ways of the process ending, such as by a signal versus normal exit. For instance you can use WIFSIGNALED(status)
to tell if it exited due to a signal, and you can use WEXITSTATUS(status)
to get the value that it passed to exit()
.The last comment only matters if you have some other wait()
or waitpid()
call that might detect the process exiting. If this is the only one, you should be fine.
If the code is in a loop, put break
after you print Hello
to stop checking.
It would be good to check if return_pid == -1
, which indicates an error, and stop the loop. If somehow the status was received by some other wait call, you'll get an error and this will keep you from retrying forever.