Download presentation
Presentation is loading. Please wait.
Published byAngelica Gray Modified over 9 years ago
1
Chapter 4: Threads
2
4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Outline n Overview n Multithreading Models n Thread Libraries n Threading Issues n Operating System Examples l Windows XP Threads l Linux Threads
3
4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Objectives n To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems n To discuss the APIs for the Pthreads, Win32, and Java thread libraries n To examine issues related to multithreaded programming
4
4.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Single and Multithreaded Processes
5
4.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Benefits n Responsiveness n Resource Sharing n Economy n Utilization of MP Architectures
6
4.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Multicore Programming n Multicore systems putting pressure on programmers, challenges include l Dividing activities l Balance l Data splitting l Data dependency l Testing and debugging
7
4.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Multithreaded Server Architecture
8
4.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Concurrent Execution on a Single-core System
9
4.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Parallel Execution on a Multicore System
10
4.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 User Threads n Thread management done by user-level threads library n Three primary thread libraries: l POSIX Pthreads l Win32 threads l Java threads
11
4.11 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Kernel Threads n Supported by the Kernel n Examples l Windows XP/2000 l Solaris l Linux l Tru64 UNIX l Mac OS X
12
4.12 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Multithreading Models n Relationships between user threads and kernel threads l Many-to-One l One-to-One l Many-to-Many
13
4.13 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Many-to-One n Many user-level threads mapped to single kernel thread l Thread management by thread library in user space efficient l No concurrency l Unable to run in MP n Examples: l Solaris Green Threads l GNU Portable Threads
14
4.14 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Many-to-One Model
15
4.15 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 One-to-One n Each user-level thread maps to kernel thread l More concurrency l Able to run in MP l Overhead of creating kernel threads n Examples l Windows NT/XP/2000 l Linux l Solaris 9 and later
16
4.16 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 One-to-one Model
17
4.17 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Many-to-Many Model n Allows many user level threads to be mapped to many kernel threads l Concurrency l Can run in MP l Developers can create as many user threads as necessary n Examples l Solaris prior to version 9 l Windows NT/2000 with the ThreadFiber package
18
4.18 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Many-to-Many Model
19
4.19 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Two-level Model n Similar to M:M, except that it allows a user thread to be bound to kernel thread n Examples l IRIX l HP-UX l Tru64 UNIX l Solaris 8 and earlier
20
4.20 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Two-level Model
21
4.21 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread Libraries n Thread library provides programmer with API for creating and managing threads n Two primary ways of implementing l Library entirely in user space l Kernel-level library supported by the OS
22
4.22 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Pthreads n May be provided either as user-level or kernel-level n A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization n API specifies behavior of the thread library, implementation is up to development of the library n Common in UNIX operating systems (Solaris, Linux, Mac OS X)
23
4.23 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Java Threads n Java threads are managed by the JVM n Typically implemented using the threads model provided by underlying OS n Java threads may be created by: l Extending Thread class l Implementing the Runnable interface
24
4.24 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threading Issues n Semantics of fork() and exec() system calls n Thread cancellation n Signal handling n Thread pools n Thread specific data n Scheduler activations
25
4.25 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Semantics of fork() and exec() n Does fork() duplicate only the calling thread or all threads? l Which of the two versions of fork() to use depends on the application Exec() immediately after fork() No exec() after fork()
26
4.26 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread Cancellation n Terminating a thread before it has finished n Two general approaches: l Asynchronous cancellation terminates the target thread immediately l Deferred cancellation allows the target thread to periodically check if it should be cancelled n Problems when resources have been allocated to a canceled thread or while in the midst of updating data sharing with other threads
27
4.27 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Signal Handling n Signals are used in UNIX systems to notify a process that a particular event has occurred l A signal handler is used to process signals 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled Synchronous vs. asynchronous signals Default vs. user-defined handlers
28
4.28 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 n Options to deliver signals in multithreaded programs: l Deliver the signal to the thread to which the signal applies l Deliver the signal to every thread in the process l Deliver the signal to certain threads in the process l Assign a specific thread to receive all signals for the process n ex: l kill(aid_t aid, int signal) l pthread_kill(pthread_t tid, int signal)
29
4.29 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread Pools n Create a number of threads in a pool where they await work n Advantages: l Usually slightly faster to service a request with an existing thread than create a new thread l Allows the number of threads in the application(s) to be bound to the size of the pool n Ex: l QueueUserWorkItem(LPTHREAD_START_ROUTI NE Function, PVOID Param, ULONG Flags) in Win32 API l java.util.concurrent package in Java 1.5
30
4.30 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread-Specific Data n Allows each thread to have its own copy of data n Useful when you do not have control over the thread creation process (i.e., when using a thread pool)
31
4.31 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Scheduler Activations n Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application n Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library l An intermediate data structure called LWP (Lightweight Process) between user thread and kernel thread n This communication allows an application to maintain the correct number of kernel threads
32
4.32 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Operating System Examples n Windows XP Threads n Linux Threads
33
4.33 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Windows XP Threads n Implements the one-to-one mapping n Each thread contains l A thread id l Register set l Separate user and kernel stacks l Private data storage area n The primary data structures of a thread include: l ETHREAD (executive thread block) l KTHREAD (kernel thread block) l TEB (thread environment block) Context of the threads
34
4.34 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Windows XP Threads
35
4.35 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Linux Threads n Linux refers to them as tasks rather than threads n Thread creation is done through clone() system call n clone() allows a child task to share the address space of the parent task (process) l CLONE_FS, CLONE_VM, CLONE_SIGHAND, CLONE_FILES
36
4.36 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Linux Threads n clone() system call l Linux doesn’t distinguish between process and thread
37
4.37 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Java Thread States
38
4.38 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Pthread Example n #include … int main(int argc, char *argv[]) { pthread_t tid; pthread_attr_t attr; … pthread_attr_init(&attr); pthread_create(&tid, &attr, runner, argv[1]); pthread_join(tid, NULL); … } void *runner(void *param) { … pthread_exit(0); }
39
4.39 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Win32 Thread Example n #include … DWORD WINAPI Summation(LPVOID Param) { … } int main(int argc, char *argv[]) { DWORD ThreadId; HANDLE ThreadHandle; … ThreadHandle = CreateThread(NULL, 0, Summation, &Param, 0, &ThreadId); if (ThreadHandle !=NULL) { WaitForSingleObject(ThreadHandle, INFINITE); CloseHandle(ThreadHandle); … }
40
4.40 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Java Thread Example n Class Summation implements Runnable { … } public class Driver { public static void main() { … Thread thrd = new Thread(new Summation(upper, sumObject)); thrd.start(); try { thrd.join(); … } catch (InterruptedException ie) {} … }
41
End of Chapter 4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.