Download presentation
1
Operating System Concepts
2
Lecture Contents Motivation for threads Thread Concept
Similarity and Difference between Process and Thread Advantages of threads Implementation of Multithreading Multithreading Models Thread Library Concept Multithreading Issues Case Study *Please follow Silberchatz, Galvin and Gagne (Chapter 4), 7th edition
3
Issues with Processes So far we have discussed Process, Creation of new process concept and its execution; However there are some issues with processes. Processes are EXPENSIVE ! (E.g. resources that are to be allocated with every process) Hardware resources (memory), kernel resources (PCB) Link establishment for IPC (unrelated processes/related processes) Concurrency Motivation ? .. A mechanism that provides an alternative of a process (so that a new process is not needed)
4
THREAD Thread- A piece of code in a process which runs concurrently with execution of other piece of codes within the same process (same address space) No need to create a new process Resources that thread need to update/allocate privately: Thread ID CPU Context (Program Counter (PC), register set) Stack Priority Kernel maintained external variable errno, (in order to tell the programmer that an error has occurred)
5
Threads within a same process share…
Multiple and concurrent threads within a process share: Code Data Open File descriptor table PCB (Process Control Block) For a thread creation, only need to allocate CPU context and Stack !! Threads are in the same process address space
6
Single and Multithreaded Processes
7
How processes are similar to threads or different ?
A thread can be in a state, similar to process (new, ready, running, waiting, terminate) Threads can also create other threads. Difference Processes execute only within their own address space, whereas threads of same process execute within address space of one major process Automatic protection mechanism for processes but what about threads… ? Synchronization issues ?
8
Advantages of thread Responsiveness - allow a program to continue running even if part of it is blocked (e.g. In a browser, multiple threads are working simultaneously to increase responsiveness) Resource sharing - threads share the memory and resources of the process (allows an application to have several threads of activity within the same address space) Economy - it is more economical to create and context-switch threads (because they share resources of the process to which they belong) In Solaris a process is thirty times slower to create and five times slower to context-switch Scheduling a thread is easy (just save the CPU context) Scalability (utilizing multiprocessor architectures)
9
Some other things.. The main reason for having threads is that in many applications, multiple activities are going on at once. Some of these may block from time to time. By decomposing such an application into multiple sequential threads that run in parallel, the programming model becomes simpler.
10
How multithreading is implemented ?
There are two techniques for implementing multithreading User-Level threads Thread creation and management is done by the application using a thread library Any application can be programmed to be multithreaded by using a thread library which is a package of application level functions. The kernel is not aware of existence of threads Kernel Level threads Thread management and implementation is done by operating system Kernel provides a way for user processes to create multiple threads
11
Difference between the two techniques…
Advantages of User level threads User-level threads can be created in a system that doesn’t provide multithreading facility Management and scheduling of thread is done by threading library in user space, so user level threads don’t invoke kernel for doing these tasks User level threads can run on any OS, no changes are required to do on underlying kernel (Portability) Disadvantages Using User Level threading strategy, a multithreaded application cannot take advantage of multiprocessor system. (different threads of a same process cannot run on different CPU’s because OS considers a process a single threaded program) If a thread is blocked, all the threads within a process gets blocked.
12
Advantages of Kernel level threads
Since kernel threads use the kernel scheduler, different kernel threads can run on different CPUs (taking advantage of multiprocessor system) If a thread within a process gets blocked (e.g. waiting for an I/O), the scheduler can schedule another thread Disadvantages Frequent invoking of kernel for management of threads More expensive to create than user threads
15
Examples User Threads Kernel Threads
Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads Kernel Threads Supported by the Kernel Examples Windows XP/2000 Solaris Linux POSIX: an acronym for Portable Operating System Interface, is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines the application programming interface (API), along with command line shells and utility interfaces, for software compatibility with variants of Unix and other operating systems. This standard defines a standard operating system interface and environment, including a command interpreter (or "shell"), and common utility programs to support applications portability at the source code level. Pthreads: Pthreads refers to the POSIX standard (IEEE c) defining an API for thread creation and synchronization. This is a specification for thread behavior, not an implementation. Threads have been standardized by the IEEE Technical Committee on Operating Systems, POSIX (Portable Operating System Interface). This standard allows users to write portable multi-thread programs.
16
Multithreading Models
Based on the number of kernel threads allocated correspondingly to the user level threads, we have three multithreading models Many-to-one Model One-to-one Model Many-to-Many Model
17
Many-to-One Model Many user-level threads mapped to single kernel thread Thread management is done by the thread library in user space, so it is efficient. Drawback - the entire process will block if a thread makes a blocking system call. Examples: Solaris Green Threads GNU Portable Threads Kernel is scheduling a single process
18
One-to-One Model Each user-level thread maps to kernel thread
True concurrency - compared to many-to-one model since another thread will be able to run when a thread makes a blocking system call. Allows multiple threads to run in parallel on multiprocessors. Drawback: Creating a user thread requires creating a corresponding kernel thread. (Overhead) Examples Windows NT/XP/2000 Linux Solaris 9 and later
19
Many-to-Many Model (Hybrid Model)
Multiple user level threads multiplexed over a smaller or equal number of kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris 2 Windows NT/2000 with the ThreadFiber package Which of three models solaris model is built?
20
Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing Library entirely in user space Kernel-level library supported by the OS -> invoking a function in the library results with a system call
21
Pthreads A POSIX standard (IEEE c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library (i.e. the OS designers) This standard has been defined to make the writing of PORTABLE threaded program possible This standard defines over 60 function calls May be provided either as user-level or kernel-level. Common in UNIX operating systems (Solaris, Linux, Mac OS X)
22
Threading Issues Thread cancellation of target thread
Asynchronous or deferred Signal handling Thread pools Thread-specific data Scheduler activations
23
Thread Cancellation Terminating a thread before it has finished
Example: multiple threads concurrently searching through a database and one thread returns the result Stopping a webpage from loading any further Two general approaches: Asynchronous cancellation terminates the target thread immediately – troublesome if it’s in middle of updating some data Deferred cancellation allows the target thread to periodically check if it should be cancelled – checking a flag. Can only be cancelled at defined points called cancellation points
24
Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred Example: illegal memory access, division by zero A signal handler is used to process signals Signal is generated by particular event Signal is delivered to a process Signal is handled In a multithreaded process, where should a signal be delivered? Options: Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process
25
Thread Pools Reason for the need of thread pool:
Time consumed to create a new thread and destroy the previous ones – leads to slow performance Unlimited threads could exhaust system resources, such as CPU time or memory Solution: Thread Pools Create a number of threads at process startup and place them in a pool where they await work Advantages: Usually slightly faster to service a request with an existing thread than create a new thread Also allows an application to specify the number of threads it requires
27
Thread Specific Data Scheduler activation
Allows each thread to have its own copy of specific data Example, in a transaction-processing system, service each transaction in a separate thread. Furthermore, each transaction might be assigned a unique identifier. To associate each thread with its unique identifier, use thread-specific data. Scheduler activation User-Level Threads: If a single user-level thread blocks, the operating system blocks the entire multithreaded process. Another limitation to a many-to-one thread mapping is that a process's threads cannot simultaneously execute on multiple processors. Scheduler activations attempt to address these limitations to user-level threads. A scheduler activation is a kernel thread that can notify a user-level threading library of events (e.g., a thread has blocked or a processor is available). This type of kernel thread is called a "scheduler activation,“ because the user-level threading library can perform thread-scheduling operations when "activated" by an event notification, sometimes called an upcall.
28
Case Study Windows XP Linux
29
Windows XP
30
Windows XP Threads
31
Linux Threads Linux refers to them as tasks rather than threads
Thread creation is done through clone() system call clone() accepts arguments that specify which resources to share with the child process
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.