26
Chapter 4. Multithreaded Programming OS lab Sun Suk Kim

Chapter 4. Multithreaded Programming

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 4. Multithreaded Programming

Chapter 4. Multithreaded Pro-

gramming

OS lab

Sun Suk KimOS lab

Sun Suk Kim

Page 2: Chapter 4. Multithreaded Programming

2OS Lab Sun Suk Kim

ContentsContents

1. Overview

2. Multithreading Model

3. Thread Libraries

4. Threading Issues

5. Operating-system Example

Page 3: Chapter 4. Multithreaded Programming

3OS Lab Sun Suk Kim

multiple threads

Introduce many concept

Look at many issues

Explore how some OSs support kernel level threads

ContentsContents

Page 4: Chapter 4. Multithreaded Programming

4OS Lab Sun Suk Kim

1 Overview1 Overview

A process ,in most modern OS, containing multiple threads

reg

code

stackreg

filedatacode

reg

data file

reg

stackstackstack

thread

Page 5: Chapter 4. Multithreaded Programming

5

Desktop PC

Some kind of Software Packages

1-1 Motivation1-1 Motivation

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

thread

Word Processor

Check spell& gram-mar

Resp key-

stroke

display

Page 6: Chapter 4. Multithreaded Programming

6

1-1 Motivation1-1 Motivation

OS Lab Sun Suk Kim

2. One process that contains multiple threads

Not create a separate process to service that re-quest

client server thread

(1) request(2) create new thread to service the request

(3) resume listening for additional client requests

Page 7: Chapter 4. Multithreaded Programming

7

1-2 Benefits1-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

Page 8: Chapter 4. Multithreaded Programming

8OS Lab Sun Suk Kim

1-3 Multicore Programming1-3 Multicore Programming

• Multithreaded programming on single-core system

• Multithreaded programming on multicore system

T1 T2 T3 T4 T1 T2 T3 T4 T1 …single core

time

T1 T3 T1 T3 T1 …core 1

T2 T4 T2 T4 T2 …core 2

time

Page 9: Chapter 4. Multithreaded Programming

9OS Lab Sun Suk Kim

1-3 Multicore Programming1-3 Multicore Programming

• Challenging areas in programming for multicore sys-

tems– 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 concur-

rent 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 syn-

chronized to accommodate the data dependency

– Testing and Debugging: is more difficult than one of single-

threaded application because it has many different execution paths

Page 10: Chapter 4. Multithreaded Programming

10

2 Multithreading Model2 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

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

Page 11: Chapter 4. Multithreaded Programming

11

2-1 Many-to-One Model2-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 multiproces-

sors

• Green thread(Solaris)GNU Potable thread

OS Lab Sun Suk Kim

k

Page 12: Chapter 4. Multithreaded Programming

12

2-2 One-to-One Model2-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

OS Lab Sun Suk Kim

k k kk

Page 13: Chapter 4. Multithreaded Programming

13OS Lab Sun Suk Kim

2-3 Many-to-Many Model2-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

k kk

Page 14: Chapter 4. Multithreaded Programming

14

3 Thread Libraries3 Thread Libraries

• Provide APIs for creating and managing threads– to provide a library entirely in user space with no kernel sup-

port– 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 sys-

tem

OS Lab Sun Suk Kim

Page 15: Chapter 4. Multithreaded Programming

15

3-1 Pthreads3-1 Pthreads

OS Lab Sun Suk Kim

main( )

pthread_attr_init( )

pthread_create( )

pthread_join( )

function

pthread_exit( )

Page 16: Chapter 4. Multithreaded Programming

16

3-2 Win32 Threads3-2 Win32 Threads

OS Lab Sun Suk Kim

main( )

CreateThread( )

WaitForSingleObject( )

function

return

Page 17: Chapter 4. Multithreaded Programming

17

4 Threading Issues4 Threading Issues

1. The fork() and exec() System Calls

2. Cancellation

3. Signal Handling

4. Thread Pools

5. Thread-Specific Data

6. Scheduler Activations

OS Lab Sun Suk Kim

Page 18: Chapter 4. Multithreaded Programming

18

4-1 The fork() and exec() System Calls4-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 un-

necessary• 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 threadsOS Lab Sun Suk

Kim

Page 19: Chapter 4. Multithreaded Programming

19

4-2 Cancellation4-2 Cancellation

• the task of terminating a thread before it has com-pleted

• 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 ter-

minate, 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 deter-

mine

OS Lab Sun Suk Kim

Page 20: Chapter 4. Multithreaded Programming

20

4-3 Signal Handling4-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 ex-

ternal to a running process• an asynchronous signal is sent to another process

OS Lab Sun Suk Kim

Page 21: Chapter 4. Multithreaded Programming

21

4-4 Thread Pools4-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

Page 22: Chapter 4. Multithreaded Programming

22

4-5 Thread-Specific Data4-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

Page 23: Chapter 4. Multithreaded Programming

23OS Lab Sun Suk Kim

4-6 Scheduler Activations4-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

k

LWP

lightweight process

Page 24: Chapter 4. Multithreaded Programming

24

5 Operating-system Example5 Operating-system Example

1. Windows XP Threads

2. Linux Threads

OS Lab Sun Suk Kim

Page 25: Chapter 4. Multithreaded Programming

25

5-1 Windows XP Threads5-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

OS Lab Sun Suk Kim

thread start ad-dress

pointer to parent process

scheduling and synchronization

information

kernel stack

thread identifier

user stack

thread-local stor-age

ETHREAD KTHREAD TEB

kernel space user space

Page 26: Chapter 4. Multithreaded Programming

26OS Lab Sun Suk Kim

5-2 Linux Threads5-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

flag meaning

CLONE_FS File-system information is shared

CLONE_VM The same memory space is shared

CLONE_SIGHAND Signal handlers are shared

CLONE_FILES The set of open files is shared