Are you there?

Checking for a process's very existence, is it alive now?, can be crucial to an application. For example, an application function receives the PID of a process as a parameter. Before it actually does something with the process via the provided PID (perhaps send it a signal), it would be a good idea to verify that the process is indeed valid (what if it's dead or the PID invalid?).

The kill(2) system call helps us in this regard: the second parameter to kill is the signal to send; using the value 0 (recall there is no signal numbered 0) validates the first parameter: the PID. How exactly? If the kill(2) returns failure, either the PID is invalid or we do not have permission to send the process (or process group) a signal.

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.