Hello and welcome!
In this video session we will see how of orphan process a zombie process and a sleeping process are
created.
Let us see this with an example of a program.
In this program we include the required header files and in the main we create a process.
The for creates a process.
So the main process the, which is called as the parent process, has forged a child process.
And if a error has occurred during the forking we exit the code.
And in this case this part of the code is the parent process and the else is the child process.
So if fork returns greater than zero, then it will be a parent process.
If id is equal to zero, it will be a child process.
So in this case, what we can observe is as soon as the parent process is scheduled it returns.
Whereas the child process sleeps for 10s and then it exits.
So meanwhile, when let's say that the child process is doing some calculation, in this case, just
to demonstrate we are using a sleep of 10s.
But let's consider some use case where a child process is doing some calculation, and it takes more
time for the child process to exit and before the child process process has exited, if the parent process
has exited, then the child process becomes an orphan process.
So in this case, the parent process exits as soon as it's scheduled, but the child process runs,
and during its execution, the child process becomes an orphan process.
Let's execute this and see the output of the code.
Let's compile the code for one dot c minus four open test.
Now we have compiled the program.
Let's execute it.
So when we execute it, what we can see is as soon as we execute the parent process exits then the child
process runs.
During this the child process becomes a non orphan.
Then later it exits.
So in Linux system, when a orphan process is present, then a process of reprinting occurs and the
parent of this child process is taken by the other process.
And in Linux system, usually the init process which has a PID of zero, becomes a parent of the child
process which is in an orphan state.
Let's execute this.
So if you see here the parent process has exited, but the child process is still running and uh, and
now the child process becomes an orphan orphan process.
And now the child process after sleeping for 10s it has exited.
So the the moment when the child process was executing and when the parent process had already exited,
then the child process was in an orphan, uh, mode.
And at that time the reparenting occurred and the uh child process was reparented to the other process,
usually the init process.
Now let us see the example of a zombie process.
Let's understand the zombie process.
Let's open an example.
Uh, this is an example of a zombie process.
Let's understand what a zombie process is.
So in this case we have created a process by forking.
So we have a parent process.
And now the parent process has created a child process.
And if you see here the parent process.
Is let's say we sleep the we make the parent process to run for uh 15 seconds here.
And the child process.
Executes for uh five seconds.
So when a child process exits before the parent process, then there is an entry in the process table,
as we have discussed in the previous video session, that every process has an entry in the process
table.
So when a process exits, it still has a entry in the process table.
So this process is called.
This is called as a zombie.
So in action the child process does not exist, though all the virtual memory is deallocated and all
the resources corresponding to the process is removed.
But still the entry of this child process is present in the process table.
So this is called as a zombie.
So let's see this zombie in action.
Let's compile this code and let's see this gcc.
Let's compile the zombie one dot c minus object file zombie one.
So I have created a zombie one as the object file.
So now let's run this.
Let's take this command.
So now let's execute this.
So when we execute this what the behavior will be after five seconds when the child process schedules.
The child process exits.
Now this child process becomes a zombie.
Let's see this in action.
So now the child process is executing.
And now if we check this sorry.
Let's say we execute this.
If you see here this is the child process.
And it has a state called as zombie.
This is the state PS UX gives the running process.
and we have grid for zombie state.
And if you see here the child process has exited before the parent process.
And a child process has is in a zombie mode.
Zombie state.
Now so what we have observed here is when the child process exits and the entry of the process is still
present in the process table.
This state is called as a zombie.
So now in order to remove the zombie, we need to have a wait in the parent process.
So by introducing a wait in the parent process, we can remove the zombie state of the child process.
Let's see how to remove the zombie state in the parent process.
Let's open the next example.
Okay.
This example is similar to the previous example, but in this case we have created a fork and the parent
process is present and we have a child process.
So here as similar to the previous program, the child process exits after sleeping for five seconds.
Now as soon as it sleeps, as soon as the child process exits, the child process is in a zombie state.
Now, when the parent process performs an wait on the child process, by performing this, we are going
to remove the zombie state of the child process.
So before performing this state, the child process is still in a zombie state, but once this is performed,
once the parent collects the exit status of the child process, the zombie state is removed.
and now there is no more zombie of the child process.
To understand this, let us execute this program.
Let's compile the program.
Now we have compiled the program.
Let's execute this.
So the behavior would be.
The child sleeps for five seconds.
Then let's run this command on the prompt and see the status.
So after five seconds we will see a zombie.
But after the sprint is after this print, the zombie will be removed.
So let's execute this.
So let's clean up before execution.
So we have executed zombie two.
So we have a zombie process here and now.
Now, if we execute once again.
So the zombie process has been removed.
Let's rerun this once again.
So we are rerunning this once again.
So we have a child process which is in a zombie state.
Now after some time if we rerun this once again now the zombie process has been removed.
So if you see if you see this, what what has happened is the child process has executed.
And after five seconds it has exited.
Once the child process has exited.
It was in a zombie state because the parent had not yet collected the exit status of the child process.
Now, once the parent performs the exit status by performing wait PID, then the zombie state is being
removed.
That means the entry from the process table is removed, and now the child process is no more in the
zombie state.
A process is said to be in a sleeping state when it performs a function similar to sleep.
So in this case, the parameter passed here is the unit of time in seconds.
When a process is performing a sleep state, it performs a similar to a no operation in assembly, and
in this case this process does nothing.
And once the time specified in this is done, the scheduler considers this process and schedules the
process accordingly.