Anda di halaman 1dari 20

Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 1

Chapter 5: Threads
Overview
Multithreading Models
Thread Libraries
Thread Pools

Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 2
Overview
Many software packages are multi-threaded
Web browser: one thread display images, another thread retrieves data
from the network
Word processor: threads for displaying graphics, reading keystrokes
from the user, performing spelling and grammar checking in the
background
Web server: instead of creating a process when a request is received,
which is time consuming and resource intensive, server creates a thread
to service the request
A thread is sometimes called a lightweight process
It is comprised over a thread ID, program counter, a register set and a
stack
It shares with other threads belonging to the same process its code
section, data section and other OS resources (e.g., open files)
A process that has multiples threads can do more than one task at a time

Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 3
Single and Multithreaded Processes
Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 4
Benefits
Responsiveness
One part of a program can continue running even if another
part is blocked
Resource Sharing
Threads of the same process share the same memory space and
resources
Economy
Much less time consuming to create and manage threads than
processes
Solaris 2: creating a process is 30 times slower than creating a
thread, context switching is 5 times slower
Utilization of MP Architectures
Each thread can run in parallel on a different processor
Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 5
User Threads
Thread management done by user-level threads library is
without the intervention of the kernel
Fast to create and manager
If the kernel is single threaded, any user-level thread
performing a blocking system call will cause the entire process
to block
User thread libraries
- POSIX Pthreads
- Mach C-threads
- Solaris 2 UI-threads
Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 6
Kernel Threads
Supported by the Kernel
Slower to create and manage than user threads
If thread performs a blocking system call, the kernel can
schedule another thread in the application for execution
In multi-processor environments, the kernel can schedule
threads on multiple processors
Examples
- Windows 95/98/NT/2000
- Solaris
- Tru64 UNIX
- BeOS
- Linux
Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 7
Multithreading Models
Many-to-One

One-to-One

Many-to-Many

Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 8
Many-to-One
Many user-level threads mapped to single kernel thread.
Efficient - thread management done in user space
Entire process will block if a thread makes a blocking system call
Only one thread can access the kernel, no parallel processing in MP
environment
GNU Portable threads and Green threads (thread library from Solaris)
use this model
Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 9
One-to-One
Each user-level thread maps to kernel thread.
Another thread can run when one thread makes a blocking call
Multiple threads an run in parallel on a MP machine
Overhead of creating a kernel thread for each user thread
Most implementations limit the number of threads supported
Examples
- Windows 95/98/NT/2000
- OS/2
- Linux
Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 10
Many-to-Many Model
Allows many user level threads to be mapped to smaller or equal
number of kernel threads.
As many user threads as necessary can be created
Corresponding kernel threads can run in parallel on a multiprocessor
When a thread performs a blocking system call, the kernel can
schedule another thread for execution
Solaris 9, Windows NT/2000 with the ThreadFiber package
Allows true concurrency in a
MP environment and does not
restrict number of threads that
can be created
Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 11
Two Level Model
Has all the functionality of the many-to
many model but also allows a user-level
thread to be bound to a kernel thread

Supported by Solaris prior to version 9,
IRIX, HP-UX and Tru64 UNIX
Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 12
Thread Libraries
API for creating and managing threads
No kernel support, strictly in use space so no system calls
involved
Kernel level directly supported by the OS. All code and data
structures for the library exits in kernel spacer
An API call typically invokes a system call
Three main libraries in use
POSIX (Portable Operating System Interface) threads
Win32
Java

Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 13
Pthreads
Pthreads refers to the POSIX standard (IEEE 1003.1c)
deifininf an API for thread creation and synchronization
Pthreads is an IEEE and Open Group certified product
The Open Group is a vendor-neutral and technology-neutral
consortium, whose vision of Boundaryless Information Flow
will enable access to integrated information, within and
among enterprises, based on open standards and global
interoperability.
This is a specification for thread behavior not an
implementation
Implemented by Solaris, Linux, MacOS X and Tru64

Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 14
Windows XP Threads
Implements the one-to-one mapping
Each thread contains
A thread id
Register set
Separate user and kernel stacks
Private data storage area
A Windows XP application runs as a separate
process and each process may contain one or
more threads
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)
Also provides support for a fiber library which
implements the many-to-many model
Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 15
Java Threads
Java threads may be created by extending the Thread class or implementing the
Runnable interface
Java threads are managed by the JVM
Not really a user- or kernel-level thread, support provided at the language level
No global data in Java, data is shared by passing the reference to the appropriate
threads
The JVM specification does not indicate how threads should be mapped to the
underlying OS
Windows 95/98/NT/2000 use the one-to-one model (each Java thread maps to a
kernel thread)
Solaris 2 used the many-to-many model
See Producer Consumer Example


Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 16
Java Threads
public class Buffer {
final static int BUFFER_SIZE = 5;
static int buffer[] = {-1,-1,-1,-1,-1};
static int out=0;
}



public class ProdConDriver {

public static void main(String args[]){
Producer prod = new Producer();
Consumer cons = new Consumer();

prod.start();
cons.start();

}
}
public class Producer extends Thread{
public void run(){
for(int i=0;i<10;i++){

while (((Buffer.in+1)%Buffer.BUFFER_SIZE) == Buffer.out);
System.out.print("\nIndex " + Buffer.in + " produced ");
Buffer.buffer[Buffer.in] = (int)(Math.random()*100);

for (int j=0; j<=Buffer.buffer.length-1;j++)
System.out.print("["+Buffer.buffer[j]+"] ");

Buffer.in = (Buffer.in+1)%Buffer.BUFFER_SIZE;
}
}
}


public class Consumer extends Thread{
public void run(){
for(int i=0;i<10;i++){

while (Buffer.in == Buffer.out);
System.out.print("\nIndex " + Buffer.out + " consumed ");
Buffer.buffer[Buffer.out] = -1;

for (int j=0; j<=Buffer.buffer.length-1;j++)
System.out.print("["+Buffer.buffer[j]+"] ");

Buffer.out = (Buffer.out+1)%Buffer.BUFFER_SIZE;
}
}
}
Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 17
Java Thread States
Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 18
Thread Pools
Though creating a new thread is more efficient than creating a new process,
there are still some issues
There is a delay in creating the thread and it will be discarded when done
There is no limit put on how many threads are allowed to be created and
system resources could be exhausted
Create a number of threads in a pool where they await work. When needed,
thread is awoken, when finished returns to pool to await more work
Advantages:
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
Size of the pool can be dynamically adjusted based on usage patterns

Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 19
import java.util.Random;

public class Matrix {
public static int[][] A, B, C;
public int n;

public Matrix() {
n = 3;
A = new int[n][n]; B = new int[n][n]; C = new int[n][n];
Random r = new Random();
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
A[i][j] = r.nextInt() % 10;
B[i][j] = r.nextInt() % 10;
}
}

public void doWork() {
MultThread[][] mt = new MultThread[n][n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
mt[i][j] = new MultThread(3, i, j);
mt[i][j].start();
}

try {
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
mt[i][j].join();
} catch(InterruptedException ie) {}

System.out.println("\nA ="); printMatrix(A);
System.out.println("\nB ="); printMatrix(B);
System.out.println("\nC ="); printMatrix(C);

}


public void printMatrix(int[][] M) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
System.out.print("\t"+M[i][j]);
System.out.println();
}
}


public static void main(String args[]) {
Matrix m = new Matrix();
m.doWork();
}

} // end of class Matrix


public class MultThread extends Thread {
private int n; // n*n matrix
private int i, j; // row, column

public MultThread(int size, int ii, int jj) {
n = size; i = ii; j = jj;
System.out.println("Thread for C["+i+"]["+j+"]");

}

public void run() {
Matrix.C[i][j] = 0;
for(int x = 0; x < n; x++)
Matrix.C[i][j] += Matrix.A[i][x] * Matrix.B[x][j];
}
}
Matrix Multiplication with Java Threads
Silberschatz / OS Concepts / 6e - Chapter 5 Threads Slide 20
Thread for C[0][0]
Thread for C[0][1]
Thread for C[0][2]
Thread for C[1][0]
Thread for C[1][1]
Thread for C[1][2]
Thread for C[2][0]
Thread for C[2][1]
Thread for C[2][2]

A =
-4 5 -8
8 -4 -7
3 -9 6

B =
5 6 0
-7 0 0
-3 8 1

C =
-31 -88 -8
89 -8 -7
60 66 6

Matrix Multiplication with Java Threads

Anda mungkin juga menyukai