Below are different values returned by fork():
- Negative Value: creation of a child process was unsuccessful.
- Zero: Returned to the newly created child process.
- Positive value: Returned to parent or caller. The value contains process ID of newly created child process.
A parent process maintains an entry about its child process in its process table/PCB. When a child process terminates, it returns an exit status(a signal indicating its completion) to the operating system, which is then returned to the parent process. Thus, a signal is sent by OS to the parent process signaling that its child has been executed. The type of signal helps the parent process determine how its child process died, i.e., either normally or abnormally. After receiving such signal, the parent process removes the entry from its process table meaning that all traces of the child process has been removed from the system. On the basis of this:
Zombie process is a child process which has finished the execution but still has entry in the process table of its parent process. A child process always first becomes a zombie before being removed from the process table. This is because until the parent process does not remove the traces of the child process from its process table, the child process seems like a zombie to OS; it is dead(completed execution) but still lingering(traces in the PCB of the parent). The child process is no more a zombie process when its parent has successfully removed its traces(somehow) form its PCB.
Orphan process is a child process whose parent process no more exists, i.e., either finished or terminated without waiting for its child process to terminate. In other words, the parent process doesn't exist when its child process has finished execution. Now, the process is on its own. It means OS doesn't need to(but most probably it will) return exit status of the child process to the terminated parent process. So, simple termination of the orphan process removes it from the system.
Now coming to your code:
if(fork()>0) //greater than 0 means control is given back to parent
sleep(100); //parent process goes to sleep,i.e., inactive for 100 seconds
In these 100 seconds, the child process created by fork() has finished its execution and returned its success/failure to OS, which then sends a signal to the parent process. But since the parent process is asleep, it won't hear/get the signal from the OS, making the child process a zombie process.
Sources: