Now, if I ask you a question that what is schedulable?
Is it threads or processes?
So your kernel or operating system do not schedule the processes.
It schedule the threads by schedule I mean that how operating system decides that which thread to allocate
which CPU for execution.
So it is the thread which is a schedulable entity.
It is not a process.
So in the diagram you can see on the right hand side, I have drawn a block diagram of a computer system
which is mainly divided into two parts the user space and kernel space.
Kernel space is a space where your kernel or operating system is running, whereas in user space all
your applications are running right.
Now, let us suppose that in user space there is a user process.
P1.
That user process P1 has two threads that is the thread T1 and threat T2.
Right.
Similarly, there is another process P2, which has two more threads, let us say T3 and T4.
Right.
So now your kernel or operating system will going to schedule the threads.
T1, T2, T3 and T4.
These four threads will compete for these three CPUs which are attached to the system as hardware resources.
So remember, the operating system sees threads as schedulable entity and not a process.
Here the competition for CPU is between four threads that is T1, T2, T3 and T4.
As I said in the beginning section of this course, that thread is nothing, but it is a basic unit
of execution flow, right?
And whatever is execution flow is supposed to be allocated CPU by the operating system for execution.
However, the fact that operating system sees thread as a schedulable entity and not the process is
violated in certain error conditions.
The first error condition is that that for some reason if a thread commits some crime and it is segmentation
fault, then it is the entire process which is terminated by the operating system.
Right?
That is including all the threads of a process.
So in this example, suppose the thread t1 do some illegal thing and and it is given a segmentation
fault signal by the operating system.
Then the entire process P1 will going to be terminated by the operating system.
Right.
It simply means that the thread T2 pays the penalty for the crime which is committed by the thread.
T1 Right.
Similarly, a signal is delivered per process and not per thread.
Now, if you don't have an idea about signals, then it is better to understand this point.
When you study signals at a high level, let me explain that when the kernel space or operating system
delivers the signal as then it is delivered.
Then the signal s is delivered at a process level and not at the thread level.
One example of the signal s is segmentation fault signal.
That is sigsegv signal, right.
Whenever the signal is delivered by the operating system to the process, the entire process is terminated
with segmentation fault error.
Similarly, when you have a multithreaded program which is running and when you press control C, it
is not one particular thread which is terminated, but the entire application is terminated.
Right?
So when you press control C the, the operating system delivers another type of signal to the process,
which is called sigterm signal, right?
So here what I am trying to say is that signal is something which works at the process level and not
at the thread level.
Now remember that we discussed the race condition which is created when a new thread is created, right?
So the race condition on thread creation is due to the fact that which thread the kernel chooses to
allocate CPU, whether it's a parent thread or it's a child thread.
Right?
So because of this indeterminism, the race condition is there.
And the last point is that that the kernel schedules threads on multiple CPUs as per the scheduling
policy.
You must have studied in your academics that there are various types of scheduling policy.
For example, first come, first serve policy or shortest job, first scheduling policy, etcetera.
Right.
So as part of this policy scheme, the kernel operating system chooses one thread among many which are
competing for the CPU and allocates the CPU to that thread.
Also note that in this diagram I have drawn three CPUs.
Right?
So it means that your operating system has three CPUs on which it has to schedule all the live threads
running in the system.
It can choose thread T1 to run on CPU one and it can choose thread t2 to run on CPU two and it can choose
thread T3 to run on CPU three and so on.
Right.
So all this theoretical concept will help you to understand the advanced features of Multithreading,
which we will going to discuss in this course in the subsequent sections.