Chapter 4-Threads.ppt
Document Details
Uploaded by GracefulMossAgate
Full Transcript
Chapter 4: Threads & Concurrency Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 Chapter 4: Threads Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues Operating System Examples Operat...
Chapter 4: Threads & Concurrency Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 Chapter 4: Threads Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues Operating System Examples Operating System Concepts – 10th Edition 4.2 Silberschatz, Galvin and Gagne ©2018 Thread A Thread is a basic unit of CPU utilization. Thread consists of a thread ID, a program counter, a register set, and a stack. Thread shares with other threads of the same process its code section, data section, and other operating system resources, such as files and signals. Traditional (heavyweight) process has a single thread of control. A process with multiple threads of control can perform more than one task at a time. Most software applications that run on modern computers are multithreaded. Operating System Concepts – 10th Edition 4.3 Silberschatz, Galvin and Gagne ©2018 Single and Multithreaded Processes • Each process has its own memory space • Threads within the same process share the address space of that process • A thread has an ID, Prog. Counter, set of registers, and stack BUT shares code section, data section, and resources like files with the other threads of the same process Thread –task Register- storage Operating System Concepts – 10th Edition 4.4 Silberschatz, Galvin and Gagne ©2018 Multithreading The ability of an OS to support multiple, concurrent paths of execution within a single process. Operating System Concepts – 10th Edition 4.5 Silberschatz, Galvin and Gagne ©2018 Motivation Most modern applications are multithreaded Threads run within application Multithreading increases concurrency within a process Multiple tasks with the application can be implemented by separate threads Update display Fetch data Spell checking Answer a network request Process creation is heavy-weight while thread creation is light-weight Can simplify code, increase efficiency Kernels are generally multithreaded Operating System Concepts – 10th Edition 4.6 Silberschatz, Galvin and Gagne ©2018 Benefits The benefits of multithreaded programming can be devided into four major categories. Responsiveness – Multithreading an interactive applications may allow a program to continue execution even if part of it is blocked, especially important for user interfaces Resource Sharing – Threads share resources of process, easier than shared memory or message passing Economy – Allocating memory and resources for process creation is costly. As threads share the resources of the process to which they belong, it is more economical to create and context switch threads. Scalability – The benefits of multithreading can be even greater in a multiprocessor architecture, where threads may be running in parallel on different processing cores. Operating System Concepts – 10th Edition 4.7 Silberschatz, Galvin and Gagne ©2018 Multicore Programming Multicore or multiprocessor systems putting pressure on programmers, challenges include: Identifying tasks: This involves examining applications to find areas that can be divided into separate, concurrent tasks. Ideally, tasks are independent of one another and thus can run in parallel on individual cores Balance: While identifying tasks that can run in parallel, programmers must also ensure that the tasks perform equal work of equal value. Data splitting: Just as applications are divided into separate tasks, the data accessed and manipulated by the task must be divided to run on separate cores. Data dependency: The data accessed by the tasks must be examined for dependencies between two or ore tasks. Testing and debugging: When a program is running in parallel on multiple cores, many different execution paths are possible. Operating System Concepts – 10th Edition 4.8 Silberschatz, Galvin and Gagne ©2018 Multicore Programming (Cont.) Types of parallelism Data parallelism – distributes subsets of the same data across multiple cores, same operation on each Task parallelism – distributing threads (not data) across cores, each thread performing a unique operation Different threads may be operating on the same data, or they may be operating on different data. Data parallelism involves the distribution of data across multiple cores and task parallelism on the distribution of tasks across multiple cores. Operating System Concepts – 10th Edition 4.9 Silberschatz, Galvin and Gagne ©2018 Concurrency vs. Parallelism Parallelism implies a system can perform more than one task simultaneously Concurrency supports more than one task making progress Single processor / core, scheduler providing concurrency Concurrent execution on single-core system: Parallelism on a multi-core system: Operating System Concepts – 10th Edition 4.10 Silberschatz, Galvin and Gagne ©2018 I/O and CPU Bound Processes/Threads CPU Bound Processes/threads which have long bursts of CPU time, followed by short bursts of I/O I/O Bound processes/Threads which have long bursts of I/O, followed by short bursts of CPU Operating System Concepts – 10th Edition 4.11 Silberschatz, Galvin and Gagne ©2018 User Threads and Kernel Threads Support for threads may be provided either at the user level for user threads or by the kernel for kernel threads. User threads - Management done by user-level threads library. User threads are supported above the kernel and managed without kernel support. Three primary thread libraries: – POSIX Pthreads – Windows threads – Java threads Kernel threads - Supported and managed by the operating system (Kernel). Examples – virtually all general purpose operating systems, including: – Windows – Solaris – Linux – Tru64 UNIX – Mac OS X Operating System Concepts – 10th Edition 4.12 Silberschatz, Galvin and Gagne ©2018 Multithreading Models Mapping user threads to kernel threads: Many-to-One One-to-One Many-to-Many Operating System Concepts – 10th Edition 4.13 Silberschatz, Galvin and Gagne ©2018 Many-to-One Many user-level threads mapped to single kernel thread Efficient because thread management is done by the thread library in user space. The entire process will block if a thread makes a blocking system call. Multiple threads may not run in parallel on muticore system because only one may be in kernel at a time Few systems currently use this model, because it does not benefit from muticore systems Examples: Solaris Green Threads GNU Portable Threads Operating System Concepts – 10th Edition 4.14 Silberschatz, Galvin and Gagne ©2018 One-to-One Each user-level thread maps to kernel thread More concurrency than many-to-one model by allowing another thread to run when a thread makes a blocking system call. Allows multiple threads to run in parallel on multiprocessors. Number of threads per process sometimes restricted due to overhead Examples Windows Linux Solaris 9 and later Operating System Concepts – 10th Edition 4.15 Silberschatz, Galvin and Gagne ©2018 Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows with the ThreadFiber package Operating System Concepts – 10th Edition 4.16 Silberschatz, Galvin and Gagne ©2018 Two-level Model Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier Operating System Concepts – 10th Edition 4.17 Silberschatz, Galvin and Gagne ©2018 Question The ____ multithreading model multiplexes many user-level threads to a smaller or equal number of kernel threads A. many-to-one model B. one-to-one model C.many-to-many model D.many-to-some model Operating System Concepts – 10th Edition 4.18 Silberschatz, Galvin and Gagne ©2018 d Thread: S.NO Process Thread 1. Process means any program is in execution. Thread means a segment of a process. 2. The process takes more time to terminate. The thread takes less time to terminate. 3. It takes more time for creation. It takes less time for creation. 4. It also takes more time for context switching. It takes less time for context switching. 5. The process is less efficient in terms of communication. Thread is more efficient in terms of communication. 6. Multiprogramming holds the concepts of multi-process. We don’t need multi programs in action for multiple threads because a single process consists of multiple threads. 7. The process is isolated. Threads share memory. 8. The process is called the heavyweight process. A Thread is lightweight as each thread in a process shares code, data, and resources. 9. Process switching uses an interface in an operating system. Thread switching does not require calling an operating system and causes an interrupt to the kernel. 10. If one process is blocked then it will not affect the execution of other processes If a user-level thread is blocked, then all other user-level threads are blocked. 11. The process has its own Process Control Block, Stack, and Address Space. Thread has Parents’ PCB, its own Thread Control Block, and Stack and common Address space. 12. Changes to the parent process do not affect child processes. Since all threads of the same process share address space and other resources so any changes to the main thread may affect the behavior of the other threads of the process. 13. A system call is involved in it. No system call is involved, it is created using APIs. 14. The process does not share data with each other. Threads share data with each other. Operating System Concepts – 10th Edition 4.19 Silberschatz, Galvin and Gagne ©2018 End of Chapter 4 Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018