Implementing a non-blocking wait alternative to the wait system call. It handles terminated child processes asynchronously, prints their PID, exit status, and termination reason (signal or normal exit), and ensures that no zombie processes are created.
To compile the program, use the following command:
gcc -o non_blocking_wait non_blocking_wait.cRun the compiled program:
./non_blocking_wait-
Sets up a
SIGCHLDhandler:- The handler uses
waitpidwith theWNOHANGflag to reap terminated child processes without blocking the parent. - It prints the PID, exit status, and termination reason (signal or normal exit) for each child.
- The handler uses
-
Launches child processes:
- Three child processes are created using
fork. - Each child sleeps for a different duration and then exits:
- The first child raises a segmentation fault (
SIGSEGV) to simulate a signal-induced termination. - The other two children exit normally with different exit codes.
- The first child raises a segmentation fault (
- Three child processes are created using
-
Parent process continues execution:
- The parent process continues executing its main loop (
while (1) { sleep(1); }) while handling child terminations asynchronously.
- The parent process continues executing its main loop (
Parenting...
Child 11532 terminated by signal 11 (Segmentation fault)
Child 11533 terminated normally with exit status 11
Child 11534 terminated normally with exit status 12- Parenting...: The parent process continues executing its main loop after launching the child processes.
- Child 11532 terminated by signal 11 (Segmentation fault): The first child process was terminated by a segmentation fault (signal 11,
SIGSEGV). - Child 11533 terminated normally with exit status 11: The second child process exited normally with an exit status of 11.
- Child 11534 terminated normally with exit status 12: The third child process exited normally with an exit status of 12.
- Non-blocking wait: Uses
WNOHANGto avoid blocking the parent process. - Zombie prevention: All terminated children are reaped promptly.
- Async-signal safety: Uses
writefor printing in the signal handler to avoid unsafe functions. - Race condition avoidance: Blocks
SIGCHLDduring setup to prevent missed signals.
- A C compiler (eg.
gcc). - A POSIX-compliant operating system (Linux, macOS, etc..).