Introduction to Threads

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this lecture we'll be studying about threats threats are an important topic in operating system and in one of the previous lectures when we discuss about process management I have shown you the difference between processes and threats so I told you that a program under execution is known as a process and threat is a basic unit of execution so each program may have a number of processes associated with it and each process can have a number of threats executing in it so thread is a basic unit of execution or a basic unit of CPU utilization so that is how we can define a thread it is a basic unit of CPU utilization now let us see what comprises of a thread so a thread comprises of a thread ID a program counter a register set and a stack so each thread comprises of these four unique items and apart from this it shares with other threads belonging to the same process it's Code section data section and other operating system resources such as open files and signals so these are the things that comprises of a thread and apart from that the thread shares these things like the Code section data section and other operating system resources are shared between threads belonging to the same process so as I told you a process can contain different threads so the threads belonging to the same process will share the resources slight open files signals Code section data section and other things so that is how a thread is basically made up so a traditional or heavyweight process has a single thread of control but if a process has multiple threads of control it can perform more than one task at a time so a traditional process also known as a heavyweight process had only a single thread of control that means it can have only one thread but this is not a very efficient system but if a process is having more than one threads then each thread will be assigned a different task and hence that process can perform more than one task at a time because it is having multiple and multiple threats will be performing multiple actions so there we see how treads becomes useful and how using threads makes the system much more efficient so here let us try to understand the concept of threads using these two diagrams so here in the first diagram it is a single threaded process which we refer to as a traditional or a heavyweight process over here so here we are having only a single thread so this whole block is considered as a process and this process contains only one thread and this is the code section the data section the files the registers and the stack belonging to this process so there is only one thread that means this process can perform only one task at a time but in contrast to this here we have another diagram showing a multi-threaded process so here this whole block represents a multi-threaded process so it is a single process and it is having multiple threads over here so if you see there are three threads in this process and each of these threads they have their own stack and registers so all these three threads they have their own stack and registers and if you see on top the code section the data section and the files belonging to this process are shared by these three threads and each thread they have their own stacks and registers so this is a diagram of a multi-threaded process so in this case this process can perform multiple tasks at a time because each thread will be performing a different task and hence multiple tasks can be performed at a time by the help of this multi-threaded process so from that itself we must have understood that it is much more efficient as compared to the single threaded process and it will make our computation faster and more efficient so in most of the systems that we use today we follow the system of multi-threaded processes it would be good if we can visually see how it works in our system in order to understand how processes are there and how threads are associated with the processes so here I have a small software processed threats view which helps us to see the processes and the threats running in our system so if I click here I can see the processes running in my system so I have opened the chromium browser in my system so here I can see the chrome dot exe processes which are running in my system so there are quite a few number of chrome dot exe processes running in the system now if I want to see the thread associated with it I will just click on it and here the details about the thread is populated so there are this many number of threads associated with that single chrome dot exe process that was for running so as I told you the thread comprises of a thread ID so these under IDs of the thread which identifies those threads uniquely and then these are the context switches and the last context switches that happened so we have discuss about context switches in one of the previous lectures and here there are so many informations about the thread that we have selected now if we come here we can see that there is the address of the thread and here we can see the details of the stack associated with that process so I told you that threads also container stack so in this way we can see what are the processes running in our system and we can also see the threads associated with those processes so we clearly see that in this system it is following a multi-threaded approach so when I click that single process of that chrome dot exe we see that this many number of threads are running inside that process so each of these threads may be used for performing a different task so in that way the browser can do more than one task at a time so let's say for example one of the processes of this browser chrome load exe is used for displaying the web page on the browser window and let's say that another thread is used for downloading something from the internet so we know that when we are downloading something we can also view the page at the same time so these two things are happening simultaneously that is possible because we are having multiple threads one thread is taking care of displaying and the other thread is taking care of downloading so if we were having a single-threaded process then while the downloading was happening the page could not be displayed or while the page is being displayed the download will not happen so that is this a simple example that I want to take in order to make us understand the usage of multi-threaded processes so different threads perform different tasks and hence multiple tasks can be performed in the same time hence improving our efficiency and speed of computation so we have seen a visual example of how multi thread processes are there in our system now let us see what are the benefits of having multi-threaded processes so from the example that I have taken you must have already understood the benefits of using multi-threaded processes but let us understand these benefits in more detail so the benefits of multi-threaded programming can be broken down into four major categories so let's see what they are one-by-one so the first benefit is responsiveness multi-threading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation thereby increasing responsiveness to the user so we are having a better responsiveness if we have multi-threading so I can explain this using the same example that I took about the web browser since we said that we are having different threads one for displaying the web page and another one for downloading and let's say that another one for the user to interact with the web page so if only one thread was there then until and unless one of the tasks completes the other task cannot be done so the user have to wait for one of the tasks to be complete in order to go to the next task but in case of multi-threading since different threads are simultaneously performing different tasks the responsiveness to the user increases so that is the first benefit of multi-threaded programming let us see what is the next benefit so the next benefit is a resource sharing so by default thread share the memory and resources of the process to which they belong the benefits of sharing code and data is that it allows an application to have several different threads of activity within the same address space so the next benefit is resource sharing so when we saw the diagram of different processes here we saw that these multiple threats they are sharing the resources of the same process so the code data and files are shared between these three threats so we said that it shares with other threats belonging to the same process it's Code section data section and other operating system resources suggest open files and signals so we see that resource sharing is happening in multi-threaded programming so the benefit of sharing code and data is that it will allow application to have several different types of activity within the same address space so we can have several different threats in the same address space of the same process so by resource sharing we are making our system more efficient because it does not need to have separate or dedicated resources for each and every threats so sharing resource is another benefit of multi-threaded programming so coming to the next benefit we have economy so allocating memory and resources for process creation is costly because threats share resource of the process to which they belong it is more economical to create and context switch threats so we already saw that there is a resource sharing happening in case of multi-threading so if there was no multi-threading then we will need to have a separate process for each and every task that has to be performed and allocating memory and resources for the processes is costly because each process will need to have its dedicated resources and memory which it may not be able to share with other processes but in threads as they are able to share the resource of the processes to which they belong it is more economical to create and switch between threads because we don't need to have those dedicated resources they are able to share and hence it is economy so this economy it comes from this resource sharing now moving on the next and the last benefit that we have is utilization of multi processor architectures the benefit of multi-threading can be greatly increased in a multiprocessor architecture where threads may be running in parallel different processors a single-threaded process can only run on one CPU no matter how many are available multi-threading on a multi CPU machine increases concurrency so the next benefit we have is utilization of multi processor architecture so we have already studied about multi processor architecture in this lecture series of operating system so multi processor architecture means there are several number of processors in the world system but if we are not having a multi-threaded approach and instead having a single threaded process then what will happen is that if we are having one process that one process can run on only one processor or on one CPU so you may have different CPUs so let's say for example that we are having four processors in your system but even though you are having four processors since your process is a single threaded process that single process can run only on one of the processor so no matter how many number of processors you may have having a single threaded process will run in only one of the processor but if we are having a multi-threaded approach then what will happen is that we know that in a multi thread approach each process has different number of threads associated with it so each of these threads can run on these multiple processors let's say that there are four threads associated with a single process and then there are four processors in your system so each of those threads can run on one of the processors so thread one may run in processor one thread to myrin processor two and so on so in this way we see that the tasks are executed concurrently at the same time and hence the process will be completed quicker and it is more efficient that is what we mean by utilization of multi processor architecture so if we don't have a multi-threaded approach even though we have multi processors it is not very useful because we are not able to make use of the multi processor architecture but in case of multi-threading we are able to make use of the multiprocessor architecture hence improving our efficiency of computing so these are the main benefits of multi programming so with this I hope you got the basic idea of threads and what is the meaning of single threaded process and multi thread process and we also saw that most of the systems now we follow a multi-threaded approach and we see that how a single process can have multiple threads associated with it and how they share the resources of that process to which they belong and how these things are beneficial for our computing so we'll be studying more things about threads in the coming lectures so for now I hope the basics of thread is clear to you so thank you for watching and see you in the next one [Applause] [Music]
Info
Channel: Neso Academy
Views: 204,948
Rating: 4.93361 out of 5
Keywords: threads, threads in os, threads in operating system, threads in os tutorial, threads in os example, os threads, os process and threads, single threaded process, multi threaded process, os, os lectures, os best lectures, operating system, operating system lecture
Id: LOfGJcVnvAk
Channel Id: undefined
Length: 14min 6sec (846 seconds)
Published: Fri May 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.