Transcript threads

Chapter 4:
Multithreaded Programming
Chapter 4: Multithreaded Programming
 Overview
 Multithreading Models
 Thread Library
 Threading Issues
 Linux Threads
Operating System Principles
4.2
Silberschatz, Galvin and Gagne ©2005
4.1 Overview
Single and Multithreaded Processes
Operating System Principles
4.3
Silberschatz, Galvin and Gagne ©2005
Benefits
 Responsiveness:

Allow an interactive program to continue running even if part
of it is blocked or is performing a lengthy operation
 Resource Sharing

Threads share the memory and the resources of the process
 Economy

It is more economical to create and context-switch threads.
For example, in Solaris, creating a process is 30 times slower
than creating a thread, and context switching a process is 5
times slower than context switching a thread
 Utilization of Multiprocessor Architectures

Threads may be running in parallel on different processors
Operating System Principles
4.4
Silberschatz, Galvin and Gagne ©2005
4.2 Multithreading Models
 User Threads

Thread management done by user-level threads library

Three primary thread libraries:

POSIX Pthreads, Win32 threads, Java threads
 Kernel threads

Supported by the Kernel

Examples:

Windows XP/2000, Solaris, Linux, Tru64 UNIX, Mac OS X
 How to establish the relationship between user and kernel threads

Many-to-One

One-to-One

Many-to-Many
Operating System Principles
4.5
Silberschatz, Galvin and Gagne ©2005
Many-to-One
 Many user-level threads mapped to single kernel thread
 Examples:

Solaris Green Threads, GNU Portable Threads
Thread management is done in user
space, so it is efficient.
If a thread makes a blocking system
call, then the entire process will block
Operating System Principles
4.6
Silberschatz, Galvin and Gagne ©2005
One-to-One
 Each user-level thread maps to kernel thread
 Examples

Windows NT/XP/2000, Linux, Solaris 9 and later
 Drawback:

creating a user thread requires creating the corresponding
kernel thread

Restrict the number of threads supported by the system
Operating System Principles
4.7
Silberschatz, Galvin and Gagne ©2005
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
 Examples: Solaris prior to version 9, Windows NT/2000 with the
ThreadFiber package
Operating System Principles
4.8
Silberschatz, Galvin and Gagne ©2005
Two-level Model
 Similar to Many-to-Many, except that it allows a
user thread to be bound to kernel thread
 Examples

IRIX, HP-UX, Tru64 UNIX, Solaris 8 and earlier
Operating System Principles
4.9
Silberschatz, Galvin and Gagne ©2005
4.3 Thread Libraries
 Two approaches of implementing a thread library

Provide the library entirely in user space with no kernel support

Implement a kernel-library supported directly by the operating
system
 Three main thread libraries in use

Posix Pthreads: either a user-level or kernel-level library

Win32: kernel library

Java: Java threads are managed by the JVM. Depending on
the OS, either a user-level or kernel-level library
Skip 4.3.1 – 4.3.3
Operating System Principles
4.10
Silberschatz, Galvin and Gagne ©2005
4.4 Threading Issues
 Semantics of fork() and exec() system calls
•
Does fork() duplicate only the calling thread or all threads?
•
If exec( ) is called immediately after forking, then duplicating
only the calling thread
•
If the child process does not call exec() after forking, then the
child process should duplicate all threads
Operating System Principles
4.11
Silberschatz, Galvin and Gagne ©2005
Thread Cancellation
 Terminating a thread before it has finished

Example: when a user presses the stop button on the
browser
 Two general approaches in canceling of a target
thread:

Asynchronous cancellation terminates the target thread
immediately

Deferred cancellation allows the target thread to
periodically check if it should be cancelled

Difficulty: in cases where resources have been allocated to a
canceled thread or where a thread is canceled while in the
midst of updating data it is sharing with other threads
Operating System Principles
4.12
Silberschatz, Galvin and Gagne ©2005
Signal Handling (1)
 Signals are used in UNIX systems to notify a process
that a particular event has occurred
 A signal handler is used to process signals
1.
Signal is generated by particular event
2.
Signal is delivered to a process
3.
Once delivered, the signal is handled
 Synchronous vs. Asynchronous signals

Synchronous signals are delivered to the same process that
caused the signal. Example: illegal memory access

Asynchronous signals are generated by an event external to
a running process
Operating System Principles
4.13
Silberschatz, Galvin and Gagne ©2005
Signal Handling (2)
 Signal handler

A default signal handler

A user-defined signal handler
 Options in delivering signals in multi-threaded
programs:

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
Operating System Principles
4.14
Silberschatz, Galvin and Gagne ©2005
Thread Pools
 Create a number of threads in a pool where they await
work
 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
Thread Specific Data
 Allows each thread to have its own copy of data
 Useful when you do not have control over the thread
creation process (i.e., when using a thread pool)
Operating System Principles
4.15
Silberschatz, Galvin and Gagne ©2005
Scheduler Activations
 Both Many-to-Many and Two-level models require
communication to maintain the appropriate number
of kernel threads allocated to the application

Solution: use an intermediate data structure -lightweight
process, which appears to be a virtual processor
 Scheduler activations provide upcalls - a
communication mechanism from the kernel to the
virtual processor

This communication allows an application to maintain the
correct number kernel threads
Operating System Principles
4.16
Silberschatz, Galvin and Gagne ©2005
4.5 Operating System Examples
 Linux refers to them as tasks rather than threads
 Thread creation is done through clone() system call
 clone() allows a child task to specify how much sharing
is to take place between the child and parent task
(process) by passing a set of flags, like CLONE_FS,
CLONE_VM, CLONE_SIGHAN and CLONE_FILES

This is through a kernel data structure for tasks that contains
pointers to the stored data
Skip 4.5.1
Operating System Principles
4.17
Silberschatz, Galvin and Gagne ©2005