Operating Systems CS 340, Chapter 3 Threads - Princess Nora University PDF
Document Details
Uploaded by ProactiveConnemara2555
Princess Nourah Bint Abdulrahman University
Tags
Summary
This document from Princess Nora University's Computer Science Department covers Operating Systems CS 340, chapter 3: Threads. It details the motivation, benefits, and overview of threads in computer systems. The document includes explanations and diagrams related to single- and multi-threaded processes.
Full Transcript
Princess Nora University Computer Sciences Department Operating Systems CS 340 1 Chapter-3 Threads (covered in chapter 4 of textbook) Chapter 3: Threads 1. Motivation 2. Benefits 3 OBJECTIVE: To introduce the notion of a thread — a fundamental uni...
Princess Nora University Computer Sciences Department Operating Systems CS 340 1 Chapter-3 Threads (covered in chapter 4 of textbook) Chapter 3: Threads 1. Motivation 2. Benefits 3 OBJECTIVE: To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems. 4 Overview Thread is a basic unit of CPU utilization. i.e. it is the smallest sequence of programmed instructions that can be managed independently. Threads provide a way to improve application performance through parallelism. 5 Overview (cont..) Thread comprises: 1. ID. 2. Current activity (copy of program counter and registers values) 3. Stack. A thread shares with other threads belonging to the same process its: 1. Code section (/text section). 2. Data section. 3. Other operating system resources such as open files. 6 Single and Multithreaded Processes (cont..) Shared CA 1 CA 1 CA 2 CA 3 Thread Thread 1 Thread 2 Thread 3 7 Single and Multithreaded Processes Single-threaded Process Multithreaded Process Also known as traditional process Also known as lightweight process. and heavyweight process. A process has a single thread of A process has multiple threads of control. control. It can perform one task at a time. It can perform more than one task at a time. Each thread belongs to exactly one process and NO thread can exist outside a process There is one program counter, and Every thread have their own current one sequence of instructions that activity, stack, but sharing common can be carried out at any given code, data, and certain structures such as time. open files. 8 Motivation Most software packages that run on modern desktop PCs are multithreaded. An application typically is implemented as a separate process with several threads of control. E.G. (1) : A word processor may have : o a thread for displaying graphics, o a thread for responding to keystrokes from the user, o a third thread for spelling and grammar checking 9 Motivation (cont..) E.G. (2) : A web server – A web server accepts client requests for web pages, images, sound, etc. A busy web server may have several (perhaps thousands of) users concurrently accessing it. Solution 1 (Process creation): Ifthe web-server process is single-threaded, the server will create a separate process to service each user request. Process creation is time consuming and resource intensive (high overhead). Solution 2 (Thread creation): If the web-server process is multithreaded, the server will create only single process and create separate thread to service each user request (less overhead). 10 Motivation (cont..) So, Ifthe web server ran as a traditional single-threaded process, it would be able to service only one client at a time user sequentially and a user might have to wait a very long time for its request to be serviced. Multiple threads allow for multiple requests to be satisfied simultaneously. REMEMBER THAT: Creating a new thread belongs to an existing process is less overhead of creating a new process. Most operating system kernels are now multithreaded. Several threads operate in the kernel, and each thread performs a specific task, such as managing devices, managing memory, or interrupt handling. 11 Benefits Benefits of multithreaded programming: 1. Economy (in time): Creating and managing threads is much faster than performing the same tasks for processes. Allocating memory and resources for each process is costly. Threads share the resources of the process to which they belong, Therefore it is more economical to create and context-switch threads. 2. Responsiveness: One thread may provide rapid response while other threads are blocked or slowed down doing intensive calculations increasing responsiveness. Useful for interactive applications. 12 Benefits (cont..) 3. Resource Sharing: Processes may only share resources (e.g. memory) through techniques (e.g. shared memory or message passing) …Such techniques must be explicitly arranged by the programmer. By default (design), threads share common code, data, and other resources, which allows multiple tasks to be performed simultaneously in a single address space. 4. Scalability: The benefits of multithreading can be greatly increased in a multiprocessor architecture, where threads may be running in parallel on different processing cores. A single-threaded process can only run on one processor, regardless how many CPUs are available. 13 Thank you End of Chapter 3