Download presentation
Presentation is loading. Please wait.
Published byGertrude Miller Modified over 9 years ago
1
Chapter 4: Threads
2
4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads
3
4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 3 Thread: Introduction Each process has Own Address Space Single thread of control A process model has two concepts: Resource grouping Execution Sometimes it is useful to separate them
4
4.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 4 Unit of Resource Ownership A process has an Address space Open files Child processes Accounting information Signal handlers Etc If these are put together in a form of a process, can be managed more easily
5
4.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 5 Unit of Dispatching Path of execution Program counter: which instruction is running Registers: holds current working variables Stack: Contains the execution history, with one entry for each procedure called but not yet returned State Processes are used to group resources together Threads are the entities scheduled for execution on the CPU Threads are also called lightweight process
6
4.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 6 Its better to distinguish between the two concepts Address space/Global Variables Open files Child processes Accounting info Signal handlers Program counter Registers Stack State Address space/Global Variables Open files Child processes Accounting info Signal handlers Program counter Registers Stack State Program counter Registers Stack State Split Program counter Registers Stack State Unit of Resource Unit of Dispatch In case of multiple threads per process Share Light weight processes Heavy weight process
7
4.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads allow you to multiplex which resources? 1. CPU 2. Memory 3. PCBs 4. Open files 5. User authentication structures
8
4.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 8 Thread A thread is a basic unit of CPU utilization. It consist of A thread ID A program Counter A register Set A stack Threads share something with its peer threads (all the other5 threads in this particular task) the things that it share are Its code section Its data section Any OS resources, available for the task. The first thread starts execution with int main(int argc, char *argv[]) The threads appear to the Scheduling part of an OS just like any other process Allow multiple execution paths in the same process environment
9
4.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads vs. Processes Threads n A thread has no data segment or heap n A thread cannot live on its own, it must live within a process n There can be more than one thread in a process, the first thread calls main & has the process’s stack n Inexpensive creation n Inexpensive context switching n If a thread dies, its stack is reclaimed n Inter-thread communication via memory. Processes A process has code/data/heap & other segments There must be at least one thread in a process Threads within a process share code/data/heap, share I/O, but each has its own stack & registers Expensive creation Expensive context switching If a process dies, its resources are reclaimed & all threads die Inter-process communication via OS and data copying.
10
4.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 10 Process Vs. Threads (a) Three threads, each running in a separate address space (b) Three threads, sharing the same address space
11
4.11 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Context switch time for which entity is greater? 1. Process 2. Thread
12
4.12 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 12 The Thread Model Each thread has its own stack
13
4.13 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Single and Multithreaded Processes A traditional heavy weight process is same as task with one thread. It has a single thread of control. If a process is multi thread, then that means more than one part of the thread is executing at one time. Multi threading can be useful in programs such as web browsers where you can wish to download a file, view an animation and print something at the same time.
14
4.14 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Single and Multithreaded Processes
15
4.15 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Implementing Threads Processes define an address space; threads share the address space Process Control Block (PCB) contains process-specific information Owner, PID, heap pointer, priority, active thread, and pointers to thread information Thread Control Block (TCB) contains thread-specific information Stack pointer, PC, thread state (running, …), register values, a pointer to PCB, … Code Initialized data Heap DLL’s mapped segments Process’s address space Stack – thread1 PC SP State Registers … PC SP State Registers … TCB for Thread1 Stack – thread2 PC SP State Registers … PC SP State Registers … TCB for Thread2
16
4.16 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads’ Life Cycle Threads (just like processes) go through a sequence of start, ready, running, waiting, and done states Running Ready Waiting Start Done
17
4.17 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Benefits Of Multi-Threading Responsiveness Multithreading increase the responsiveness.As the process consist of more than one thread, if one thread block or busy in lengthy calculation, some other thread still executing of the process. So the user get response from executing process. Resource Sharing All threads, which belongs to one process, share the memory and resources of that process. Secondly it allow the application to have several different threads within the same address space.
18
4.18 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Benefits Of Multi-Threading Economy Allocation of memory and resources or process creation is costly. All threads of a process share the resources of that process so it is more economical to create and context switch the thread. Utilization of Multi processor Architectures MP architecture allows the facility of parallel processing, which is most efficient way of processing. A single process can run on one CPU even if we have more processors. Multi-threading on MP system increase the concurrency. If a process is dividing into multiple threads, these threads can execute simultaneously on different processors.
19
4.19 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Types of Threads There are two types of threads Kernel Threads User Threads
20
4.20 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts User Threads User level threads are not seen by operating system and also very fast (switching from one thread to another thread in a single process does not require context switch since same process is still executing). However, if the thread that is currently executing blocks, the rest of the process may also blocked( if OS is using only one single kernel thread for this process i.e. the thread that kernel sees is same as blocked thread, hence kernel assume that whole process is blocked). Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads
21
4.21 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Kernel Threads Kernel Supported threads are seen by operating system and must be scheduled by the operating system. One multi thread may have multiple kernel threads. Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X
22
4.22 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts User-Level vs. Kernel Threads User-Level Managed by application Kernel not aware of thread Context switching cheap Create as many as needed Must be used with care Kernel-Level Managed by kernel Consumes kernel resources Context switching expensive Number limited by kernel resources Simpler to use Key issue: kernel threads provide virtual processors to user-level threads, but if all of kthreads block, then all user-level threads will block even if the program logic allows them to proceed
23
4.23 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing Library entirely in user space with no kernel support. All the code and data structure exists in user space. This means that invoking of a function in a the library results in a local function call in user space and not a system call. Kernel-level library supported by the OS. In this case code and data structures for the library exits in kernel space. Invoking a function in the API of library typically results in a system call to a kernel.
24
4.24 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Libraries Three main thread libraries are used in today POSIX Pthreads Win32 Java
25
4.25 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Multithreading Models Many-to-One One-to-One Many-to-Many
26
4.26 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Many-to-One Many user-level threads mapped to single kernel thread. It is efficient because it is implemented in user space. A process using this model blocked entirely if a thread makes a blocking system call. Only one thread can access the kernel at a time so it can not be run in parallel on multiprocessor. Examples: Solaris Green Threads GNU Portable Threads("Genuinely Not Unix" ; GNU is an operating system composed of free software)
27
4.27 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Many-to-One Model
28
4.28 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts One-to-One Each user-level thread maps to kernel thread. It provides more concurrency because it allows another thread to execute when threads invoke the blocking system call. It facilitates the parallelism in multiprocessor systems. Each user thread requires a kernel thread, which may affect the performance of the system. Creation of threads in this model is restricted to certain number. Examples Windows NT/XP/2000 Linux Solaris 9 and later
29
4.29 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts One-to-one Model
30
4.30 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 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 Number of kernel threads may be specific to a either a particular application or a particular machine. The user can create any number of threads and corresponding kernel level threads can run in parallel on multiprocessor. When a thread makes a blocking system call, the kernel can execute another thread. Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package
31
4.31 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Many-to-Many Model
32
4.32 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Multithreading Models: Comparison The many-to-one model allows the developer to create as many user threads as he/she wishes, but true concurrency can not be achieved because only one kernel thread can be scheduled for execution at a time The one-to-one model allows more concurrence, but the developer has to be careful not to create too many threads within an application The many-to-many model does not have these disadvantages and limitations: developers can create as many user threads as necessary, and the corresponding kernel threads can run in parallel on a multiprocessor
33
4.33 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 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
34
4.34 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Two-level Model LWP user-level threads LWP Combination of one-to-one + “strict” many-to-many models Supports both bound and unbound threads –Bound threads - permanently mapped to a single, dedicated LWP –Unbound threads - may move among LWPs in set Thread creation, scheduling, synchronization done in user space Flexible approach, “best of both worlds” Used in Solaris implementation of Pthreads and several other Unix implementations (IRIX, HP-UX)
35
4.35 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Two-level Model
36
4.36 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X)
37
4.37 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Java Threads Java threads are managed by the JVM Java threads may be created by: Extending Thread class Implementing the Runnable interface
38
4.38 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Java Thread States
39
4.39 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threading Issues Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations
40
4.40 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Semantics of fork() and exec() As the fork() system call is used to create a separate, duplicate process. The semantics of the fork() and exec() system calls change in a multithreaded program. If one thread in a program calls the fork(), does the new process duplicate all the threads or is the new process single threaded?
41
4.41 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads? Two versions of fork() in UNIX:, one that duplicates all threads and another that duplicates only the thread that invoked the fork() system call. If a thread invokes the exec() system call, the program specified in the parameter to exec() will replace the entire process – including all threads. If exec() is called immediately after forking, then duplicating all threads is unnecessary, as the program specified in the parameters to exec() will replace the process. In this instance, duplicating only the calling thread is appropriate. If the separate process does not call exec() after forking, the separate process should duplicate all threads.
42
4.42 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Cancellation Thread cancellation is the task of terminating a thread before it has finished. For example, if multiple threads are concurrently searching through a database and one thread return the result, the remaining threads might be canceled. A thread that is to be often canceled is referred as the target thread. Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled, allowing it an opportunity to terminate itself in an orderly fashion.
43
4.43 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Problem in Thread Cancellation The difficulty with cancellation occurs in a situations where resources have been allocated to a cancel thread Where thread has been cancelled in the midst of updating data it share with other threads. This becomes especially difficult with asynchronous cancellation. Often operating system will reclaim system resources from the canceled thread but will not reclaim all the resources. Therefore, canceling a thread asynchronously may not free a necessary system-wide resource.
44
4.44 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Signal Handling A signal is used in a UNIX system to notify a process that a particular event has occurred. Signal may be received either asynchronously or synchronously. All Signal follow the same pattern. A signal handler is used to process signals Signal is generated by particular event Signal is delivered to a process Signal is handled
45
4.45 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Signal Handling Examples of synchronous signals include illegal memory access and division by 0. if a running program perform either of these operations a signal is generated. Synchronous signals are delivered to the same process that performed the operation that caused the signals. When a signal is generated by some event external to a running process, that process receive the signal asynchronously. Examples of such signals include terminating a process with specific key strokes (such as ) and having a timer expire. Typically, asynchronous signal is sent to another process.
46
4.46 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Signal Handling in UNIX Every signal may be handled by one of two possible handlers: A default signal handler that is run by the kernel when handling that signal. A user-defined signal handler that is called to handle a signal.
47
4.47 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Signal Handling Delivering the signal in multithreaded programs is more complicated, where a process may have several threads. Following Options exist: 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.
48
4.48 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Pools The general idea is to create a number of threads at process startup and place them into a pool where they sit and wait for work Advantages: Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool
49
4.49 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Specific Data Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool)
50
4.50 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Scheduler Activations A final issue to be considered with multithreaded programs concerns with communication between kernel level and user level thread library. Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application. Many system implementing either M:M or Two level model place an intermediate data structure between user and kernel threads. This data structure is typically known as light weight process or (LWP).
51
4.51 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Scheduler Activations One scheme for communication between user thread library and kernel thread library is known as Scheduler Activation. It works as follow: The kernel provides an application with a set of virtual processor (LWPs), and the application can schedule user threads onto an available virtual processor. The kernel must inform an application about certain events. This procedure is known as upcalls - a communication mechanism from the kernel to the thread library. Upcalls are handled by thread library with an upcall handler. Upcall handler must run on virtual processor. This communication allows an application to maintain the correct number of kernel threads.
52
4.52 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Operating System Examples Windows XP Threads Linux Thread
53
4.53 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Windows XP Threads Implements the one-to-one mapping, kernel-level Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block)
54
4.54 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Windows XP Threads
55
4.55 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process)
56
4.56 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Linux Threads
57
4.57 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts 57 Thread Usage Less time to create a new thread than a process the newly created thread uses the current process address space no resources attached to them Less time to terminate a thread than a process. Less time to switch between two threads within the same process, because the newly created thread uses the current process address space. Less communication overheads threads share everything: address space, in particular. So, data produced by one thread is immediately available to all the other threads Performance gain Substantial Computing and Substantial Input/output Useful on systems with multiple processors
58
End of Chapter 4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.