Process Termination: exit() and Return Codes
You'll learn: how a running process ends its execution and signals whether it succeeded or failed to its parent.
How Processes Say Goodbye
Every process must eventually terminate—ending its execution and releasing all its resources back to the operating system. The primary way a process ends normally is by calling the exit() system call (or by reaching the end of its main function, which implicitly calls exit()).
When a process terminates, it doesn't just disappear silently. It communicates a return code (also called an exit status) to its parent process. This is a small integer value that acts like a final report card:
- 0 typically means "success—everything went fine"
- Non-zero values indicate some kind of error or abnormal condition
Think of it like leaving a classroom: you don't just vanish—you turn in your assignment (the return code) so the teacher (parent process) knows how you did.
Why Return Codes Matter
The parent process can use the wait() family of system calls to retrieve this exit status and decide what to do next. For example, if a parent launches a child to perform a task and the child returns 0, the parent knows to continue. If the child returns an error code, the parent might retry, log the failure, or terminate itself.
This parent-child communication is fundamental to how complex programs coordinate multiple processes, especially in shell scripts and system utilities.
Key Takeaway:
Processes terminate via exit(), passing a return code (0 = success, non-zero = error) to their parent, enabling inter-process status communication and coordination.