+ All Categories
Home > Documents > Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf ·...

Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf ·...

Date post: 17-Jun-2020
Category:
Upload: others
View: 21 times
Download: 0 times
Share this document with a friend
39
Chapter 4: Multithreaded Programming
Transcript
Page 1: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Chapter 4: Multithreaded

ProgrammingProgramming

Page 2: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Chapter 4: Multithreaded Programming

• Overview

• Multithreading Models

• Thread Libraries

• Threading Issues• Threading Issues

• Operating-System Examples

2009/10/19 2

Page 3: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

4.1 Overview

• A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack.

• A traditional, or heavyweight process has a single thread of control.

• It shares with other threads belonging to the same • It shares with other threads belonging to the same process its code section, data section, and other OSresources, such as open files and signals.

• In busy WWW server: The server creates a separate thread that would listen for clients requests, when a request was made, creates a thread to service the request.

2009/10/19 3

Page 4: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Single and Multithreaded Processes

2009/10/19 4

Page 5: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Benefits (1)

• Responsiveness: Allow a program to continue running even if part of it is blocked or is performing a lengthy operation.

• Resource sharing: several different threads of activity all within the same address space.

• Economy: Allocating memory and resources for process creation is costly. In Solaris, creating a process is about 30 times slower than is creating a thread, and context switching is about five times slower. A register set switch is still required, but no memoryno memory--management related work is neededmanagement related work is needed.

2009/10/19 5

Page 6: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Benefits (2)

• Scalability (Utilization of multiprocessor architecture):

Several thread may be running in parallel on different

processors.

– Of course, multithreading a process may introduce

concurrency control problem that requires the use of critical concurrency control problem that requires the use of critical

sections or locks.

2009/10/19 6

Page 7: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Multithreaded Server Architecture

2009/10/19 7

Page 8: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Multicore Programming (1)

• Placed multiple computing cores on a single chip,

where each core appears as a separate processor to

OS

• On a system with a single core, concurrency merely

means that the execution of the threads will be means that the execution of the threads will be

interleaved over time

• On a system with multicore, concurrency means that

the threads can run in parallel

• The trend towards multicore systems has placed

pressure on system designers as well as application

programmers to make better use of multicore

2009/10/19 8

Page 9: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Concurrent Execution on a Single-core System

Parallel Execution on a Multi-core System

2009/10/19 9

Page 10: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Multicore Programming (2)

• Multicore systems putting pressure on

programmers, challenges include

– Dividing activities into separate, concurrent tasks

– Balance the workload of multiple tasks– Balance the workload of multiple tasks

– Data splitting into separate cores

– Data dependency must be carefully examined

– Testing and debugging are more difficult than

single-thread applications

2009/10/19 10

Page 11: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

User Threads

• Thread management done by user-level threads library.

– Fast: All thread creating and scheduling are done in user space without the need for kernel intervention.

• Any user-level thread performing a blocking system

call will cause the entire process to block, even if there call will cause the entire process to block, even if there

are other threads available to run within the

applications, if the kernel is single-thread.

• Examples

– Pthreads

– Win32 threads

– Java threads2009/10/19 11

Page 12: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Kernel Threads

• Supported and managed directly by the OS

• Examples

– Windows XP/2000– Windows XP/2000

– Solaris

– Linux

– Tru64 UNIX

– Mac OS X

2009/10/19 12

Page 13: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

4.2 Multithreading Models (1)(Multi-Thread vs. Multi-process)

• Multiple processes

– Each is independent and has it own program counter, stack

register, and address space. This is useful for unrelated jobs.

– Multiple processes can perform the same task as well. (e.g.,

provide data to remote machines in a network file system).

Each executes the same code but has it own memory and file Each executes the same code but has it own memory and file

resources.

• Multiple-thread process

– It is more efficient to have one process containing multiple

threads serve the same task.

• Most Systems Support for both user and kernel threads

2009/10/19 13

Page 14: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Multithreading Models (2)

• Uses fewer resources, including memory, open files and CPU

scheduling.

• Threads are not independent to each other. This structure

does not provide protection.

– Only a single user can own an individual task with

multiple threads. The threads should be designed to

assist one another.assist one another.

• Threads can create child threads. If one thread is blocked,

another thread can run.

• Threads provide a mechanism that allows sequential

processes to make blocking system calls while also achieving

parallelism.

2009/10/19 14

Page 15: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Multithreading Models (3)

• Many-to-One

• One-to-One

• Many-to-Many

2009/10/19 15

Page 16: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Many-to-One

• Many user-level threads mapped to single kernel thread.

• Used on systems that do not support kernel threads.

• Thread management is done in user space, so it is efficient.

The entire process will block if a thread makes a • The entire process will block if a thread makes a blocking system call.

• Only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors.

2009/10/19 16

Page 17: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Many-to-One Model

2009/10/19 17

Page 18: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

One-to-One

• Each user-level thread maps to a kernel thread.

• More concurrency

• Overhead: Creating a thread requires creating the corresponding kernel thread.

• Examples• Examples

– - Windows XP/NT/2000

– - Linux

– - Solaris 9 and later

2009/10/19 18

Page 19: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

One-to-one Model

2009/10/19 19

Page 20: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Many-to-Many Model

• Allows many user level threads to be mapped to many

kernel threads

• Allows the operating system to create a sufficient number

of kernel threads

• Multiplexes many user-level threads to a smaller or equal number of kernel threadsnumber of kernel threads

• The corresponding kernel threads can run in parallel on a multiprocessor.

• When a thread performs a blocking call, the kernel can schedule another thread for execution

• Solaris prior to version 9

• Windows NT/2000 with the ThreadFiber package

2009/10/19 20

Page 21: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Many-to-Many Model

2009/10/19 21

Page 22: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Two-level Model

• Popular variation on Many-to-Many model

• Similar to M:M, except that it allows a user thread

to be bound to a kernel thread

• Examples• Examples

– IRIX

– HP-UX

– Tru64 UNIX

– Solaris 8 and earlier

2009/10/19 22

Page 23: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Two-level Model

2009/10/19 23

Page 24: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

4.3 Thread Libraries

• Thread library provides programmer with API

for creating and managing threads

• Two primary ways of implementing

– Library entirely in user space– Library entirely in user space

– Kernel-level library supported by the OS

2009/10/19 24

Page 25: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

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, • 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)

2009/10/19 25

Page 26: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Java Threads

• Java threads are managed by the JVM

• Typically implemented using the threads

model provided by underlying OSmodel provided by underlying OS

• Java threads may be created by:

– Extending Thread class

– Implementing the Runnable interface

2009/10/19 26

Page 27: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

4.4 Threading Issues

• Semantics of fork() and exec() system calls. Duplicate all the threads or not?

• Thread cancellation: Asynchronous or deferred

• Signal handling: Where then should a signal be delivered?delivered?

• Thread pools: Create a number of threads at process startup.

• Thread specific data: Each thread might need its own copy of certain data.

• Scheduler activations

2009/10/19 27

Page 28: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Semantics of fork() and exec()

• Does fork() duplicate only the calling thread

or all threads?

– Two versions of fork(): duplicates all threads

and another duplicates only the thread that and another duplicates only the thread that

invoked the fork() system call

– If exec() is called immediately after forking,

then duplicating all threads is unnecessary.

2009/10/19 28

Page 29: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Thread Cancellation

• Terminating a thread before it has completed.

• Two general approaches:

– Asynchronous cancellation terminates the target thread immediatelytarget thread immediately

– Deferred cancellation The target thread to periodically check whether it should be terminate, allowing it an opportunity to terminate itself in an orderly fashion (canceled safely).

• Cancellation points

2009/10/19 29

Page 30: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

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– Signal is handled

• Delivering Signals:

– 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

2009/10/19 30

Page 31: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Thread Pools

• Create a number of threads at process start

and place them in a pool where they await

work

• Advantages:• Advantages:

– 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

2009/10/19 31

Page 32: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Thread Specific Data

• Allows each thread to have its own copy of

data

– Each transaction assigned a unique number in the

transaction-processing systemtransaction-processing system

• Useful when you do not have control over the

thread creation process (i.e., when using a

thread pool)

2009/10/19 32

Page 33: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Scheduler Activations

• Both M:M and Two-level models require

communication to maintain the appropriate

number of kernel threads allocated to the

application

• Scheduler activations provide upcalls - a • 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

2009/10/19 33

Page 34: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Operating-system Example

• Explore how threads are implemented in

Windows XP and Linux systems.

2009/10/19 34

Page 35: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Windows XP Threads

• Implements the one-to-one mapping

• 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)2009/10/19 35

Page 36: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

Windows XP Threads

2009/10/19 36

Page 37: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

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 • clone() allows a child task to share the address

space of the parent task (process)

2009/10/19 37

Page 38: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

End of Chapter 4

Page 39: Chapter 4: Multithreaded Programminghscc.cs.nthu.edu.tw/~sheujp/lecture_note/09os/CH04OS.pdf · Chapter 4: Multithreaded Programming • Overview • Multithreading Models • Thread

HomeworkHomework

• 4.1, 4.4, 4.6

• Due Oct. 28

• Project- Matrix Multiplication (multi-thread vs. multi-process)

• Due Nov. 10

2009/10/19 39


Recommended