Multithreading
Most modern applications are multithreaded
Threads run within application
Operating Systems I
Multiple tasks with the application can be implemented
by separate threads
Update display
Fetch data
Unit 6 – Threads
Spell checking
Answer a network request
Prof. Dr. Alejandro Zunino
Process creation is heavy-weight while thread creation
is light-weight
ISISTAN - CONICET
Can simplify code, increase efficiency
Kernels are generally multithreaded
Example Chapter 5: Threads
Overview
Multithreading Models
Threading Issues
Thread programming
Single and Multithreaded
Process
Processes
Have a virtual address space which holds the
process image
Protected access to processors, other
processes, files, and I/O resources
Thread Threads (Cont.)
In a multiple threaded task, while one server thread is
An execution state (running, ready, etc.) blocked and waiting, a second thread in the same task can
run.
Saved thread context when not running
Cooperation of multiple threads in same job confers higher
throughput and improved performance.
Has an execution stack
Applications that require sharing a common buffer (i.e.,
producer-consumer) benefit from thread utilization.
Some per-thread static storage for local
Threads provide a mechanism that allows sequential
variables processes to make blocking system calls while also
achieving parallelism.
Access to the memory and resources of its
Kernel-supported threads (Mach, Solaris 2, Linux, NT).
process
User-level threads; supported above the kernel, via a set of
library calls at the user level (Project Andrew from CMU).
all threads of a process share this
Hybrid approach implements both user-level and kernel-
supported threads (Solaris 2, Linux).
Multicore Programming Threads
Multicore or multiprocessor systems putting pressure on
A thread (or lightweight process) is a basic
programmers, challenges include: unit of CPU utilization; it consists of:
Dividing activities
program counter
Balance
register set
Data splitting
stack space
Data dependency
Testing and debugging
A thread shares with its peer threads its:
Parallelism implies a system can perform more than
code section
one task simultaneously
data section
Concurrency supports more than one task making
operating-system resources
progress collectively know as a task.
Single processor / core, scheduler providing
A traditional or heavyweight process is equal
concurrency to a task with one thread
Benefits Concurrency vs. Parallelism
Responsiveness
Concurrent execution on single-core system:
may allow continued execution if part of process is
blocked, especially important for user interfaces
Resource Sharing
threads share resources of process, easier than
shared memory or message passing
Economy
Parallelism on a multi-core system:
cheaper than process creation, thread switching
lower overhead than context switching
Scalability
process can take advantage of multicore
architectures
Multicore Programming: types of
Kernel Threads
parallelism
Data parallelism
Kernel maintains context information for the
distributes subsets of the same data across multiple process and the threads
cores, same operation on each
Task parallelism
Scheduling is done on a thread basis
distributing threads across cores, each thread performing
Examples
unique operation
Windows 95/98/NT/2000
Linux, Android
MacOS
Multithreading Models User-Level Threads
Many-to-One
Thread management done by user-level
threads library
One-to-One
The kernel is not aware of the existence of
threads
Many-to-Many
Combined Approaches Many-to-One
Thread creation done in the user space
Many user-level threads mapped to single
kernel thread.
One thread blocking causes all to block
Bulk of scheduling and synchronization of
Used on systems that do not support kernel
threads done in the user space threads or for specific needs
One-to-One Thread Programming
Each user-level thread maps to kernel
Usually through a thread library:
thread
provides programmer with API for creating and
More concurrency than many-to-one managing threads
Number of threads per process sometimes
Two primary ways of implementing
restricted due to overhead
Library entirely in user space
Examples:
Kernel-level library supported by the OS
Windows
Linux
Pthreads 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.
Hard to implement
Requires a user level thread scheduler
Pthreads Java Threads
May be provided either as user-level or
Java threads are managed by the JVM
kernel-level
Typically implemented using the threads
A POSIX standard (IEEE 1003.1c) API for model provided by underlying OS
thread creation and synchronization.
Java threads may be created by:
Extending Thread class
API specifies behavior of the thread library,
Implementing the Runnable interface
implementation is up to development of
the library.
Common in UNIX operating systems.
Java Threads A simple Web server
Runnable
Implementing Runnable <<interface>>
class Producer implements Runnable { HTTPRequestHandler socket = s;
public void run() { is = [Link]();
while(true) { // do some work } final static String CRLF="\r\n" os = [Link]();
Socket socket br = new BufferedReader(
} new InputStreamReader( is ) );
InputStream is
} OutputStream os
BufferedReader br processRequest();
Creating a thread: HTTPRequestHandler(Socket s)
run()
thread producer = new Producer(); byte buffer[] = new byte[1024];
processRequest() int bytes = 0;
[Link](); sendBytes(FileInputStream fis, while((bytes = [Link](buffer))!= -1 )
OutputStream os) [Link]( [Link]( buffer, 0, bytes ) );
Waiting for a thread String contentType(String fileName)
try { if( [Link](".htm") ||
[Link](".html" )
[Link](); return "text/html";
...
} catch (InterruptedException e) { }
A simple Web server A simple Web server
// Construct the response message. HTTPServer s = new ServerSocket(80);
String serverLine = "Server: simple java httpServer"; run();
String statusLine = null; ServerSocket s
String contentTypeLine = null; Main() while(true) {
String entityBody = null; run() Socket sock = [Link]();
String contentLengthLine = "error"; [Link]("New connection accepted " +
if ( fileExists ) { [Link]() + ":" + [Link]());
statusLine = "HTTP/1.0 200 OK" + CRLF ; try {
contentTypeLine = "Content-type: " + HTTPRequestHandler request =
new HTTPRequestHandler(sock);
contentType( fileName ) + CRLF ;
Thread thread = new Thread( request );
contentLengthLine = "Content-Length: "
[Link]();
+ (new Integer([Link]())).toString() + CRLF; } catch ...
} creates
else {
statusLine = "HTTP/1.0 404 Not Found" + CRLF ;
contentTypeLine = "text/html" ;
Runnable
entityBody = "<HTML>" + <<interface>>
"<HEAD><TITLE>404 Not Found</TITLE></HEAD>" +
"<BODY>404 Not Found"
+"<br>usage:[Link] HTTPRequestHandler
+"[Link]</BODY></HTML>" ;
}
A simple Web server A simple Web server
// Send the status line.
while(true) { [Link]([Link]());
String headerLine = [Link](); // Send the server line.
[Link](headerLine); [Link]([Link]());
if([Link](CRLF) || [Link]("")) break; // Send the content type line.
[Link]([Link]());
StringTokenizer s = new StringTokenizer(headerLine); // Send the Content-Length
String temp = [Link](); [Link]([Link]());
// Send a blank line to indicate the end of the header lines.
if([Link]("GET")) { [Link]([Link]());
String fileName = [Link](); // Send the entity body.
fileName = "." + fileName ; if (fileExists) {
sendBytes(fis, output) ;
// Open the requested file. [Link]();
FileInputStream fis = null ; }
boolean fileExists = true ; else [Link]([Link]());
try { }
fis = new FileInputStream( fileName ) ; }
} try {
catch ( FileNotFoundException e ) { [Link]();
fileExists = false ; [Link]();
} [Link]();
} catch(Exception e) {}