Transcript for:
Understanding Threading Fork and Exec Issues

In this lecture, we will be studying about threading issues. So, in the previous few lectures, we have been studying and discussing about threads and we saw how process consists of threads and we also saw multi-threading and multi-threading models and we saw how threads makes our computation more efficient. But also, we need to know that when we use threads, there are certain issues that are related to this threading and we need to address those issues.

So, in this lecture, we will be studying about one of the issues that we face in threading and we will see how that can be addressed. So, the first issue that we are going to discuss in threading is related to the fork and exe system calls which we discussed in the previous lecture. So, in the previous lecture we saw that the fork and exe system calls are used for specific purposes. And what are they?

The fork is used for duplicating a process or for creating a child process from a parent process with a different process id. And then the exe system call is used for replacing the contents of a process with another process but retaining the same process id. Now, the issue that we are going to face related to threading is that how will this fork and exe system calls behave when we have threading involved. So, the semantics of the fork and exe system calls change in a multi-threaded program.

And why is that? That is because we know that a process can consist of multiple threads. And let's say that one thread in the process it requests for a fork system call. So, when the fork call is made, we know that the function of the fork system call is to create a duplicate process. So, when a particular thread belonging to a process calls the fork system call, will all the threads present in that process be duplicated or will only the thread that is calling the fork system call be duplicated?

So, that is the question that we can have. So, this is the issue that we have. If one thread in a program calls fork, does the new process duplicate all threads or is the new process single threaded? So, what is the solution for this? So, the solution for this is that some Unix systems have chosen to have two versions of fork.

One that duplicates all the threads and another that duplicates only the thread that invoked the fork system call. So, in order to solve this issue, Some of the Unix systems have adopted two versions of fork. And among the two versions of fork, one of them will duplicate all the threads. That means if a thread in a process invokes a fork system call, then all the threads in that process will be duplicated. So, that is one version of fork that is there.

And then the other version of fork, what it does is, it will duplicate only that thread that invoked the fork system call. So, if a thread belonging to a process invokes a fork system call then only that particular thread will be duplicated and the other threads present in a process will not be duplicated so always keep in mind when we are talking about this that threats are a part of a process so there is a particular process and a particular process can consist of a number of threads so it is these threads that makes up a particular process so that is why we are having this issue that is whether to duplicate only that thread that invokes the fork system call or whether to duplicate all the threads present in that process from where the particular thread made a fork system call. So, we are having two versions of fork in order to tackle this.

So, one will duplicate all the threads and in the other one it will duplicate only that thread that invoked the fork system call. But now the question arises which of the fork to use and when. So, we saw that there are two versions of fork that some unique systems have adopted. But how do we know which version of fork to use and when?

When should we duplicate all the threads? Or when should we duplicate only the single thread that called the fork system call? Which version of fork to use and when? Now, before we come to that, let us also understand and recall what was the purpose of the exeC system call and what will happen when exeC system call is invoked in a process consisting of multiple threads.

So, if a thread invokes the exeC system call, The program specified in the parameter to EXCC will replace the entire process including all the threads. So, let's say that we are having a process again consisting of a number of threads and one of the thread invokes a EXCC system call. And we know what is the function of EXCC system call. It is to replace the contents of a process with another process that is passed as a parameter in the EXCC system call.

So, So, when a thread invokes the EXCC system call, then what will happen? The entire process will be replaced including all the threads. So, there is no exception to that. So, whenever a EXCC system call is invoked even by just a single thread among many threads in a process, then the entire process including all the threads are going to be replaced by the new process that is mentioned in the parameter of the EXCC system call. Now, let us try to understand which version of fork to use and when.

Now, we will combine the uses of fork and exe-c and see what are the situations or what are the scenarios in which the fork and exe-c system calls are invoked and which one to use when. Alright, so now we are going to find out which version of fork to use and when. So, which of the two versions of forks to use depends on the application.

Let's see how do we decide which one to use. So, here is the first case. That is, if EXCC is called immediately after forking. That means if we have just invoked a fork system call or if a thread has just invoked a fork system call and immediately after that, if the EXCC system call is also invoked, then what should we do?

Then in this case, duplicating all the threads is unnecessary as the program specified in the parameters to EXCC will replace the process. So, in this instance, duplicating all the threads only the calling thread is appropriate. Alright, so let us try to understand what this means. So, if an EXCC is called immediately after forking, when we fork, what is going to happen? The process is going to be duplicated.

So, if EXCC is called immediately after forking, then what will happen? EXCC is going to replace the contents of the process by another process that is passed as a parameter in this EXCC function. So, just think that If we duplicate all the threads, what will happen?

Even if we duplicate all the threads, after the fork system call was invoked, immediately EXEC is also invoked. So, what will happen? Even though we have duplicated all the threads, the EXEC system call is going to replace those entire threads that were duplicated with something else.

Because that is what EXEC does. It becomes useless if we just duplicate all the threads. Because anyway they are going to be replaced by the EXCC system call by another process.

So, if this kind of a situation arise where EXCC is called immediately after forking, then it is better not to duplicate all the threads but to duplicate only that thread which invoked the fork system call. So, it says here in this instance duplicating only the calling thread is appropriate. And what is the calling thread?

The thread that calls the fork system call. So, that is what we have to do when EXCC is called immediately after forking. So, here we have made use of one of the versions of forking that we have discussed in the beginning of this lecture. So, we have discussed about two versions of fork.

So, one of them is used here. And which is that? The one which duplicates only the calling thread. Alright, now let's look at the second scenario. So, in the second scenario, if the separate process does not call EXCC after forking.

So, in this first scenario, EXCC is called immediately after forking. But in this scenario, let's say that a thread in a process calls the fork system call. That means the fork system call is invoked by one of the thread, but it is not followed by any EXCC.

Then what to do? Then the separate process should duplicate all the threads. Because here we know that EXCC is not going to be called or it is not called.

And when it is not called, we know that it is not going to be replaced. Nothing is going to be replaced. So, at that time the case may be that we want a duplicate of all the threads and that is why the thread actually called the fork system call.

So, when the fork system call is not followed by an EXCC, then the separate process should duplicate all the threads. So, that is how we use the two versions of fork depending upon the application. So, these are the two kind of scenarios that we can have. So, if the first scenario arises, which is this one, then we should duplicate only the thread that is invoking the fork system call.

And if the second scenario arises, where the fork system call is not followed by an EXCC, then the new process that is created by the fork system call should have all the threads in it. That means all the threads should be duplicated. So, that is how we deal with the issues related with fork and EXCC system call in a multi-threaded approach.

So, that was one of the issues that we face in threading. So, in the following lectures, we will be seeing some more issues that we face in threading and how they are resolved. So, I hope this lecture about the first issue that we face in threading was clear to you.

Thank you for watching and see you in the next one.