Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.

Similar presentations


Presentation on theme: "Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading."— Presentation transcript:

1 Chapter 4: Threads

2 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 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 – 7 th edition, Jan 23, 2005 Thread: Definition A Thread is a basic unit of CPU utilization. A thread of execution. A placeholder information associated with a single use of a program that can handle multiple concurrent users Thread Lightweight process (LWP) Threads of instructions or thread of control Shares address space and other global information with its process Registers, stack, signal masks and other thread-specific data are local to each thread

4 4.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread Composition A Thread is comprised of: A thread ID. A program counter. A register set. A stack.

5 4.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Process vs Threads A traditional or heavyweight process has a single thread of control. If a process has multiple threads of control, it can perform more than one task at a time, thus a thread is a lightweight process.

6 4.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Single and Multithreaded Processes

7 4.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Benefits Responsiveness A program may continue even if parts of it are blocked. Ex: web browser loading some images while others take more time. Resource Sharing By default, threads share memory and the resources of the process to which they belong. Economy Easier to create and context switch between threads since they share resources. Utilization of MP Architectures Run multiple threads in parallel, taking advantage of multiple processors.

8 4.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Benefits Takes less time to create a new thread than a process. Less time to terminate a thread than a process. Less time to switch between two threads within the same process. Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel

9 4.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Multithreading Modes Provided in different levels: User Threads.  Supported above the kernel and managed without kernel support. Kernel Threads.  Supported and managed directly by the operating system.

10 4.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 User Threads Thread management done by user-level threads library. The kernel is not aware of the existence of threads. Three primary thread libraries: POSIX Pthreads Win32 threads Java threads

11 4.11 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Kernel Threads Supported by the Kernel Kernel maintains context information for the process and the threads Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X

12 4.12 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Multithreading Models Many-to-One Maps many user-level threads to a single kernel thread. One-to-One Maps each user-level thread to a kernel thread. Many-to-Many Multiplexes many user-level threads to a smaller or equal number of kernel threads.

13 4.13 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Many-to-One Many user-level threads mapped to single kernel thread. Thread management is done by the thread library in user space. Will block if thread makes a blocking system call. Multiple threads are unable to run in parallel on multiprocessors. Examples: Solaris Green Threads 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 Each user-level thread maps to kernel thread. Provides more concurrency allowing another thread to run when one makes a blocking system call. Allows multiple threads to run in parallel in multiprocessors. Developer must be careful not to make too many threads per application. Examples Windows NT/XP/2000 Linux 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 Allows many user level threads to be mapped to many kernel threads. Allows the operating system to create a sufficient number of kernel threads. Developers can create as many threads as necessary. Solaris prior to version 9. 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 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

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 State: Life Cycle of a Thread Thread states Born state Ready state (runnable state) Running state Dead state Blocked state Waiting state Sleeping state  Sleep interval specifies for how long a thread will sleep

22 4.22 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread State: Life Cycle of a Thread Thread Life Cycle:

23 4.23 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread Libraries A thread library provides the programmer an API for creating and mapping threads. Two ways of implementing a thread library: Library entirely in user space with no kernel support.  Invoking a function in the library results in a local call function not a system call. Kernel-level library supported directly by the operating system.  Code and structures reside in kernel space.  Invoking a function in the API typically results in a system call to the kernel.

24 4.24 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread Libraries Three main libraries are used today: POSIX Pthreads.  User or kernel level. Win32.  Kernel level only on windows systems. Java.  Java programs.

25 4.25 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 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) Third party support for Windows Systems.

26 4.26 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 PTHREADS Code Example:

27 4.27 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threading Issues Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations

28 4.28 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Semantics of fork() and exec() The semantics of fork() and exec() system calls must change in a multithreaded program. Fork(): New process duplicates all threads? New process a single threaded program? UNIX Some Unix implementations have chosen two types of fork():  Duplicates all threads  Duplicates only the thread that invokes the fork().

29 4.29 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 EXEC() The exec() system call functions in the same way as with single threaded programs. If a thread invokes an exec() system call, the program specified in the parameters to exec() will replace the entire process including all threads.

30 4.30 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 UNIX fork() Which of the two versions of the UNIX fork() do we use? Dependant on the application. If exec() is called immediately after fork:  Duplicating all threads is not necessary.  Duplicating only the calling thread is appropriate. If the separate process does not call exec():  The separate process should duplicate all threads.

31 4.31 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread Cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately.  Problems arise when reclaiming resources.  Free system wide resources. Deferred cancellation allows the target thread to periodically check if it should be cancelled.  Cancellation only occurs only after the target thread has finished its current operation, thus freeing up resources.  Pthreads refer this as cancellation points. When should thread cancellation be used?

32 4.32 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred A signal handler is used to process signals  Signal is generated by particular event  Signal is delivered to a process  Signal is handled 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 threa to receive all signals for the process

33 4.33 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread Signal Delivery Two types of signals Synchronous:  Occur as a direct result of program execution  Should be delivered to currently executing thread Asynchronous:  Occur due to an event typically unrelated to the current instruction  Threading library must determine each signal’s recipient so that asynchronous signals are delivered properly Each thread is usually associated with a set of pending signals that are delivered when it executes Thread can mask all signals except those that it wishes to receive Most multithreaded versions of UNIX allow a thread to specify which signals it will accept and which it will block. Usually signals are only delivered to the first non blocking thread.

34 4.34 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread Signal Delivery Examples Kill (aid_t aid, int signal) Particular signal to deliver. Process to deliver the signal to. Posix Pthreads: Pthread_kill(pthread_t tid, int signal)  Signal delivered to a specific thread)

35 4.35 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread Pools Create a number of threads in a pool where they await work. Pools are created when the server or machine starts. 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.

36 4.36 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Thread Pools Once a thread is needed, it awakens a thread from the pool, if one is available it is passed to the requested service. Once the thread completes its service, it is returned to the pool and awaits for more work. If no threads are available from the pool the server waits for one to complete and return to the pool.

37 4.37 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 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)

38 4.38 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Scheduler Activations Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library. LWP (used as intermediary) This communication allows an application to maintain the correct number kernel threads

39 4.39 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Windows XP Threads Implements the one-to-one mapping 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)

40 4.40 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone(FLAGS) allows a child task to share the address space of the parent task (process)

41 4.41 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Java Threads Java threads are managed by the JVM Java threads may be created by: Extending Thread class Implementing the Runnable interface

42 4.42 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Java Thread States

43 End of Chapter 4


Download ppt "Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading."

Similar presentations


Ads by Google