 Hello everyone. Today we are going to learn about threads in operating system. The learning outcome of this session are at the end of this session student will be able to compare between processes and threads and to introduce notion of threads which is a fundamental unit of CPU utilization that forms the basis of multi-threaded computer system. Let us see fundamental process characteristics. Process has two features that is unit of resource ownership and unit of execution. In unit of resource ownership, process includes a virtual address space for process image. For example, files or IO devices operating system also give protection to the process from input output devices or from some external resources. In unit of execution, the execution path may be interleaved with other processes. The execution states in this case are ready running or blocked and it contains some dispatching priority. The unit of resource ownership is also called as a process or task or it is also called as a heavy weight process while unit of execution is also called as a thread or light weight process. Let us see what is process and thread as shown in this figure. Thread is also called as a light weight process. It is a basic unit of CPU utilization. It contains thread ID and program counter. It also contains a register set and stack. Thread can share its code, data and file section with another thread if both belongs to a single process. Now let us see the single and multi-threaded process. Now as shown in this figure, we can see the left part of the figure is single-threaded process. As shown here, that thread contains its own thread ID and program counter along with its register sets and stack while code, data and file section is also associated with that thread. So this is example of process with single thread. Now in this case, it contains three different threads. The process has three different threads. As we can see here, each thread contains its own ID and program counter along with their own register set and stack while code, data and file section of that threads are shared with each other because they belongs to a single process. So this is nothing but a process with multiple thread and this process is more capable than this process because this process can perform more than one task at a time. So the process with multiple thread is called as a multi-threading. Multi-threading is nothing but ability of an operating system to support multiple concurrent path of execution within a single process. Means we have only one process but that process is able to execute multiple tasks at a time. Let us see the motivation behind the thread. Thread can perform multiple tasks independently of the others. If one of the tasks may block, then it is desired to allow the other task to proceed without blocking. For example, Microsoft Word. Now here we can see Microsoft Word as a single process but this process is able to perform multiple tasks. For example, spelling and grammar can be checked when user is giving input to the Microsoft Word. That is while user is giving keystrokes. So there is one thread which is created to check spelling and grammar of user input. User input is taken by another thread. Again a thread may be created to load images from the hard dive. Again there may be a thread to which keep periodic automatic backup of the file which is continuously editing. So here we can see there is a single process that is Microsoft Word along with four different threads and as this process contains multiple thread, this process is able to perform multiple tasks at a time as shown here. Another example of multi-threading is client server architecture. Here the client sends request to the server. Server creates thread to process that request and resumes for listening to the new request from another client. The client, same client or another client may give request again back to the server and server may create another thread to process that request. In this way the multi-threaded server architecture can be developed and here this server performs multiple tasks by creating multiple threads. Now these are nothing but difference between processes and threads which you can see later on. Now just pause your video and think about that why process is called as a heavyweight process and why thread is called as a lightweight process. Up till now whatever we studied about thread and process just think why process is heavyweight and why thread is lightweight. Pause your video and write down your answer. Now as we can see individual processes are independent of each other. Processes have independent data and code segment while thread can share code and data segment if they belong to same process. All the different processes are created separately by operating system while thread has their own library. Then process require more resources than thread because process has its own address space while thread share the address space of the process. Process require more time for creation and termination and due to all these reasons process is called as a heavyweight process while thread is called as a lightweight process. Now what are different benefits of thread? Thread takes less time to create a new thread than a process. It also takes less time to terminate a thread than a process. Switching between two threads takes less time than switching between the processes and threads enhance efficiency in communication between programs. Due to these benefits threads are used in the processes. Now where this thread can be used in a single user system? So application of thread in single user system is first is foreground and background work. Now consider a spreadsheet program. Okay here one thread can be created to display menu and read input from the user while the another thread may be used to execute user commands and update a spreadsheet. Second application is asynchronous processing. Asynchronous processing means asynchronous elements in the program can be implemented as a thread. For example a thread may be created to take periodic backup of a file means writing the data of that file periodically into the disk writing the data which is present in RAM into the disk and that thread the sole job of thread is after a particular period of time it has to write the whole content present in that RAM to the disk. So this asynchronous processing can also be done using thread. Next is speed of execution. Now in multi threaded environment the processes can perform multiple tasks at a time. So one process may read the input from the user while another thread is used for spell checking as we have seen. So it automatically increases the speed of execution. Next is modular program structure. In this case the program that involves a variety of activities or variety of sources and destination of input and output may be easier to design and implement using the threads. So let us see thread execution states. The key state for a thread are running, ready and block. So these are these states are also associated with process along with suspended state. But suspended state is not it is not a point here because it is a state of that process. But along with this state four basic operations of thread are there. First is spawn. When a process is created automatically one thread is created for that process. That thread can create sub thread or another thread for more tasks. Next block that when a thread needs to wait for an event it will block. When it blocks it saves its user registers program counter and stack pointer. The processor may now turn to execution of another ready thread in the same or different process. Next is unblock. Now unblock means whenever thread is blocked for us for some event and that event occurs then automatically thread becomes unblocked and it changes its state from block to ready state and it can be used for execution. Next is finish. When a thread complete its execution its register context and stacks are deallocated. So these are nothing but different states of the process along with four operations which is associated with the thread. Now thread synchronization. It is necessary to synchronize the activity of various thread because that thread belongs to same process. So synchronization must be there. All thread of the process share the same address space and other resources. Any alteration of a resource by one thread affect the other thread in the same process. For example suppose two different thread want to add an element in doubly linked list at the same time. Then there may be loss of that element and the doubly linked list may be malformed. So such situation should not occur. So there must be proper synchronization between thread. So this is all about threads. These are the references. Thank you.