silberschatz, galvin, and gagne 1999 5.1 applied operating system concepts module 5: threads...

32
Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.1 Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading Models Solaris 2 Threads Java Threads NOTE: Instructor annotations in BLUE

Post on 20-Dec-2015

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.1

Module 5: Threads9/29/03+

• Overview

• Benefits

• User and Kernel Threads

• Multithreading Models

• Solaris 2 Threads

• Java Threads

NOTE: Instructor annotations in BLUE

Page 2: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.2

Threads - Overview

• A thread (or lightweight process) is a basic unit of CPU utilization; it consists of:

– program counter– register set – stack space

• A thread shares with its peer threads (in same process) its:– code section– data section– operating-system resourcescollectively know as a task.

Actually the three things above belong to the process Threads in the process share this stuff.

• A traditional or heavyweight process is equal to a task with one thread

Page 3: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.3

Threads (- Overview Cont.)

• In a multiple threaded task, while one server thread is blocked and waiting, a second thread in the same task can run. (assuming the blocked thread doesn’t block the process - if so, everything comes to a screeching halt!)

– Cooperation of multiple threads in same job confers higher throughput and improved performance.

– Applications that require sharing a common buffer (i.e., producer-consumer) benefit from thread utilization.

• Threads provide a mechanism that allows sequential processes to make blocking system calls while also achieving parallelism.… a thread within the process makes the call & only it blocks while other threads in the process still run …assuming kernel supported threads

• Kernel-supported threads – can reside in user or kernel space, but kernel is involved in their control.

• User-level (supported) threads; supported from a set of library calls at the user level. Resides in user space.

• Hybrid approach implements both user-level and kernel-supported threads (Solaris 2).

Page 4: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.4

Multiple Threads within a Task

Page 5: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.5

• Responsiveness - Opens the possibility of blocking only one thread in a process on a blocking call rather than the entire process.

• Resource Sharing – Threads share resources of process to which they belong – code sharing allow multiple instantiations of code execution

• Economy – low overhead in thread context switching & thread management compared to process context switching & management

• Utilization of MP Architectures – can be applied to a multi-processor. In a uniprocessor, task switching is so fast that it gives illusion of parallelism.

Benefits

Page 6: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.6

Single and Multithreaded Processes

Page 7: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.7

User Level (supported) Threads (ULTs)

• Thread Management Done by User-Level Threads Library

• Kernel unaware of ULTsNo kernel intervention needed for thread management.Thus fast to create and manage

• If thread blocks, the kernel sees it as the process blocking, and may block the entire process (all threads).

• Examples

- POSIX Pthreads

- Mach C-threads

- Solaris threads … but not totally kernel independent …

Page 8: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.8

Kernel Level (supported) Threads (KLTs)

• Supported by the Kernel – thread management done by kernel in kernel space.

• Since kernel managing threads, kernel can schedule another thread if a given thread blocks rather than blocking the entire processes.

• Examples

- Windows 95/98/NT

- Solaris

- Digital UNIX

Page 9: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.9

Multithreading Models

User Threads / Kernel Threads models.

Three common types of threading implementation:

• Many-to-One

• One-to-One

• Many-to-Many

Page 10: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.10

Many-to-One

• Many User-Level Threads Mapped to Single Kernel Thread.

• Thread management done at user level - efficient

• Entire user processes blocks if one of its threads block – kernel only sees the process.

• Bad for multiprocessor (parallel) architectures because only one thread at a time can access kernel.

• Used on Systems That Do Not Support Kernel Threads. - Single threaded kernel

Page 11: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.11

Many-to-one Model

Page 12: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.12

One-to-One

• Each User-Level Thread Maps to a distinct Kernel Thread.

• Other threads can run when a particular thread makes a blocking call - better concurrency.

• Multiple threads can run in parallel in a multiprocessing architecture.

• Drawback is that creating a user thread requires creating a kernel thread … overhead factor … number of threads restricted

• Examples

- Windows 95/98/NT

- OS/2

Page 13: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.13

One-to-one Model

Page 14: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.14

Many-to-Many Model

• Time Multiplexes many ULT’s to a smaller of equal number of KLT’s.

• Number of kernel threads may be specific to either a particular application or machine … typically a multiprocessor may get more k-threads than a uniprocessor.

• Programmer may create as many user threads as desired (compare to 1-to-1 in which there my be a shortage of k-threads.

• True concurrency limited to scheduling (multiplexing) many user threads against a less number of k-threads. But the multiple k-threads can be run in parallel on a multiprocessor machine.

Page 15: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.15

Many-to-many Model

Page 16: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.16

Pthreads

• Example of user controlled threads

• Refers to the POSIX standard (IEEE 1003.1c) API - specifies behavior, not implementation.

• More commonly implemented on UNIX based system such as Solaris

• Runs from a Pthead user level library with not specified relationship and any associated kernel threads.

– Some implementations may allow some kernel control - ex: scheduling???

• The “main” function in the C application program is the initial thread created by default, all other threads are created using library calls from the application program- each thread has stack

Page 17: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.17

Pthreads (continued)

• Two typical Pthread library calls:

– pthread_create creates a new thread allows you to pass the name of a function which has will be

the behavior or function that this thread will do. Allows you to pass parameters to the function of the new

thread. sets a thread id variable to the unique “tid”.

– Pthread_join allows a thread to wait for another specified thread to complete

before continuing on - a synchronization tedchique

• There is a very large and rich set of library function calls to use in Pthread programming - Lots of calls for thread synchronization - more later!

• See example in Figure 5.5, page 140.

Page 18: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.18

Threads Support in Solaris 2

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

• Solaris 2 implements the pthread API, in addition to supporting userlevel threadswith a thread library with API’s for thread creation & management ==> Solaris threads

• LWP – intermediate level between user-level threads and kernel-level threads.

• Resource needs of thread types:– Kernel thread (are in the kernel - does OS stuff or associates with an LWP) :

small data structure and a stack; thread switching does not require changing memory access information – relatively fast.

– LWP: Associated with a both a user process (PCB) and a kernel thread. Has register data, accounting, and memory information; switching between LWPs is relatively slow. - because of kernel intervention. Scheduled by kernel

– User-level thread: only need stack and program counter; no kernel involvement means fast switching (multiplexing to LPW) . Kernel only sees the LWPs that are dedicated to user-level threads.

Page 19: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.19

Solaris 2 Threads

Page 20: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.20

Solaris Threads

• Threads in user space similar to “pure” Pthreads but with required kernel control

• Process Structure control information completely in kernel space

• LWP in kernel space but share the user process resources - can interface with user threads - is visible to user threads.

• LWP can be thought of as a virtual CPU that is available for executing code .. . We have a single process in one memory space with many virtual “CPU’s” running different parts of the process concurrently (Lewis & Berg).

• Each LWP is separately scheduled by kernel - kernel schedules LWP’s not user threads.

• The thread library for user threads is in user space - the user level thread library schedules (multiplexes) user threads onto LWP’s, and LWP’s, in turn, are implemented by kernel threads and scheduled by the kernel.

Page 21: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.21

Solaris Process

Page 22: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.22

Java Threads

• Java Threads May be Created by:

– Extending Thread class

– Implementing the Runnable interface

Page 23: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.23

Extending the Thread Class

class Worker1 extends Thread

{

public void run() {

System.out.println(“I am a Worker Thread”);

}

}

Page 24: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.24

Creating the Thread

public class First

{

public static void main(String args[]) {

Worker runner = new Worker1();

runner.start();

System.out.println(“I am the main thread”);

}

}

Page 25: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.25

The Runnable Interface

public interface Runnable

{

public abstract void run();

}

Page 26: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.26

Implementing the Runnable Interface

class Worker2 implements Runnable

{

public void run() {

System.out.println(“I am a Worker Thread”);

}

}

Page 27: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.27

Creating the Thread

public class Second

{

public static void main(String args[]) {

Runnable runner = new Worker2();

Thread thrd = new Thread(runner);

thrd.start();

System.out.println(“I am the main thread”);

}

}

Page 28: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.28

Java Thread Management

• suspend() – suspends execution of the currently running thread.

• sleep() – puts the currently running thread to sleep for a specified amount of time.

• resume() – resumes execution of a suspended thread.

• stop() – stops execution of a thread.

Page 29: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.29

Java Thread States

Runnable state has both actively executing threads, and “ready” threads

Page 30: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.30

Producer Consumer Problem

public class Server {

public Server() {

MessageQueue mailBox = new MessageQueue();

Producer producerThread = new Producer(mailBox);

Consumer consumerThread = new Consumer(mailBox);

producerThread.start();

consumerThread.start();

}

public static void main(String args[]) {

Server server = new Server();

}

}

Page 31: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.31

Producer Thread

class Producer extends Thread {

public Producer(MessageQueue m) {

mbox = m;

}

public void run() {

while (true) {

// produce an item & enter it into the buffer

Date message = new Date();

mbox.send(message);

}

}

private MessageQueue mbox;

}

Page 32: Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading

Applied Operating System Concepts Silberschatz, Galvin, and Gagne 1999 5.32

Consumer Thread

class Consumer extends Thread {

public Consumer(MessageQueue m) {

mbox = m;

}

public void run() {

while (true) {

Date message = (Date)mbox.receive();

if (message != null)

// consume the message

}

}

private MessageQueue mbox;

}