Zombie Processes
Zombie, or defunct, processes are processes that have finished execution but still linger in the process table. They occur when a parent process fails to read the child's exit status.
Example
For instance, consider the following C code:
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
pid_t child_pid;
child_pid = fork();
if(child_pid > 0){
sleep(60); // Parent process sleeps for 60 seconds
}
else{
exit(0); // Child process exits immediately
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this code, the child process exits before the parent, becoming a Zombie process for the duration of the parent's sleep time.
Orphan Processes
Orphan processes are those whose parent process has finished or terminated, leaving the child process still running. The OS assigns these Orphan processes to the init process, which periodically performs a wait operation to eliminate any zombie processes.
Example
Consider the following C code:
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
pid_t child_pid;
child_pid = fork();
if(child_pid > 0){
exit(0); // Parent process exits immediately
}
else{
sleep(60); // Child process sleeps for 60 seconds
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this case, the parent process exits immediately after the fork, while the child process continues execution, becoming an Orphan process.
Also see, Difference Between Bit and Byte, Demand Paging in OS
Frequently Asked Questions
What is a Zombie process in an OS?
A Zombie process is a process that has completed execution but still remains in the process table because the parent process didn't read its exit status.
What is an Orphan process?
An Orphan process is a running process whose parent process has finished or terminated.
How does an OS handle these processes?
The OS reassigns Orphan processes to the init process and periodically removes Zombie processes from the process table.
Conclusion
Understanding Zombie and Orphan processes is crucial in system administration and process management. While these processes are generally harmless, excessive numbers could indicate an issue and may consume system resources unnecessarily. Hence, monitoring and maintaining healthy process relationships in any OS is paramount for smooth performance.
Recommended Article: fork() system call
Difference Between List and Set