Chapter 4: Multithreaded
Programming

Chapter 4: Multithreaded
Programming Overview
 Multithreading Models
 Thread Libraries
 Threading Issues
 Operating-System Examples

Operating System Concepts

4.2

Silberschatz, Galvin and Gagne ©2005

Overview A thread is a basic unit of CPU utilization; it

comprises a thread ID, a program counter, aregister set, and a stack.
 A traditional, or heavyweight process has asingle thread of control.
 It shares with other threads belonging to the sameprocess its code section, data section, and otherOS resources, 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 toservice the request.

Operating System Concepts

4.3

Silberschatz, Galvin and Gagne ©2005

Single and Multithreaded Processes

Operating System Concepts

4.4

Silberschatz, Galvin and Gagne ©2005

Benefits (1) Responsiveness: Allow a program to continue runningeven if part of it is blocked or is performing alengthy operation.
 Resource sharing: several different threads ofactivity all within the same address space.
 Economy: Allocating memory and resources for processcreation 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. Aregister set switch is still required, but nomemory--management related work is needed.
memoryneededOperating System Concepts

4.5

Silberschatz, Galvin and Gagne ©2005

Benefits (2) 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 useof critical sections or locks.

Operating System Concepts

4.6

Silberschatz, Galvin and Gagne ©2005

User Threads
 Thread management done by user-level threads

library.
Fast: All thread creating and scheduling are donein user space without the need for kernelintervention.

 Any user-level thread performing a blocking systemcall will cause the entire process to block, even if
there are other threads available to run within theapplications, if the kernel is single-thread.
 Examples
Pthreads

Win32 threads

Operating System Concepts

4.7

Silberschatz, Galvin and Gagne ©2005

Kernel Threads
 Supported and managed directly by the OS Examples
 Windows XP/2000
 Solaris
 Linux Tru64 UNIX Mac OS X

Operating System Concepts

4.8

Silberschatz, Galvin and Gagne ©2005

Multithreading Models (1)(Multi-Thread vs. Multi-process) Multiple processes
 Each is independent and has it own program

counter, stack register, and address space. Thisis useful for unrelated jobs.
 Multiple processes can perform the same task aswell. (e.g., provide data to remote machines in anetwork file system). Each executes the same code
but has it own memory and file resources.
 Multiple-thread process It is more efficient to have one processcontaining multiple threads serve the same task.
 Most Systems Support for both user and kernel threads
Operating System Concepts

4.9

Silberschatz, Galvin and Gagne ©2005

Multithreading Models (2) Uses fewer resources, including memory, open

files and CPU scheduling.
 Threads are not independent to each other. Thisstructure does not provide protection.
 Only a single user can own an individual taskwith multiple threads. The threads should bedesigned to assist one another.
 Threads can create child threads. If one threadis blocked, another thread can run.
 Threads provide a mechanism that allows

sequential processes to make blocking system
calls while also achieving parallelism.
Operating System Concepts

4.10

Silberschatz, Galvin and Gagne ©2005

Multithreading Models
 Many-to-One
 One-to-One
 Many-to-Many

Operating System Concepts

4.11

Silberschatz, Galvin and Gagne ©2005

Many--to
Manyto--One Many user-level threads mapped to single

kernel thread.
 Used on systems that do not support kernelthreads.
 Thread management is done in user space, soit is efficient.
 The entire process will block if a thread
makes a blocking system call.
 Only one thread can access the kernel at atime, multiple threads are unable to run inparallel on multiprocessors.
Operating System Concepts

4.12

Silberschatz, Galvin and Gagne ©2005

Many--toMany
to--One Model

Operating System Concepts

4.13

Silberschatz, Galvin and Gagne ©2005

One--toOneto--One Each user-level thread maps to kernelthread.
 More concurrency
 Overhead: Creating a thread requires
creating the corresponding kernel thread.
 Examples

Operating System Concepts- Windows XP/NT/2000- Linux- Solaris 9 and later

4.14

Silberschatz, Galvin and Gagne ©2005

One--toOne
to--one Model

Operating System Concepts

4.15

Silberschatz, Galvin and Gagne ©2005

Many--toMany
to--Many Model Allows many user level threads to be mapped tomany kernel threads
 Allows theoperating system to create asufficient number of kernel threads
 Multiplexes many user-level threads to asmaller or equal number of kernel threads
 The corresponding kernel threads can run inparallel on a multiprocessor.
 When a thread performs a blocking call, thekernel can schedule another thread forexecution
 Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package
Operating System Concepts

4.16

Silberschatz, Galvin and Gagne ©2005

Many--to
Manyto--Many Model

Operating System Concepts

4.17

Silberschatz, Galvin and Gagne ©2005

Two--level Model
Two Similar to M:M, except that it allows auser thread to be bound to kernel thread Examples
 IRIX HP-UX Tru64 UNIX
 Solaris 8 and earlier

Operating System Concepts

4.18

Silberschatz, Galvin and Gagne ©2005

Two--level Model
Two

Operating System Concepts

4.19

Silberschatz, Galvin and Gagne ©2005

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 bedelivered?
 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

Operating System Concepts

4.20

Silberschatz, Galvin and Gagne ©2005

Semantics of fork() and exec()
 Does fork() duplicate only the callingthread or all threads?
 Two versions of fork(): duplicates all

threads and another duplicates only thethread that invoked the fork() system call
 If exec() is called immediately after

forking, then duplicating all threads isunnecessary.

Operating System Concepts

4.21

Silberschatz, Galvin and Gagne ©2005

Thread Cancellation
 Terminating a thread before it has completed.
 Two general approaches: Asynchronous cancellation terminates thetarget thread immediately
 Deferred cancellation The target thread toperiodically check whether it should beterminate, allowing it an opportunity toterminate itself in an orderly fashion(canceled safely).
Cancellation points

Operating System Concepts

4.22

Silberschatz, Galvin and Gagne ©2005

Signal Handling Signals are used in UNIX systems to notify a processthat 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 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 forthe process

Operating System Concepts

4.23

Silberschatz, Galvin and Gagne ©2005

Thread Pools
 Create a number of threads in a poolwhere they await work Advantages:
 Usually slightly faster to service arequest with an existing thread thancreate a new thread Allows the number of threads in theapplication(s) to be bound to the size ofthe pool

Operating System Concepts

4.24

Silberschatz, Galvin and Gagne ©2005

Thread Specific Data Allows each thread to have its own copy ofdata
 Each transaction assigned a unique number inthe transaction-processing system Useful when you do not have control over

the thread creation process (i.e., whenusing a thread pool)

Operating System Concepts

4.25

Silberschatz, Galvin and Gagne ©2005

Scheduler Activations
 Both M:M and Two-level models requirecommunication to maintain the appropriate numberof kernel threads allocated to the application
 Scheduler activations provide upcalls - a

communication mechanism from the kernel to thethread library This communication allows an application tomaintain the correct number kernel threads

Operating System Concepts

4.26

Silberschatz, Galvin and Gagne ©2005

Operating--system Example
Operating Explore how threads are implemented inWindows XP and Linux systems.

Operating System Concepts

4.27

Silberschatz, Galvin and Gagne ©2005

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)Operating System Concepts

4.28

Silberschatz, Galvin and Gagne ©2005

Linux Threads
 Linux refers to them as tasks ratherthan threads
 Thread creation is done throughclone() system call clone() allows a child task to share

the address space of the parent task(process)Operating System Concepts

4.29

Silberschatz, Galvin and Gagne ©2005

Home works
 1, 3, 4, 5, 6 Project- Matrix Multiplication (multi-threadvs. multi-process)Operating System Concepts

4.30

Silberschatz, Galvin and Gagne ©2005

End of Chapter 4