Figure 5.01 - Binghamton University
Download
Report
Transcript Figure 5.01 - Binghamton University
Module 5: Threads
9/29/03+
•
•
•
•
•
•
Overview
Benefits
User and Kernel Threads
Multithreading Models
Solaris 2 Threads
Java Threads
NOTE: Instructor annotations in BLUE
Applied Operating System Concepts
5.1
Silberschatz, Galvin, and Gagne 1999
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 resources
collectively 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
Applied Operating System Concepts
5.2
Silberschatz, Galvin, and Gagne 1999
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).
Applied Operating System Concepts
5.3
Silberschatz, Galvin, and Gagne 1999
Multiple Threads within a Task
Applied Operating System Concepts
5.4
Silberschatz, Galvin, and Gagne 1999
Benefits
•
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 multiprocessor. In a uniprocessor, task switching is so fast that
it gives illusion of parallelism.
Applied Operating System Concepts
5.5
Silberschatz, Galvin, and Gagne 1999
Single and Multithreaded Processes
Applied Operating System Concepts
5.6
Silberschatz, Galvin, and Gagne 1999
User Level (supported) Threads (ULTs)
•
•
•
•
Thread Management Done by User-Level Threads Library
Kernel unaware of ULTs
No 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 …
Applied Operating System Concepts
5.7
Silberschatz, Galvin, and Gagne 1999
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
Applied Operating System Concepts
5.8
Silberschatz, Galvin, and Gagne 1999
Multithreading Models
User Threads / Kernel Threads models.
Three common types of threading implementation:
•
Many-to-One
•
One-to-One
•
Many-to-Many
Applied Operating System Concepts
5.9
Silberschatz, Galvin, and Gagne 1999
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
Applied Operating System Concepts
5.10
Silberschatz, Galvin, and Gagne 1999
Many-to-one Model
Applied Operating System Concepts
5.11
Silberschatz, Galvin, and Gagne 1999
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
Applied Operating System Concepts
5.12
Silberschatz, Galvin, and Gagne 1999
One-to-one Model
Applied Operating System Concepts
5.13
Silberschatz, Galvin, and Gagne 1999
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 kthreads can be run in parallel on a multiprocessor machine.
Applied Operating System Concepts
5.14
Silberschatz, Galvin, and Gagne 1999
Many-to-many Model
Applied Operating System Concepts
5.15
Silberschatz, Galvin, and Gagne 1999
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
Applied Operating System Concepts
5.16
Silberschatz, Galvin, and Gagne 1999
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.
Applied Operating System Concepts
5.17
Silberschatz, Galvin, and Gagne 1999
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.
Applied Operating System Concepts
5.18
Silberschatz, Galvin, and Gagne 1999
Solaris 2 Threads
Applied Operating System Concepts
5.19
Silberschatz, Galvin, and Gagne 1999
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.
Applied Operating System Concepts
5.20
Silberschatz, Galvin, and Gagne 1999
Solaris Process
Applied Operating System Concepts
5.21
Silberschatz, Galvin, and Gagne 1999
Java Threads
•
Java Threads May be Created by:
– Extending Thread class
– Implementing the Runnable interface
Applied Operating System Concepts
5.22
Silberschatz, Galvin, and Gagne 1999
Extending the Thread Class
class Worker1 extends Thread
{
public void run() {
System.out.println(“I am a Worker Thread”);
}
}
Applied Operating System Concepts
5.23
Silberschatz, Galvin, and Gagne 1999
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”);
}
}
Applied Operating System Concepts
5.24
Silberschatz, Galvin, and Gagne 1999
The Runnable Interface
public interface Runnable
{
public abstract void run();
}
Applied Operating System Concepts
5.25
Silberschatz, Galvin, and Gagne 1999
Implementing the Runnable Interface
class Worker2 implements Runnable
{
public void run() {
System.out.println(“I am a Worker Thread”);
}
}
Applied Operating System Concepts
5.26
Silberschatz, Galvin, and Gagne 1999
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”);
}
}
Applied Operating System Concepts
5.27
Silberschatz, Galvin, and Gagne 1999
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.
Applied Operating System Concepts
5.28
Silberschatz, Galvin, and Gagne 1999
Java Thread States
Runnable state has both actively executing
threads, and “ready” threads
Applied Operating System Concepts
5.29
Silberschatz, Galvin, and Gagne 1999
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();
}
}
Applied Operating System Concepts
5.30
Silberschatz, Galvin, and Gagne 1999
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;
}
Applied Operating System Concepts
5.31
Silberschatz, Galvin, and Gagne 1999
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;
}
Applied Operating System Concepts
5.32
Silberschatz, Galvin, and Gagne 1999