Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

Similar presentations


Presentation on theme: "Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)"— Presentation transcript:

1 Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

2 2 CS 446/646 Principles of Computer Operating Systems Chapter 4: Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

3 3 CS 446/646 Principles of Computer Operating Systems  The execution part is a “thread” Pasta for six –boil 1 quart salty water –stir in the pasta –cook on medium until “al dente” –serve Program Process CPU input data thread of execution  The execution part is a “thread” that can be multiplied other thread same CPU working on two things Multithreading

4 4 CS 446/646 Principles of Computer Operating Systems ex: Solaris, Mach, Windows ex: Java VM ex: MS-DOS ex: early UNIX uniprogramming multiprogramming Process-Thread relationship

5 5 CS 446/646 Principles of Computer Operating Systems Single and Multithreaded Processes

6 6 CS 446/646 Principles of Computer Operating Systems Multithreaded Server Architecture

7 7 CS 446/646 Principles of Computer Operating Systems Benefits Responsiveness: Continue even if part of an application is blocked due to I/O Allow user interaction in one thread while image is loading in another. Resource Sharing: Memory / resource sharing is by default with threads. Several different threads of activity within the same address space. Economy: It is more economical to create and context-switch threads. In general, creating a process is 30 times slower, context-switching a process is 5 times slower. Scalability: Multiprocessor architectures A single-threaded process can only run on one processor.

8 8 CS 446/646 Principles of Computer Operating Systems Multicore Scalability Parallel Execution on a Multicore System Concurrent Execution on a Single-core System

9 9 CS 446/646 Principles of Computer Operating Systems Multicore Programming Multicore systems putting pressure on programmers, challenges include Dividing activities: Finding areas of code that can run in parallel as concurrent tasks. Balance: Tasks should perform equal work of equal value. Data splitting: Data manipulated by the tasks must be divided to run on separate cores. Data dependency: Ensure the execution of the tasks is synchronized to accommodate data dependencies between tasks. Testing and debugging: Testing a program with multiple execution paths is inherently more challenging.

10 10 CS 446/646 Principles of Computer Operating Systems User Threads Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads

11 11 CS 446/646 Principles of Computer Operating Systems Kernel Threads Supported by the Kernel Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X

12 12 CS 446/646 Principles of Computer Operating Systems Multithreading Models Many-to-One One-to-One Many-to-Many Two-level

13 13 CS 446/646 Principles of Computer Operating Systems Many-to-One Many user-level threads mapped to single kernel thread. Thread management is done by the thread library in user space, Is efficient, But, entire process will block is a thread makes a blocking system call. Only one thread can access the kernel at a time, Multiple threads can not run in parallel on multiprocessors. Examples: Solaris Green Threads GNU Portable Threads

14 14 CS 446/646 Principles of Computer Operating Systems One-to-One Each user-level thread maps to kernel thread. Provides more concurrency. No blocking due to another thread. Threads may run in parallel on multiprocessors. Creating a user thread requires creating the corresponding kernel thread. Hence, OSes limit the number of threads. Examples Windows NT/XP/2000 Linux Solaris 9 and later

15 15 CS 446/646 Principles of Computer Operating Systems Many-to-Many Model Many user level threads are mapped to many kernel threads. Allows the operating system to create a sufficient number of kernel threads. Suffers from neither of shortcomings of many-to-one or one-to-one. Examples Windows NT/2000 with the ThreadFiber package Solaris prior to version 9

16 16 CS 446/646 Principles of Computer Operating Systems Two-level Model Similar to many-to-many, except that it allows a user thread to be bound to kernel thread. Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

17 17 CS 446/646 Principles of Computer Operating Systems

18 18 CS 446/646 Principles of Computer Operating Systems Thread Libraries Thread library provides programmer with API for creating and managing threads Pthreads Win32 threads Java threads Two primary ways of implementing Library entirely in user space Kernel-level library supported by the OS  Thread functions invoke system calls.

19 19 CS 446/646 Principles of Computer Operating Systems Pthreads May be provided either as user-level or kernel-level 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) n pthread_create() n pthread_join()

20 20 CS 446/646 Principles of Computer Operating Systems Java Threads Java threads are managed by the JVM Typically implemented using the threads model provided by underlying OS Java threads may be created by: Extending Thread class Implementing the Runnable interface  Must define run() method  Call start() method to initialize a thread  Use join() method to wait for a thread

21 21 CS 446/646 Principles of Computer Operating Systems Threading Issues Semantics of fork() and exec() system calls Thread cancellation of target thread Asynchronous or deferred Signal handling Thread pools Thread-specific data Scheduler activations

22 22 CS 446/646 Principles of Computer Operating Systems Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads? UNIX provides both approaches. exec() works the same entire process is replaced with the new one

23 23 CS 446/646 Principles of Computer Operating Systems Thread Cancellation Terminating a thread before it has finished Asynchronous cancellation terminates the target thread immediately OS may not reclaim all resources Deferred cancellation allows the target thread to periodically check if it should be cancelled

24 24 CS 446/646 Principles of Computer Operating Systems 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 thread to receive all signals for the process

25 25 CS 446/646 Principles of Computer Operating Systems Thread Pools Create a number of threads in a pool where they await work 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

26 26 CS 446/646 Principles of Computer Operating Systems 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)

27 27 CS 446/646 Principles of Computer Operating Systems Scheduler Activations Communication between the kernel and thread library. Both many-to-many 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 This communication allows an application to maintain the correct number kernel threads

28 28 CS 446/646 Principles of Computer Operating Systems I/O request/completion Time T4 User-Level Runtime System Kernel Virtual Processors Time T1 User-Level Runtime System Kernel Time T3 a s_a is unblocked Time T2 B a s_a is blocked

29 29 CS 446/646 Principles of Computer Operating Systems 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)

30 30 CS 446/646 Principles of Computer Operating Systems 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)

31 Modified from Silberschatz, Galvin and Gagne ©2009 End of Chapter 4


Download ppt "Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)"

Similar presentations


Ads by Google