Hello and welcome!
In this video session we will discuss on the combination usage of fork and exec.
So in the previous video sessions we had discussed fork separately and exec separately.
So fork is a system call used to create a child process, whereas exec family of function calls are
the one which replaces the existing program virtual memory by the new program's virtual memory.
Now let us see how the combination of fork and actually works.
And in most of the use cases, a exec is usually used along with the fork.
So let us uh, go to the example program.
So in this case we have the program one dot c.
And here we create a fork.
And the program one creates a child process.
And in this case it executes the child process if Cpid.
The return value of fork is zero, then this part will be the child process.
Else the parent process executes and when it executes, it waits for the child process to finish.
Then it collects the wait status and then it prints the child's process ID and prints the status and
exits.
Now, an important point to note here is in the child process we have called an execl.
So what happens is we have now two processes in memory.
One is the main process which is nothing but the parent process, the other is the child process.
Now in child process, we are replacing the virtual memory of the child process by a new memory, a
new virtual memory of program two.
Now in the memory.
We have two separate virtual memories, and both the virtual memories are different.
Now, before when we fork the child process, the virtual memory of the child was similar to the virtual
memory of the parent and.
But in this case, the virtual memory of the child process is different from the parent's virtual memory
because of its equal.
And this has a lot of use cases where the child process can execute its own set of instructions and
return the status back to the parent process.
Now let's open the program to code as well here.
Here this is the program two to code.
This is a very simple program where we print.
We print the process ID of the running program and we print the arguments.
Command line arguments and then sleep for 20s in order to say that.
In order to verify that the parent is waiting when the child is sleeping, then we.
Then the program two returns, and once it returns, this exit information will be collected in the
parent.
So let's compile both the programs separately and execute it so that we can have a clear picture on
the combination of fork and exec.
So now let's uh, let's compile the program gcc program one dot c minus o program one.
Let's compile the second program as well.
Now let's run program one.
And let's have a look at the same time on the code as well.
So when we run program one, this program is run and the program one forks a child process.
And in the child process the child process virtual memory is replaced by this program.
So let's have a look into this.
Now here child the parent is executing before the before wait and child process ID is 18615.
Now if you see here, uh, the parent is waiting here at this point of time for the child to finish
its execution.
And the child is executing the sleep of 20s.
Once the child finishes its execution, the.
Wait, uh, the white function call is done in the parent process, and it comes out of the wait and
prints the, uh, process ID of the child.
So if you see here this was the first, uh, parent process running, and it created a child 18615 five
with a process ID 18615.
This is what we got here.
Parent executing before weight.
So we got this here.
Parent executing before weight.
Then we got before execute.
This is from uh here.
This one where when the child process uh, was created and it got the scheduling time and it executed
this line.
Now after that, it it created an equal.
And the new program was being loaded in the virtual memory of process, uh, child process.
And if you note this line is not printed, will not be seen here because this program's virtual memory
is being replaced by the new program.
And when this is called, it begins its execution from the main of program two.
And we can see I am a new process called bicycle will be printed here.
And the PID is 18615.
So if you note here this programs two will be ran as a child process now, right?
This process ID and the process ID created by the parent is same.
And these are the command line arguments passed from the by from here from the program one.
And the wait in the parent is done.
So this will be uh, this print will get only after the child sleeps for 20s.
And when it exits, it returns here.
And the parent process executes and prints the following lines.
So it prints the cpp id, which is nothing but the self process ID and also prints the status here.
So status is zero because it uh, because it entered a returned a zero here.
So this is the main use case of fork and exec together.