Comp 655 - Processes, Threads, etc
Download
Report
Transcript Comp 655 - Processes, Threads, etc
Processes - outline
• Process & thread basics
• Multi-threaded clients
• Server basics
• Server design issues
• Code Migration
3/31/2016
Distributed Systems - Comp 655
1
If you remember one thing…
• Threads execute
• Processes don’t
A thread owns
A process owns
–
–
–
–
–
–
–
–
–
–
Id
Program counter
Scheduling priority
Stack
Local storage
3/31/2016
Id
Thread table
Page tables
Authenticated identity
IO resources
Distributed Systems - Comp 655
2
Threads in Java
Threads are objects
• … like almost everything in Java
• To make one,
1. Define a class that
•
•
Extends java.lang.Thread, or
Implements java.lang.Runnable
2. At runtime, create and initialize an instance
3. Call the start method:
•
•
3/31/2016
mythread.start(); or
new Thread(myrunnable).start();
Distributed Systems - Comp 655
3
Java thread example
public class Worker implements Runnable {
private long myInterval;
private int myReps;
public Worker(long interval, int repetitions) {
myInterval = interval;
myReps = repetitions;
}
public void run() {
String me = Thread.currentThread().getName();
for( int repsDone=1; repsDone<=myReps; repsDone++) {
System.out.println(me+" ["+(repsDone)+"]");
try{ Thread.sleep(myInterval); }
catch(Exception e) {}
}
}
}
3/31/2016
Distributed Systems - Comp 655
4
Starting the thread example
Public static void main(String names[]) {
for( int i=0; i<names.length; i++ ) {
Worker w = new Worker(1000,7);
Thread t = new Thread(w);
t.setName(names[i]);
t.start();
}
}
3/31/2016
Distributed Systems - Comp 655
5
Possible output
java ThreadExample hey whats up
hey [1]
whats [1]
up [1]
hey [2]
whats [2]
up [2]
…
3/31/2016
Distributed Systems - Comp 655
6
Why threads?
• Sometimes you have to block
– usually for IO
• Context switching (for processes) is expensive
• Why is context switching expensive?
– Memory management
• Why do memory management?
– Concurrency transparency
3/31/2016
Distributed Systems - Comp 655
7
Context switching
The most expensive
operations
3/31/2016
Distributed Systems - Comp 655
8
Lightweight processes
• User-level thread packages can do
context switching for threads MUCH
less expensively
• BUT, if one thread makes a blocking
system call, all threads block
• Solutions:
– Lightweight processes
– Scheduler activations
3/31/2016
Distributed Systems - Comp 655
9
Lightweight processes
process
thread
table
• Most thread switching is done in user space
• LWP switching occurs only when a thread makes a blocking
system call
• Scheduling routine, thread table, and thread table mutexes
are shared by LWPs and the user-level thread package
3/31/2016
Distributed Systems - Comp 655
10
User threads multiplexed
Processes - outline
• Process & thread basics
• Multi-threaded clients
• Server basics
• Server design issues
• Code Migration
3/31/2016
Distributed Systems - Comp 655
12
Multi-threaded clients
• Typical web browser
– Main thread gets the page you requested
– Additional threads are dispatched to fetch
images, frames, scripts, etc
• User can start viewing before all the
data has been received
• Parallelism can shorten elapsed
download time when web server is
replicated
3/31/2016
Distributed Systems - Comp 655
13
Processes - outline
• Process & thread basics
• Multi-threaded clients
• Server basics
• Server design issues
• Code Migration
3/31/2016
Distributed Systems - Comp 655
14
Multi-threaded server
3/31/2016
Distributed Systems - Comp 655
15
Multi-threaded server – plan 1
Thread
type
How
many
Responsibility
Dispatcher Main
1
Listen for requests and
queue them
Worker
Main or
pool
manager
7+
Dequeue a request, process
it, send results to client,
repeat
Pool
manager
Main
1
Watch size of request
queue, and start new
Workers if average queue
size is too large
3/31/2016
Started
by
Distributed Systems - Comp 655
16
Threads may need synchronization
• Why synchronize? (preview of week 6):
concurrent access to shared resources
– Concurrent writers can corrupt data
– Concurrent reader and writer can confuse the
reader
• Example: workers de-queue and dispatcher
en-queues
• All operating systems (and some languages)
provide synchronization primitives
• When you design a multi-threaded program
you have to have a synchronization
plan/design/architecture
3/31/2016
Distributed Systems - Comp 655
17
Multi-threaded server – plan 1
synchronization
Object to
synch on
Threads
involved
How it works
Request
queue
Dispatcher,
Workers
• Dispatcher locks object before
queuing a request
• Dispatcher notifies one Worker
after queuing a request
• When a running Worker has no
request to process, it locks the
queue, checks for requests; if found,
dequeues it, unlocks queue,
processes request
• If no requests are waiting, Worker
waits on the queue
3/31/2016
Distributed Systems - Comp 655
18
Multi-threaded server – plan 2
Thread
type
Started
by
Dispatcher Main
Worker
How
many
Responsibility
1
Listen for requests and
start a Worker to handle
each one
Dispatcher 1 per
request
Process a request, send
results to client, end
Compared to plan 1, no synchronization, but more overhead
for starting and stopping threads
3/31/2016
Distributed Systems - Comp 655
19
Processes - outline
• Process & thread basics
• Multi-threaded clients
• Server basics
• Server design issues
• Code Migration
3/31/2016
Distributed Systems - Comp 655
20
Typical server design issues
• How to find it
– While minimizing use of well-known
endpoints (why?)
• How to interrupt it
• Stateful vs stateless
• Object activation policy
3/31/2016
Distributed Systems - Comp 655
21
The super-server
3/31/2016
Distributed Systems - Comp 655
22
The super-server: inetd
3/31/2016
Distributed Systems - Comp 655
23
Interrupting a server that is
processing a long-running request
Major options
•Abrupt termination
•Out-of-band control messages (see below)
•In-band control messages with priority
•Response chunking with option to quit after
each chunk
Out-of-band control messages
Client
3/31/2016
Regular (data) messages
Distributed Systems - Comp 655
Server
24
Stateful vs stateless servers
• Stateless server forgets everything after
each request has been processed. E.g.
HTTP server
• Stateful servers can have better
performance and be less annoying
BUT
• At the cost (usually) of losing
– Relocation transparency
– Failure transparency
3/31/2016
Distributed Systems - Comp 655
25
Object activation policy options
Object creation
• Per request
• Fixed-size pool
• Dynamic pool
Threading
• Single
• Fixed-size thread pool
• Dynamic thread pool
• Thread per object
An object adapter is an implementation
of an activation policy.
3/31/2016
Distributed Systems - Comp 655
26
Object adapters in an object server
and two activation policies
For example,
new object on a
new thread for
each request
For example,
single object on a
single thread handles
all requests
3/31/2016
Distributed Systems - Comp 655
27
Code Migration
• What it is: moving programs across a
network on demand
3/31/2016
Distributed Systems - Comp 655
28
Why migrate code?
• Performance improvement
• Flexibility
• Simplified administration
3/31/2016
Distributed Systems - Comp 655
29