chapter 5: threads

28
Silberschatz, Galvin and Gagne 2002 5.1 Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads

Upload: nura

Post on 04-Jan-2016

27 views

Category:

Documents


2 download

DESCRIPTION

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads. Thread vs. Process. A thread – lightweight process (LWP) is a basic unit of CPU utilization. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.1Operating System Concepts

Chapter 5: Threads

Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads

Page 2: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.2Operating System Concepts

Thread vs. Process

A thread – lightweight process (LWP) is a basic unit of CPU utilization.

It comprises a thread ID, a program counter, a register set, and a stack.

A traditional (heavyweight) process has a single thread of control.

If the process has multiple threads of control, it can do more than one task at a time.

Page 3: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.3Operating System Concepts

Single and Multithreaded Processes

Page 4: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.4Operating System Concepts

Motivation

An application typically is implemented as a separate process with several threads of control.

For example, a web browser might have one thread display images or text while another thread retrieves data from the network.

It is more efficient for a process that contains multiple threads to serve the same purpose.

This approach would multithread the web-server process.

Page 5: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.5Operating System Concepts

Benefits

Responsiveness: Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user.

Resource Sharing: Threads share the memory and the resources of the process to which they belong.

Economy: Allocating memory and resources for process creation is costly.

Utilization of MP (multiprocessor) Architectures: Each thread may be running in parallel on a different processor.

Page 6: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.6Operating System Concepts

User Threads

Thread management done by user-level threads library User-level threads are fast to create and manage. If the kernel is single-threaded, then any user-level thread

performing a blocking system call will cause the entire process to block.

Examples

- POSIX Pthreads

- Mach C-threads

- Solaris UI-threads

Page 7: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.7Operating System Concepts

Kernel Threads

Supported by the Kernel: The kernel performs thread creation, scheduling, and management in kernel space.

Examples

- Windows 95/98/NT/2000

- Solaris

- Tru64 UNIX

- BeOS

- OpenBSD

- FreeBSD

- Linux

Page 8: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.8Operating System Concepts

Multithreading Models

Many-to-One: The many-to-one model maps many user-level threads to one kernel thread.

One-to-One: The one-to-one model maps each user thread to a kernel thread.

Many-to-Many: The many-to-many multiplexes many user-level threads to a smaller or equal number of kernel threads.

Page 9: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.9Operating System Concepts

Many-to-One

Many user-level threads mapped to single kernel thread.

Used on systems that do not support kernel threads.

Page 10: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.10Operating System Concepts

Many-to-One Model

Page 11: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.11Operating System Concepts

One-to-One

Each user-level thread maps to kernel thread.

Examples

- Windows 95/98/NT/2000

- OS/2

Page 12: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.12Operating System Concepts

One-to-one Model

Page 13: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.13Operating System Concepts

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.

Solaris 2 Windows NT/2000 with the ThreadFiber package

Page 14: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.14Operating System Concepts

Many-to-Many Model

Page 15: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.15Operating System Concepts

Multithreading Models - Conclusion

Whereas the many-to-one model allows the developer to create as many user threads as she wishes, true concurrency is not gained because the kernel can schedule only one thread at a time.

The one-to-one model allows for greater concurrency, but the developer has to be careful not to create too many threads within an application.

The many-to-many model suffers from neither of these shortcomings: Developer can create as many user threads as necessary, and the corresponding kernel threads can run in parallel on a multiprocessor. Also, when a thread performs a blocking system call, the kernel can schedule another thread for execution.

Page 16: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.16Operating System Concepts

Threading Issues

Semantics of fork() and exec() system calls: Two versions of fork are one that duplicates all threads and another that duplicates only the thread that invoked the fork system call.

Thread cancellation is the task of terminating a thread before it has completed.

• Two different scenarios are asynchronous cancellation and deferred cancellation.

• A thread checks if it should be cancelled at a point referred as cancellation point.

Signal handling Thread pools Thread specific data is a copy of data owned by a

thread.

Page 17: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.17Operating System Concepts

Signal Handling

A signal is used in UNIX systems to notify a process that a particular event has occurred.

All signals follow the same pattern:1. A signal is generated by the occurrence of a particular

event.

2. A generated signal is delivered to a process.

3. Once delivered, the signal must be handled. A signal can be synchronous or asynchronous:

• Synchronous signals are delivered to the same process.• Asynchronous signal is generated by an external event.

Page 18: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.18Operating System Concepts

Signal Handling

Every signal may be handled by one of two possible handlers: • A default signal handler• A user-defined signal handler

The options exist for delivering signals to a multithreaded program:1. Deliver the signal to the thread to which the signal applies.

2. Deliver the signal to every thread in the process.

3. Deliver the signal to certain threads in the process.

4. Assign a specific thread to receive all signals for the process.

Windows 2000 uses asynchronous procedure calls (APCs) to emulate signals.

Page 19: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.19Operating System Concepts

Thread Pools

A multithread server has potential problems: • The time required includes thread creating and discarding.• Unlimited threads could exhaust system resources.

One solution to this issue is to use thread pools. A thread pool has a number of threads being created at

process startup, where they sit and wait for work. The benefits of thread pools are:

• It is usually faster to service a request with an existing thread.

• A thread pool limits the number of threads at any one point.

Page 20: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.20Operating System Concepts

Thread-Specific Data

A multithread server has potential problems: • The time required includes thread creating and discarding.• Unlimited threads could exhaust system resources.

One solution to this issue is to use thread pools. A thread pool has a number of threads being created at

process startup, where they sit and wait for work. The benefits of thread pools are:

• It is usually faster to service a request with an existing thread.

• A thread pool limits the number of threads at any one point.

Page 21: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.21Operating System Concepts

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. Refer to the program shown in Figure 5.5.

Page 22: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.22Operating System Concepts

Solaris 2 Threads

Solaris 2 is a version of UNIX with support for threads at the kernel and user levels, SMP, and real-time scheduling.

Solaris 2 implements the Pthread API and UI threads. Between user- and kernel-level threads are ligthweight

processes (LWPs). Threads in a process multiplex to connect an LWP. An

LWP corresponds a kernel thread. A (un)bound user-level thread is (not) permanently

attached to an LWP.

Page 23: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.23Operating System Concepts

Solaris 2 Threads

Page 24: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.24Operating System Concepts

Solaris Process

Page 25: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.25Operating System Concepts

Windows 2000 Threads

Implements the one-to-one mapping. Each thread contains

- a thread id

- register set

- separate user and kernel stacks

- private data storage area

Page 26: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.26Operating System Concepts

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 27: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.27Operating System Concepts

Java Threads

Java threads may be created by:

Extending Thread class Implementing the Runnable interface

Calling the start method for the new object does two things:

1. It allocates memory and initializes a new thread in the JVM.

2. It calls the run method, making the thread eligible to be run by JVM.

Java threads are managed by the JVM.

Page 28: Chapter 5: Threads

Silberschatz, Galvin and Gagne 20025.28Operating System Concepts

Java Thread States