Presentation is loading. Please wait.

Presentation is loading. Please wait.

Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim.

Similar presentations


Presentation on theme: "Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim."— Presentation transcript:

1

2 Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim

3 3 multiple threads Introduce many concept Look at many issues Explore how some OSs support kernel level threads Contents

4 4 OS Lab Sun Suk Kim 1 Overview A process,in most modern OS, containing multiple threads reg code stackreg filedatacode reg datafile reg stack thread

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

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

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

8 1-3 Multicore Programming 8 OS Lab Sun Suk Kim Multithreaded programming on single-core system Multithreaded programming on multicore system T1T2T3T4T1T2T3T4T1… single core time T1T3T1T3T1… core 1 T2T4T2T4T2… core 2 time

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

10 2 Multithreading Model 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 10 OS Lab Sun Suk Kim Support for threads may be provided both user or kernel level So, relationship must exist between user threads and kernel threads

11 2-1 Many-to-One Model 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 11 OS Lab Sun Suk Kim k k

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

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

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 14 OS Lab Sun Suk Kim

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

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

17 4 Threading Issues 1.The fork() and exec() System Calls 2.Cancellation 3.Signal Handling 4.Thread Pools 5.Thread-Specific Data 6.Scheduler Activations 17 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 18 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 19 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 20 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 21 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 22 OS Lab Sun Suk Kim

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

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

25 5-1 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 25 OS Lab Sun Suk Kim thread start address pointer to parent process … scheduling and synchronization information kernel stack … thread identifier user stack thread-local storage … ETHREADKTHREADTEB kernel spaceuser space

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 26 OS Lab Sun Suk Kim flagmeaning CLONE_FSFile-system information is shared CLONE_VMThe same memory space is shared CLONE_SIGHANDSignal handlers are shared CLONE_FILESThe set of open files is shared


Download ppt "Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim."

Similar presentations


Ads by Google