WEBVTT

00:08.000 --> 00:11.660
Now, if I ask you a question that what is schedulable?

00:11.660 --> 00:13.670
Is it threads or processes?

00:13.820 --> 00:17.630
So your kernel or operating system do not schedule the processes.

00:17.630 --> 00:25.160
It schedule the threads by schedule I mean that how operating system decides that which thread to allocate

00:25.190 --> 00:27.380
which CPU for execution.

00:28.350 --> 00:31.600
So it is the thread which is a schedulable entity.

00:31.620 --> 00:33.520
It is not a process.

00:33.540 --> 00:40.320
So in the diagram you can see on the right hand side, I have drawn a block diagram of a computer system

00:40.320 --> 00:45.060
which is mainly divided into two parts the user space and kernel space.

00:45.090 --> 00:51.060
Kernel space is a space where your kernel or operating system is running, whereas in user space all

00:51.060 --> 00:53.670
your applications are running right.

00:54.670 --> 00:58.120
Now, let us suppose that in user space there is a user process.

00:58.150 --> 00:58.810
P1.

00:58.840 --> 01:04.270
That user process P1 has two threads that is the thread T1 and threat T2.

01:04.300 --> 01:05.140
Right.

01:05.920 --> 01:12.550
Similarly, there is another process P2, which has two more threads, let us say T3 and T4.

01:12.700 --> 01:13.510
Right.

01:14.820 --> 01:19.620
So now your kernel or operating system will going to schedule the threads.

01:19.650 --> 01:22.380
T1, T2, T3 and T4.

01:22.410 --> 01:30.360
These four threads will compete for these three CPUs which are attached to the system as hardware resources.

01:31.100 --> 01:36.990
So remember, the operating system sees threads as schedulable entity and not a process.

01:37.010 --> 01:42.980
Here the competition for CPU is between four threads that is T1, T2, T3 and T4.

01:43.850 --> 01:49.160
As I said in the beginning section of this course, that thread is nothing, but it is a basic unit

01:49.190 --> 01:51.350
of execution flow, right?

01:51.350 --> 01:57.980
And whatever is execution flow is supposed to be allocated CPU by the operating system for execution.

01:58.550 --> 02:05.390
However, the fact that operating system sees thread as a schedulable entity and not the process is

02:05.390 --> 02:08.150
violated in certain error conditions.

02:08.390 --> 02:15.590
The first error condition is that that for some reason if a thread commits some crime and it is segmentation

02:15.590 --> 02:20.750
fault, then it is the entire process which is terminated by the operating system.

02:20.750 --> 02:21.470
Right?

02:21.470 --> 02:24.540
That is including all the threads of a process.

02:24.560 --> 02:32.600
So in this example, suppose the thread t1 do some illegal thing and and it is given a segmentation

02:32.600 --> 02:35.120
fault signal by the operating system.

02:35.120 --> 02:40.520
Then the entire process P1 will going to be terminated by the operating system.

02:40.520 --> 02:41.140
Right.

02:41.150 --> 02:46.730
It simply means that the thread T2 pays the penalty for the crime which is committed by the thread.

02:46.760 --> 02:48.260
T1 Right.

02:49.450 --> 02:54.490
Similarly, a signal is delivered per process and not per thread.

02:54.520 --> 02:59.920
Now, if you don't have an idea about signals, then it is better to understand this point.

02:59.950 --> 03:07.510
When you study signals at a high level, let me explain that when the kernel space or operating system

03:07.510 --> 03:11.770
delivers the signal as then it is delivered.

03:11.800 --> 03:17.260
Then the signal s is delivered at a process level and not at the thread level.

03:17.290 --> 03:21.160
One example of the signal s is segmentation fault signal.

03:21.520 --> 03:25.630
That is sigsegv signal, right.

03:25.660 --> 03:32.020
Whenever the signal is delivered by the operating system to the process, the entire process is terminated

03:32.020 --> 03:34.030
with segmentation fault error.

03:34.360 --> 03:40.990
Similarly, when you have a multithreaded program which is running and when you press control C, it

03:40.990 --> 03:45.880
is not one particular thread which is terminated, but the entire application is terminated.

03:45.880 --> 03:46.480
Right?

03:46.480 --> 03:52.310
So when you press control C the, the operating system delivers another type of signal to the process,

03:52.310 --> 03:55.520
which is called sigterm signal, right?

03:55.610 --> 04:02.060
So here what I am trying to say is that signal is something which works at the process level and not

04:02.060 --> 04:03.320
at the thread level.

04:03.350 --> 04:09.420
Now remember that we discussed the race condition which is created when a new thread is created, right?

04:09.440 --> 04:16.580
So the race condition on thread creation is due to the fact that which thread the kernel chooses to

04:16.580 --> 04:21.290
allocate CPU, whether it's a parent thread or it's a child thread.

04:21.290 --> 04:21.920
Right?

04:21.920 --> 04:25.790
So because of this indeterminism, the race condition is there.

04:26.940 --> 04:33.090
And the last point is that that the kernel schedules threads on multiple CPUs as per the scheduling

04:33.090 --> 04:33.840
policy.

04:33.870 --> 04:38.100
You must have studied in your academics that there are various types of scheduling policy.

04:38.100 --> 04:45.050
For example, first come, first serve policy or shortest job, first scheduling policy, etcetera.

04:45.060 --> 04:45.840
Right.

04:47.630 --> 04:53.870
So as part of this policy scheme, the kernel operating system chooses one thread among many which are

04:53.870 --> 04:57.410
competing for the CPU and allocates the CPU to that thread.

04:58.280 --> 05:02.630
Also note that in this diagram I have drawn three CPUs.

05:02.660 --> 05:03.420
Right?

05:03.440 --> 05:10.820
So it means that your operating system has three CPUs on which it has to schedule all the live threads

05:10.820 --> 05:12.020
running in the system.

05:13.420 --> 05:21.700
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

05:21.700 --> 05:24.990
thread T3 to run on CPU three and so on.

05:25.000 --> 05:25.810
Right.

05:27.240 --> 05:33.150
So all this theoretical concept will help you to understand the advanced features of Multithreading,

05:33.150 --> 05:37.200
which we will going to discuss in this course in the subsequent sections.
