00:00:00
in this lecture we'll be studying about
00:00:02
threats threats are an important topic
00:00:04
in operating system and in one of the
00:00:06
previous lectures when we discuss about
00:00:08
process management I have shown you the
00:00:10
difference between processes and threats
00:00:12
so I told you that a program under
00:00:14
execution is known as a process and
00:00:17
threat is a basic unit of execution so
00:00:20
each program may have a number of
00:00:22
processes associated with it and each
00:00:25
process can have a number of threats
00:00:27
executing in it so thread is a basic
00:00:30
unit of execution or a basic unit of CPU
00:00:34
utilization so that is how we can define
00:00:36
a thread it is a basic unit of CPU
00:00:38
utilization now let us see what
00:00:41
comprises of a thread so a thread
00:00:43
comprises of a thread ID a program
00:00:46
counter a register set and a stack so
00:00:49
each thread comprises of these four
00:00:52
unique items and apart from this it
00:00:55
shares with other threads belonging to
00:00:57
the same process it's Code section data
00:01:00
section and other operating system
00:01:02
resources such as open files and signals
00:01:05
so these are the things that comprises
00:01:08
of a thread and apart from that the
00:01:11
thread shares these things like the Code
00:01:13
section data section and other operating
00:01:15
system resources are shared between
00:01:18
threads belonging to the same process so
00:01:20
as I told you a process can contain
00:01:23
different threads so the threads
00:01:25
belonging to the same process will share
00:01:27
the resources slight open files signals
00:01:30
Code section data section and other
00:01:32
things so that is how a thread is
00:01:35
basically made up so a traditional or
00:01:38
heavyweight process has a single thread
00:01:41
of control but if a process has multiple
00:01:44
threads of control it can perform more
00:01:47
than one task at a time so a traditional
00:01:50
process also known as a heavyweight
00:01:52
process had only a single thread of
00:01:55
control that means it can have only one
00:01:58
thread but this is not a very efficient
00:02:00
system but if a process is having more
00:02:02
than one threads then each thread will
00:02:05
be assigned a different task and hence
00:02:08
that process can perform more than one
00:02:10
task at a time because it is having
00:02:12
multiple
00:02:13
and multiple threats will be performing
00:02:15
multiple actions so there we see how
00:02:18
treads becomes useful and how using
00:02:21
threads makes the system much more
00:02:24
efficient so here let us try to
00:02:26
understand the concept of threads using
00:02:28
these two diagrams so here in the first
00:02:31
diagram it is a single threaded process
00:02:33
which we refer to as a traditional or a
00:02:35
heavyweight process over here so here we
00:02:38
are having only a single thread so this
00:02:41
whole block is considered as a process
00:02:44
and this process contains only one
00:02:46
thread and this is the code section the
00:02:49
data section the files the registers and
00:02:52
the stack belonging to this process so
00:02:55
there is only one thread that means this
00:02:57
process can perform only one task at a
00:02:59
time but in contrast to this here we
00:03:02
have another diagram showing a
00:03:04
multi-threaded process so here this
00:03:06
whole block represents a multi-threaded
00:03:09
process so it is a single process and it
00:03:12
is having multiple threads over here so
00:03:14
if you see there are three threads in
00:03:17
this process and each of these threads
00:03:19
they have their own stack and registers
00:03:22
so all these three threads they have
00:03:25
their own stack and registers and if you
00:03:27
see on top the code section the data
00:03:30
section and the files belonging to this
00:03:33
process are shared by these three
00:03:36
threads and each thread they have their
00:03:39
own stacks and registers so this is a
00:03:42
diagram of a multi-threaded process so
00:03:45
in this case this process can perform
00:03:48
multiple tasks at a time because each
00:03:50
thread will be performing a different
00:03:52
task and hence multiple tasks can be
00:03:55
performed at a time by the help of this
00:03:57
multi-threaded process so from that
00:04:00
itself we must have understood that it
00:04:02
is much more efficient as compared to
00:04:04
the single threaded process and it will
00:04:06
make our computation faster and more
00:04:09
efficient so in most of the systems that
00:04:11
we use today we follow the system of
00:04:14
multi-threaded processes it would be
00:04:16
good if we can visually see how it works
00:04:18
in our system in order to understand how
00:04:20
processes are there and how threads are
00:04:23
associated with the processes so here I
00:04:25
have a small software
00:04:26
processed threats view which helps us to
00:04:29
see the processes and the threats
00:04:31
running in our system so if I click here
00:04:34
I can see the processes running in my
00:04:36
system so I have opened the chromium
00:04:39
browser in my system so here I can see
00:04:41
the chrome dot exe processes which are
00:04:44
running in my system so there are quite
00:04:47
a few number of chrome dot exe processes
00:04:49
running in the system now if I want to
00:04:52
see the thread associated with it I will
00:04:54
just click on it and here the details
00:04:57
about the thread is populated so there
00:05:00
are this many number of threads
00:05:03
associated with that single chrome dot
00:05:05
exe process that was for running so as I
00:05:08
told you the thread comprises of a
00:05:10
thread ID so these under IDs of the
00:05:13
thread which identifies those threads
00:05:15
uniquely and then these are the context
00:05:17
switches and the last context switches
00:05:19
that happened so we have discuss about
00:05:21
context switches in one of the previous
00:05:23
lectures and here there are so many
00:05:24
informations about the thread that we
00:05:27
have selected now if we come here we can
00:05:30
see that there is the address of the
00:05:33
thread and here we can see the details
00:05:35
of the stack associated with that
00:05:38
process so I told you that threads also
00:05:40
container stack so in this way we can
00:05:43
see what are the processes running in
00:05:45
our system and we can also see the
00:05:47
threads associated with those processes
00:05:49
so we clearly see that in this system it
00:05:52
is following a multi-threaded approach
00:05:54
so when I click that single process of
00:05:57
that chrome dot exe we see that this
00:05:59
many number of threads are running
00:06:01
inside that process so each of these
00:06:03
threads may be used for performing a
00:06:05
different task so in that way the
00:06:07
browser can do more than one task at a
00:06:10
time so let's say for example one of the
00:06:12
processes of this browser chrome load
00:06:14
exe is used for displaying the web page
00:06:17
on the browser window and let's say that
00:06:19
another thread is used for downloading
00:06:22
something from the internet so we know
00:06:24
that when we are downloading something
00:06:25
we can also view the page at the same
00:06:28
time so these two things are happening
00:06:30
simultaneously that is possible because
00:06:32
we are having multiple threads one
00:06:34
thread is taking care of displaying and
00:06:36
the other thread is taking care of
00:06:37
downloading so if we were having
00:06:40
a single-threaded process then while the
00:06:43
downloading was happening the page could
00:06:45
not be displayed or while the page is
00:06:47
being displayed the download will not
00:06:49
happen so that is this a simple example
00:06:51
that I want to take in order to make us
00:06:53
understand the usage of multi-threaded
00:06:55
processes so different threads perform
00:06:57
different tasks and hence multiple tasks
00:06:59
can be performed in the same time hence
00:07:01
improving our efficiency and speed of
00:07:04
computation so we have seen a visual
00:07:06
example of how multi thread processes
00:07:08
are there in our system now let us see
00:07:10
what are the benefits of having
00:07:11
multi-threaded processes so from the
00:07:14
example that I have taken you must have
00:07:15
already understood the benefits of using
00:07:17
multi-threaded processes but let us
00:07:19
understand these benefits in more detail
00:07:22
so the benefits of multi-threaded
00:07:23
programming can be broken down into four
00:07:25
major categories so let's see what they
00:07:28
are one-by-one so the first benefit is
00:07:30
responsiveness multi-threading an
00:07:32
interactive application may allow a
00:07:35
program to continue running even if part
00:07:38
of it is blocked or is performing a
00:07:40
lengthy operation thereby increasing
00:07:43
responsiveness to the user so we are
00:07:46
having a better responsiveness if we
00:07:49
have multi-threading so I can explain
00:07:52
this using the same example that I took
00:07:53
about the web browser since we said that
00:07:56
we are having different threads one for
00:07:58
displaying the web page and another one
00:08:00
for downloading and let's say that
00:08:02
another one for the user to interact
00:08:04
with the web page so if only one thread
00:08:07
was there then until and unless one of
00:08:10
the tasks completes the other task
00:08:12
cannot be done so the user have to wait
00:08:14
for one of the tasks to be complete in
00:08:16
order to go to the next task but in case
00:08:19
of multi-threading since different
00:08:21
threads are simultaneously performing
00:08:22
different tasks the responsiveness to
00:08:25
the user increases so that is the first
00:08:27
benefit of multi-threaded programming
00:08:30
let us see what is the next benefit so
00:08:32
the next benefit is a resource sharing
00:08:34
so by default thread share the memory
00:08:36
and resources of the process to which
00:08:39
they belong the benefits of sharing code
00:08:41
and data is that it allows an
00:08:44
application to have several different
00:08:46
threads of activity within the same
00:08:48
address space so the next benefit is
00:08:51
resource sharing so when we saw the
00:08:53
diagram of
00:08:53
different processes here we saw that
00:08:56
these multiple threats they are sharing
00:08:58
the resources of the same process so the
00:09:01
code data and files are shared between
00:09:05
these three threats so we said that it
00:09:07
shares with other threats belonging to
00:09:09
the same process it's Code section data
00:09:12
section and other operating system
00:09:14
resources suggest open files and signals
00:09:17
so we see that resource sharing is
00:09:19
happening in multi-threaded programming
00:09:21
so the benefit of sharing code and data
00:09:24
is that it will allow application to
00:09:26
have several different types of activity
00:09:28
within the same address space so we can
00:09:30
have several different threats in the
00:09:32
same address space of the same process
00:09:34
so by resource sharing we are making our
00:09:37
system more efficient because it does
00:09:40
not need to have separate or dedicated
00:09:42
resources for each and every threats so
00:09:45
sharing resource is another benefit of
00:09:47
multi-threaded programming so coming to
00:09:50
the next benefit we have economy so
00:09:52
allocating memory and resources for
00:09:54
process creation is costly because
00:09:57
threats share resource of the process to
00:09:59
which they belong it is more economical
00:10:01
to create and context switch threats so
00:10:05
we already saw that there is a resource
00:10:07
sharing happening in case of
00:10:09
multi-threading so if there was no
00:10:11
multi-threading then we will need to
00:10:13
have a separate process for each and
00:10:16
every task that has to be performed and
00:10:18
allocating memory and resources for the
00:10:20
processes is costly because each process
00:10:24
will need to have its dedicated
00:10:25
resources and memory which it may not be
00:10:28
able to share with other processes but
00:10:31
in threads as they are able to share the
00:10:34
resource of the processes to which they
00:10:36
belong it is more economical to create
00:10:39
and switch between threads because we
00:10:42
don't need to have those dedicated
00:10:43
resources they are able to share and
00:10:45
hence it is economy so this economy it
00:10:48
comes from this resource sharing now
00:10:51
moving on the next and the last benefit
00:10:53
that we have is utilization of multi
00:10:56
processor architectures the benefit of
00:10:58
multi-threading can be greatly increased
00:11:00
in a multiprocessor architecture where
00:11:03
threads may be running in parallel
00:11:05
different processors a single-threaded
00:11:07
process can only run on one CPU no
00:11:11
matter how many are available
00:11:13
multi-threading on a multi CPU machine
00:11:15
increases concurrency so the next
00:11:18
benefit we have is utilization of multi
00:11:20
processor architecture so we have
00:11:22
already studied about multi processor
00:11:24
architecture in this lecture series of
00:11:26
operating system so multi processor
00:11:28
architecture means there are several
00:11:30
number of processors in the world system
00:11:32
but if we are not having a
00:11:34
multi-threaded approach and instead
00:11:36
having a single threaded process then
00:11:39
what will happen is that if we are
00:11:41
having one process that one process can
00:11:44
run on only one processor or on one CPU
00:11:47
so you may have different CPUs so let's
00:11:49
say for example that we are having four
00:11:51
processors in your system but even
00:11:54
though you are having four processors
00:11:56
since your process is a single threaded
00:11:59
process that single process can run only
00:12:02
on one of the processor so no matter how
00:12:05
many number of processors you may have
00:12:08
having a single threaded process will
00:12:10
run in only one of the processor but if
00:12:13
we are having a multi-threaded approach
00:12:16
then what will happen is that we know
00:12:18
that in a multi thread approach each
00:12:20
process has different number of threads
00:12:22
associated with it so each of these
00:12:25
threads can run on these multiple
00:12:28
processors let's say that there are four
00:12:30
threads associated with a single process
00:12:32
and then there are four processors in
00:12:35
your system so each of those threads can
00:12:38
run on one of the processors so thread
00:12:40
one may run in processor one thread to
00:12:42
myrin processor two and so on so in this
00:12:45
way we see that the tasks are executed
00:12:48
concurrently at the same time and hence
00:12:50
the process will be completed quicker
00:12:52
and it is more efficient that is what we
00:12:55
mean by utilization of multi processor
00:12:57
architecture so if we don't have a
00:12:59
multi-threaded approach even though we
00:13:01
have multi processors it is not very
00:13:03
useful because we are not able to make
00:13:06
use of the multi processor architecture
00:13:08
but in case of multi-threading we are
00:13:10
able to make use of the multiprocessor
00:13:12
architecture hence improving our
00:13:14
efficiency of computing so these are the
00:13:17
main benefits of multi
00:13:19
programming so with this I hope you got
00:13:21
the basic idea of threads and what is
00:13:24
the meaning of single threaded process
00:13:25
and multi thread process and we also saw
00:13:28
that most of the systems now we follow a
00:13:31
multi-threaded approach and we see that
00:13:33
how a single process can have multiple
00:13:36
threads associated with it and how they
00:13:38
share the resources of that process to
00:13:40
which they belong and how these things
00:13:43
are beneficial for our computing so
00:13:45
we'll be studying more things about
00:13:47
threads in the coming lectures so for
00:13:49
now I hope the basics of thread is clear
00:13:51
to you
00:13:52
so thank you for watching and see you in
00:13:53
the next one
00:13:55
[Applause]
00:13:57
[Music]