300 likes | 327 Views
Chapter 4 Multithreaded Programming. 8 th Edition. Operating System Concepts. Ok-Kyoon Ha. Operating System Laboratory, GNU. Contents. Overview Multithreading Models Thread Libraries Threading Issues Operating-System Examples. Chapter Objectives.
E N D
Chapter 4 Multithreaded Programming 8th Edition Operating System Concepts Ok-Kyoon Ha Operating System Laboratory, GNU
Contents • Overview • Multithreading Models • Thread Libraries • Threading Issues • Operating-System Examples
Chapter Objectives • To introduce the notion of a thread • To discuss the APIs for the Pthreads, Win32, and Java thread libraries • To examine issues related to multithreaded programming
Overview • Multithreading Models • Thread Libraries • Threading Issues • Operating-System Examples
Motivation • The Thread of Control • A traditional 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 • Single-threaded and multithreaded processes code data files code data files registers stack registers registers registers stack stack stack thread
Multithreaded server architecture • Operating System Kernels • most operating system kernels are now multithreaded • several threads operate in the kernel • each thread performs a specific task (2) create new thread to service the request (1) request client server thread (3) resume listening for additional client requests
Benefits • Responsiveness • may allow a program to continue running • increasing responsiveness to the user • Resource sharing • only share resources through techniques (shared memory or message passing) • allows an application to have several threads of activity within the same address space • Economy • more economical to create and context-switch threads • Scalability • can be greatly increased in a multiprocessor architecture • Multithreading on a multi-CPU machine increases parallelism
Multicore Programming • Multithreaded programming on single-core system • Multithreaded programming on multicore system single core time core 1 core 2 time
Challenging areas in programming for multicore systems • Dividing activities: find areas that can be divided into separate, concurrent task and thus can run in parallel on individual cores. • Balance: ensure that the tasks perform equal work of equal value • Data splitting: the data accessed and manipulated by the concurrent tasks must be divided to run on separate cores • Data dependency: where one task depends on data from another, programmer must ensure that the execution of the tasks is synchronized to accommodate the data dependency • Testing and Debugging: is more difficult than one of single-threaded application because it has many different execution paths
Overview • Multithreading Models • Thread Libraries • Threading Issues • Operating-System Examples
User Threads and Kernel Threads • User threads • implemented by a thread library at the user level • are supported above the kernel and are managed without kernel support • thread creation and scheduling are done in user space: fast ex) POSIX threads, Win32 threads, Java threads • Kernel threads • supported and managed directly by the operating system • thread creation and scheduling are done in kernel space: slow ex) Window XP, Linux, Mac OS X, Solaris, Tru64 UNIX • Relationship must exist between user threads and kernel threads
Many-to-One Model • Maps many user-level threads to one kernel thread • Thread management is done by the thread library in user space → efficient • When a thread makes a blocking system call → the entire process will block • Only one thread can access the kernel at a time • multiple threads are unable to run in parallel on multiprocessors user thread kernel thread k
One-to-One Model • Maps each user thread to a kernel thread • provides more concurrency than the many-to-one model • When a thread makes a blocking system call → allows another thread to run • Drawback: creating a user thread requires creating the corresponding kernel thread • the overhead of creating kernel threads restrict the number of threads supported by the system • Linux, Windows user thread k k k k kernel thread
Many-to-Many Model • Multiplexes many user threads to a smaller or equal number of kernel threads • allows creating as many user threads as the developer • When a thread performs a blocking system call → the kernel can schedule another thread for execution • True concurrency is not gained • the kernel can schedule only one thread at a time user thread kernel thread k k k
Overview • Multithreading Models • Thread Libraries • Threading Issues • Operating-System Examples
Thread Library • A set of APIs for creating and managing threads • to provide a library entirely in user space with no kernel support • to implement a kernel-level library supported directly by the operating system • Three main thread libraries • Pthreads: extension of the POSIX standard, may be provided as either a user or kernel level library • Win32 threads: a kernel level library available on Windows systems • Java threads: using a thread library available on the host system (JVM) Java threads Pthreads Win32 threads
Pthreads • Related functions • pthread_attr_init ( ) • pthread_create ( ) • pthread_join ( ) • Demonstration • result of Figure 4.9 main( ) pthread_attr_init( ) pthread_create( ) summation func. pthread_join( ) pthread_exit( )
Win32 Threads • Related function • CreateThread( ) • WaitForSingleObject( ) • Demonstration • result of Figure 4.10 main( ) CreateThread( ) summation func. WaitForSingleObject( ) return
Overview • Multithreading Models • Thread Libraries • Threading Issues • Operating-System Examples
The fork( ) and exec( ) System Calls • fork( ) • does the new process duplicate all threads • duplicates only the thread that invoked the fork( ) system call • exec( ) • specified in the parameter to exec( ) will replace the entire process (all thread) • fork( ) → exec( ) • exec( ) is called immediately after forking: duplicating is unnecessary • the program specified in the parameters to exec( ) will replace the process • separate process does not call exec( ) after forking→ the separate process should duplicate all threads
Cancellation • the task of terminating a thread before it has completed • example: searching on a database, pressing the stop button in web browser • Asynchronous cancellation • One thread immediately terminates the target thread • the difficulty with asynchronous cancellation • resources have been allocated to a canceled thread • canceled while in the midst of updating data it is sharing with other threads • Deferred cancellation • The target thread periodically checks whether it should terminate, allowing it an opportunity to terminate itself in an orderly fashion • can be canceled safely • occurs only after the target thread has checked a flag to determine
Signal Handling • Patterns of signal • A signal is generated by the occurrence of a particular event • A generated signal is delivered to a process • Once delivered, the signal must be handled • Synchronous or Asynchronous signals • Synchronous: If a running program performs either illegal memory access and division by 0 • are delivered to the same process that performed the operation that caused the signal • Asynchronous: when a signal is generated by an event external to a running process • an asynchronous signal is sent to another process
Handling signals in multithreaded programs • The method for delivering a signal depend on the type of signal generated • synchronous signals need to be delivered to the thread causing the signal and not to other threads in the process • asynchronous signals is not clear (should be sent to all threads , particular threads, or only same thread)
Thread Pools • is a solution to potential problems in multithreaded programs • multithreaded programs have potential problems: time and space • Benefits of thread pools • Servicing a request with an existing thread is usually faster than waiting to create a thread • A thread pool limits the number of threads that exist at any one point • particularly important on systems that cannot support a large number of concurrent threads
Thread-Specific Data • The sharing of data provides one of the benefits of multithreaded programming • each thread might need its own copy of certain data • call such data thread-specific data • Most thread libraries provide some form of support for thread-specific data
Scheduler Activations • Communication between the kernel and the thread library • Many systems implementing either the many-to-many or the two-level model • How to allow the number of kernel threads to be dynamically adjusted to help ensure the best performance • LWP (Lightweight Process) • To the user-thread library, the LWP appears to be a virtual processor • The application can schedule a user thread to run on virtual processor • each LWP is attached to a kernel thread that the operating system schedules to run on physical processor user thread LWP lightweight process k kernel thread
Overview • Multithreading Models • Thread Libraries • Threading Issues • Operating-System Examples
Windows XP Threads • Uses the one-to-one model • each user-level thread maps to an associated kernel thread • also provides support for a fiber library, which provides the functionality of the many-to-many model • The primary data structures of a thread ETHREAD KTHREAD TEB kernel space user space
Linux Threads • clone( ) system call • Linux does not distinguish between processes and threads → uses the term task • determine how much sharing is to take place between the parent and child tasks • flags of clone( ) • fork( ): new task requires a copy of all the associated data structures of the parent process • clone( ): new task points to the data structures of the parent task