Transcript
Page 1: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

Modified from Silberschatz, Galvin and Gagne ©2009

Lecture 7

Chapter 4: Threads (cont)

Page 2: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

2CS 446/646 Principles of Computer Operating Systems

Chapter 4: Threads

Overview

Multithreading Models

Thread Libraries

Threading Issues

Operating System Examples

Windows XP Threads

Linux Threads

Page 3: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

3CS 446/646 Principles of Computer Operating Systems

The execution part is a “thread”

Pasta for six

– boil 1 quart salty water

– stir in the pasta

– cook on medium until “al dente”

– serve

ProgramProcess

CPU

input data

thread of execution

The execution part is a “thread” that can be multiplied

other thread

same CPU workingon two things

Multithreading

Page 4: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

4CS 446/646 Principles of Computer Operating Systems

ex: Solaris, Mach, Windows

ex: Java VMex: MS-DOS

ex: early UNIX

uniprogramming

multiprogramming

Process-Thread relationship

Page 5: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

5CS 446/646 Principles of Computer Operating Systems

Single and Multithreaded Processes

Page 6: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

6CS 446/646 Principles of Computer Operating Systems

Multithreaded Server Architecture

Page 7: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

7CS 446/646 Principles of Computer Operating Systems

Benefits

Responsiveness: Continue even if part of an application is blocked due to I/O

Allow user interaction in one thread while image is loading in another.

Resource Sharing: Memory / resource sharing is by default with threads.

Several different threads of activity within the same address space.

Economy: It is more economical to create and context-switch threads.

In general, creating a process is 30 times slower,

context-switching a process is 5 times slower.

Scalability: Multiprocessor architectures

A single-threaded process can only run on one processor.

Page 8: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

8CS 446/646 Principles of Computer Operating Systems

Multicore Scalability

Parallel Execution on a Multicore System

Concurrent Execution on a Single-core System

Page 9: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

9CS 446/646 Principles of Computer Operating Systems

Multicore Programming

Multicore systems putting pressure on programmers, challenges include

Dividing activities: Finding areas of code that can run in parallel as concurrent tasks.

Balance: Tasks should perform equal work of equal value.

Data splitting: Data manipulated by the tasks must be divided to run on separate cores.

Data dependency: Ensure the execution of the tasks is synchronized to accommodate data dependencies between tasks.

Testing and debugging: Testing a program with multiple execution paths is inherently more challenging.

Page 10: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

10CS 446/646 Principles of Computer Operating Systems

User Threads

Thread management done by user-level threads library

Three primary thread libraries:

POSIX Pthreads

Win32 threads

Java threads

Page 11: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

11CS 446/646 Principles of Computer Operating Systems

Kernel Threads

Supported by the Kernel

Examples

Windows XP/2000

Solaris

Linux

Tru64 UNIX

Mac OS X

Page 12: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

12CS 446/646 Principles of Computer Operating Systems

Multithreading Models

Many-to-One

One-to-One

Many-to-Many

Two-level

Page 13: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

13CS 446/646 Principles of Computer Operating Systems

Many-to-One

Many user-level threads mapped to single kernel thread.

Thread management is done by the thread library in user space,

Is efficient,

But, entire process will block is a thread makes a blocking system call.

Only one thread can access the kernel at a time,

Multiple threads can not run in parallel on multiprocessors.

Examples:

Solaris Green Threads

GNU Portable Threads

Page 14: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

14CS 446/646 Principles of Computer Operating Systems

One-to-One

Each user-level thread maps to kernel thread.

Provides more concurrency.

No blocking due to another thread.

Threads may run in parallel on multiprocessors.

Creating a user thread requires creating the corresponding kernel thread.

Hence, OSes limit the number of threads.

Examples

Windows NT/XP/2000

Linux

Solaris 9 and later

Page 15: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

15CS 446/646 Principles of Computer Operating Systems

Many-to-Many Model

Many user level threads are mapped to many kernel threads.

Allows the operating system to create a sufficient number of kernel threads.

Suffers from neither of shortcomings of many-to-one or one-to-one.

Examples

Windows NT/2000 with the ThreadFiber package

Solaris prior to version 9

Page 16: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

16CS 446/646 Principles of Computer Operating Systems

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

Page 17: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

17CS 446/646 Principles of Computer Operating Systems

Page 18: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

18CS 446/646 Principles of Computer Operating Systems

Thread Libraries

Thread library provides programmer with API for creating and managing threads

Pthreads

Win32 threads

Java threads

Two primary ways of implementing

Library entirely in user space

Kernel-level library supported by the OS

Thread functions invoke system calls.

Page 19: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

19CS 446/646 Principles of Computer Operating Systems

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, implementation is up to development of the library

Common in UNIX operating systems (Solaris, Linux, Mac OS X)

pthread_create()

pthread_join()

Page 20: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

20CS 446/646 Principles of Computer Operating Systems

Java Threads

Java threads are managed by the JVM

Typically implemented using the threads model provided by underlying OS

Java threads may be created by:

Extending Thread class

Implementing the Runnable interface

Must define run() method

Call start() method to initialize a thread

Use join() method to wait for a thread

Page 21: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

21CS 446/646 Principles of Computer Operating Systems

Threading Issues

Semantics of fork() and exec() system calls

Thread cancellation of target thread

Asynchronous or deferred

Signal handling

Thread pools

Thread-specific data

Scheduler activations

Page 22: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

22CS 446/646 Principles of Computer Operating Systems

Semantics of fork() and exec()

Does fork() duplicate only the calling thread or all threads?

UNIX provides both approaches.

exec() works the same

entire process is replaced with the new one

Page 23: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

23CS 446/646 Principles of Computer Operating Systems

Thread Cancellation

Terminating a thread before it has finished

Asynchronous cancellation terminates the target thread immediately

OS may not reclaim all resources

Deferred cancellation allows the target thread to periodically check if it should be cancelled

Page 24: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

24CS 446/646 Principles of Computer Operating Systems

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

1. Signal is generated by particular event

2. Signal is delivered to a process

3. Signal is handled

Options:

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

Page 25: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

25CS 446/646 Principles of Computer Operating Systems

Thread Pools

Create a number of threads in a pool where they await work

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

Page 26: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

26CS 446/646 Principles of Computer Operating Systems

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)

Page 27: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

27CS 446/646 Principles of Computer Operating Systems

Scheduler Activations

Communication between the kernel and thread library.

Both many-to-many and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application

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

Page 28: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

28CS 446/646 Principles of Computer Operating Systems

I/O request/completion

Time T4

User-LevelRuntime System

Kernel

Virtual Processors

Time T1

User-LevelRuntime System

Kernel

Time T3

a s_a is unblocked

Time T2

B

a s_a is blocked

Page 29: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

29CS 446/646 Principles of Computer Operating Systems

Windows XP Threads

Implements the one-to-one mapping, kernel-level

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)

Page 30: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

30CS 446/646 Principles of Computer Operating Systems

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 space of the parent task (process)

Page 31: Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)

Modified from Silberschatz, Galvin and Gagne ©2009

End of Chapter 4


Recommended