in Programming in C
9,770 views
2 votes
2 votes
main()
{
if(fork()>0)
sleep(100);
}

The given code results in the creation of:
I) an orphan process
II) a zombie process
III)  a process that executes forever
IV) None of these

 Can someone explain this?
in Programming in C
9.8k views

3 Comments

Can you explain in your words? I tried those links but not able to get it
0
0

zombie process or defunct process is a process that has completed execution but still has an entry in the process table as its parent process didn't invoke $wait()$ system call.

0
0

2 Answers

12 votes
12 votes
Best answer

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:

selected by

3 Comments

Awesome!
0
0

@Akhilesh Singla

$+1$ Nicely explained with the words.

0
0

Add yu a little bit,

For termination of Orphan process, yu don't need to remove it from the system 

Instead, OS like  LINUX run primary processes like " init " which 're secondarly responsible for adopting Orphan process, inorder to terminate them.

 

 

0
0
1 vote
1 vote

Zombie Process.

Zombie Process -  
A process which has finished the execution but still has an entry in the process table to report to its parent process is known as a zombie process.

Orphan Process-
A process whose parent process no more exists i.e. either finished or terminated without waiting for its child process to terminate is called an orphan process.

Now in the above code, After creating a child process (by fork()) parent process sleeps for 100ms hence doesn’t call wait() .
After termination of the child process, its entry still remains there in the process table. So it becomes a zombie process.

1 comment

Thanks :)
0
0

Related questions