1 / 26

Chapter 4. Multithreaded Programming

Chapter 4. Multithreaded Programming. OS lab Sun Suk Kim. Contents. Overview Multithreading Model Thread Libraries Threading Issues Operating-system Example. Contents. multiple threads. Introduce many concept. Look at many issues. Explore how some OSs support kernel level threads.

Download Presentation

Chapter 4. Multithreaded Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4.Multithreaded Programming OS lab Sun Suk Kim

  2. Contents • Overview • Multithreading Model • Thread Libraries • Threading Issues • Operating-system Example OS Lab Sun Suk Kim

  3. Contents multiple threads Introduce many concept Look at many issues Explore how some OSs support kernel level threads OS Lab Sun Suk Kim

  4. 1 Overview A process ,in most modern OS, containing multiple threads code data file code data file reg stack reg reg reg stack stack stack thread OS Lab Sun Suk Kim

  5. 1-1 Motivation 1. Software package in Desktop Separate process with several threads of control Desktop PC Some kind of Software Packages Web Browser The other process Word Processor display display thread Resp keystroke retrieve data thread Check spell& grammar OS Lab Sun Suk Kim

  6. 1-1 Motivation 2. One process that contains multiple threads Not create a separate process to service that request (2) create new thread to service the request (1) request client server thread (3) resume listening for additional client requests OS Lab Sun Suk Kim

  7. 1-2 Benefits • Responsiveness • May allow a program to continue running • Increasing responsiveness to the user • Resource sharing • Threads share the memory and resources of the process(default) • 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 OS Lab Sun Suk Kim

  8. 1-3 Multicore Programming • Multithreaded programming on single-core system • Multithreaded programming on multicore system single core time core 1 core 2 time OS Lab Sun Suk Kim

  9. 1-3 Multicore Programming • 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 OS Lab Sun Suk Kim

  10. 2 Multithreading Model Support for threads may be provided both user or kernel level So, relationship must exist between user threads and kernel threads • User thread(user level) • Supported above the kernel • Managed without kernel support • Kernel thread(kernel level) • Supported, managed directly by OS • Windows XP, Linux, Mac OS X, Solaris,Tru64 UNIX support kernel threads OS Lab Sun Suk Kim

  11. 2-1 Many-to-One Model k • Maps many user-level threads to one kernel thread • Thread management is done by the thread library in user space • When a thread makes a blocking system call then other process will block • Only one thread can access the kernel at a time • multiple threads are unable to run in parallel on multiprocessors • Green thread(Solaris)GNU Potable thread OS Lab Sun Suk Kim

  12. 2-2 One-to-One Model k k k k • 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 • Creating a user thread - creating the kernel thread • Creating kernel threads is make overhead • System restrict the number of threads • Linux, Windows OS Lab Sun Suk Kim

  13. 2-3 Many-to-Many Model k k k • 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 • Some kind of variation on this model • two level model OS Lab Sun Suk Kim

  14. 3 Thread Libraries • Provide 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 OS Lab Sun Suk Kim

  15. 3-1 Pthreads main( ) pthread_attr_init( ) pthread_create( ) function pthread_join( ) pthread_exit( ) OS Lab Sun Suk Kim

  16. 3-2 Win32 Threads main( ) CreateThread( ) function WaitForSingleObject( ) return OS Lab Sun Suk Kim

  17. 4 Threading Issues • The fork() and exec() System Calls • Cancellation • Signal Handling • Thread Pools • Thread-Specific Data • Scheduler Activations OS Lab Sun Suk Kim

  18. 4-1 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 OS Lab Sun Suk Kim

  19. 4-2 Cancellation • the task of terminating a thread before it has completed • 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 OS Lab Sun Suk Kim

  20. 4-3 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 OS Lab Sun Suk Kim

  21. 4-4 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 OS Lab Sun Suk Kim

  22. 4-5 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 OS Lab Sun Suk Kim

  23. 4-6 Scheduler Activations LWP lightweight process k • 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 OS Lab Sun Suk Kim

  24. 5 Operating-system Example Windows XP Threads Linux Threads OS Lab Sun Suk Kim

  25. 5-1 Windows XP Threads ETHREAD KTHREAD TEB kernel space user space • 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 OS Lab Sun Suk Kim

  26. 5-2 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 OS Lab Sun Suk Kim

More Related