Figure 5.01 - Homepage of Professor Ivan A. Escobar Broitman

Download Report

Transcript Figure 5.01 - Homepage of Professor Ivan A. Escobar Broitman

Chapter 4: Threads
Chapter 4: Threads
 Overview
 Multithreading Models
 Threading Issues
 Pthreads
 Windows XP Threads
 Linux Threads
 Java Threads
Operating System Concepts – 7th edition, Jan 23, 2005
4.2
Silberschatz, Galvin and Gagne ©2005
Thread: Definition
 A Thread is a basic unit of CPU utilization.
 A thread of execution.
 A placeholder information associated with a single use of a
program that can handle multiple concurrent users
 Thread

Lightweight process (LWP)

Threads of instructions or thread of control

Shares address space and other global information with its
process

Registers, stack, signal masks and other thread-specific data
are local to each thread
Operating System Concepts – 7th edition, Jan 23, 2005
4.3
Silberschatz, Galvin and Gagne ©2005
Thread Composition
 A Thread is comprised of:

A thread ID.

A program counter.

A register set.

A stack.
Operating System Concepts – 7th edition, Jan 23, 2005
4.4
Silberschatz, Galvin and Gagne ©2005
Process vs Threads
 A traditional or heavyweight process has a single thread of
control.
 If a process has multiple threads of control, it can perform more
than one task at a time, thus a thread is a lightweight process.
Operating System Concepts – 7th edition, Jan 23, 2005
4.5
Silberschatz, Galvin and Gagne ©2005
Single and Multithreaded Processes
Operating System Concepts – 7th edition, Jan 23, 2005
4.6
Silberschatz, Galvin and Gagne ©2005
Benefits
 Responsiveness

A program may continue even if parts of it are blocked.

Ex: web browser loading some images while others take more
time.
 Resource Sharing

By default, threads share memory and the resources of the
process to which they belong.
 Economy

Easier to create and context switch between threads since they
share resources.
 Utilization of MP Architectures

Run multiple threads in parallel, taking advantage of multiple
processors.
Operating System Concepts – 7th edition, Jan 23, 2005
4.7
Silberschatz, Galvin and Gagne ©2005
Benefits
 Takes less time to create a new thread than a process.
 Less time to terminate a thread than a process.
 Less time to switch between two threads within the same process.
 Since threads within the same process share memory and files,
they can communicate with each other without invoking the kernel
Operating System Concepts – 7th edition, Jan 23, 2005
4.8
Silberschatz, Galvin and Gagne ©2005
Multithreading Modes
 Provided in different levels:

User Threads.


Supported above the kernel and managed without kernel
support.
Kernel Threads.

Supported and managed directly by the operating system.
Operating System Concepts – 7th edition, Jan 23, 2005
4.9
Silberschatz, Galvin and Gagne ©2005
User Threads
 Thread management done by user-level threads library.
 The kernel is not aware of the existence of threads.
 Three primary thread libraries:

POSIX Pthreads

Win32 threads

Java threads
Operating System Concepts – 7th edition, Jan 23, 2005
4.10
Silberschatz, Galvin and Gagne ©2005
Kernel Threads
 Supported by the Kernel
 Kernel maintains context information for the process and the
threads
 Examples

Windows XP/2000

Solaris

Linux

Tru64 UNIX

Mac OS X
Operating System Concepts – 7th edition, Jan 23, 2005
4.11
Silberschatz, Galvin and Gagne ©2005
Multithreading Models
 Many-to-One

Maps many user-level threads to a single kernel thread.
 One-to-One

Maps each user-level thread to a kernel thread.
 Many-to-Many

Multiplexes many user-level threads to a smaller or equal
number of kernel threads.
Operating System Concepts – 7th edition, Jan 23, 2005
4.12
Silberschatz, Galvin and Gagne ©2005
Many-to-One
 Many user-level threads mapped to single kernel thread.
 Thread management is done by the thread library in user space.
 Will block if thread makes a blocking system call.
 Multiple threads are unable to run in parallel on multiprocessors.
 Examples:

Solaris Green Threads

GNU Portable Threads
Operating System Concepts – 7th edition, Jan 23, 2005
4.13
Silberschatz, Galvin and Gagne ©2005
Many-to-One Model
Operating System Concepts – 7th edition, Jan 23, 2005
4.14
Silberschatz, Galvin and Gagne ©2005
One-to-One
 Each user-level thread maps to kernel thread.
 Provides more concurrency allowing another thread to run when
one makes a blocking system call.
 Allows multiple threads to run in parallel in multiprocessors.
 Developer must be careful not to make too many threads per
application.
 Examples

Windows NT/XP/2000

Linux

Solaris 9 and later
Operating System Concepts – 7th edition, Jan 23, 2005
4.15
Silberschatz, Galvin and Gagne ©2005
One-to-one Model
Operating System Concepts – 7th edition, Jan 23, 2005
4.16
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.
 Developers can create as many threads as necessary.
 Solaris prior to version 9.
 Windows NT/2000 with the ThreadFiber package.
Operating System Concepts – 7th edition, Jan 23, 2005
4.17
Silberschatz, Galvin and Gagne ©2005
Many-to-Many Model
Operating System Concepts – 7th edition, Jan 23, 2005
4.18
Silberschatz, Galvin and Gagne ©2005
Two-level Model
 Similar to M:M, 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 Concepts – 7th edition, Jan 23, 2005
4.19
Silberschatz, Galvin and Gagne ©2005
Two-level Model
Operating System Concepts – 7th edition, Jan 23, 2005
4.20
Silberschatz, Galvin and Gagne ©2005
Thread State: Life Cycle of a Thread
 Thread states

Born state

Ready state (runnable state)

Running state

Dead state

Blocked state

Waiting state

Sleeping state

Sleep interval specifies for how long a thread will sleep
Operating System Concepts – 7th edition, Jan 23, 2005
4.21
Silberschatz, Galvin and Gagne ©2005
Thread State: Life Cycle of a Thread
Thread Life Cycle:
Operating System Concepts – 7th edition, Jan 23, 2005
4.22
Silberschatz, Galvin and Gagne ©2005
Thread Libraries
 A thread library provides the programmer an API for creating and
mapping threads.
 Two ways of implementing a thread library:

Library entirely in user space with no kernel support.


Invoking a function in the library results in a local call
function not a system call.
Kernel-level library supported directly by the operating system.

Code and structures reside in kernel space.

Invoking a function in the API typically results in a system
call to the kernel.
Operating System Concepts – 7th edition, Jan 23, 2005
4.23
Silberschatz, Galvin and Gagne ©2005
Thread Libraries
 Three main libraries are used today:

POSIX Pthreads.


Win32.


User or kernel level.
Kernel level only on windows systems.
Java.

Java programs.
Operating System Concepts – 7th edition, Jan 23, 2005
4.24
Silberschatz, Galvin and Gagne ©2005
Pthreads
 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)
 Third party support for Windows Systems.
Operating System Concepts – 7th edition, Jan 23, 2005
4.25
Silberschatz, Galvin and Gagne ©2005