The need to wait

The fork-exec is really interesting, but hang on a second: While the child process performs an exec on the user command, and the successor is running (indicated by the dot-dash blue line in the preceding diagram), what should the parent process do? Obviously, it should wait, but for how long? Should we have it sleep? Well, no, as sleep takes the number of seconds to sleep as its argument. We do not know in advance how long the successor will take (it could be milliseconds, it could be months). The correct thing to do is this: have the parent process wait until the child (now the successor) dies.

This is precisely what the wait(2) API is designed to do. When the parent process issues the wait(2) API, it is put to sleep; the ...

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.