Zombie Processes
A zombie process refers to a process that has completed execution but still retains an entry in the process table. Such a process has finished its work and exited normally, but its parent process has not called wait() or waitpid() to retrieve the child process's termination status, so it still occupies a slot in the process table. Processes in this state are termed "zombie" processes.
Example
For example, in a Unix system, when a child process completes its task, it sends a SIGCHLD signal to the parent process. If the parent process does not handle this signal correctly (typically by calling wait() to read the child process's exit status), the child process's process descriptor and related resources are not fully released, resulting in a zombie process. If numerous zombie processes exist in the system, they may exhaust system resources and degrade performance.
Orphan Processes
An orphan process is one where the parent process has ended or exited abnormally, while the child process continues running. These orphan processes are adopted by the init process (the process with PID 1) and become its child processes. The init process periodically calls wait() to clean up terminated child processes, ensuring no zombie processes remain.
Example
Suppose a parent process creates a child process, and then the parent process terminates for some reason (e.g., due to an exception or abnormal exit). At this point, the child process continues running but has no parent process, so it becomes an orphan process. Due to Unix system design, the init process automatically becomes the new parent of this orphan process and handles its exit status.
Summary
Overall, zombie processes and orphan processes represent two distinct process states closely tied to their lifecycle and system resource management. System administrators and programmers must manage these processes properly to avoid wasting or exhausting system resources.