290 likes | 418 Vues
This outline covers the fundamentals of processes and threads, emphasizing the importance of multi-threading in server design, client operations, and efficient code migration in distributed systems. It explains the core concepts, such as how threads execute as objects in Java, the significance of synchronization to prevent data corruption, and the challenges of blocking calls. The document also discusses server design issues, including state management, interruption strategies, and the balance between efficiency and synchronization. Essential for developers, this material equips them with the foundational knowledge to handle concurrency in modern applications.
E N D
Processes - outline • Process & thread basics • Multi-threaded clients • Server basics • Server design issues • Code Migration Distributed Systems - Comp 655
If you remember one thing… • Threads execute • Processes don’t • A thread owns • Id • Program counter • Scheduling priority • Stack • Local storage • A process owns • Id • Thread table • Page tables • Authenticated identity • IO resources Distributed Systems - Comp 655
Threads in Java Threads are objects • … like almost everything in Java • To make one, • Define a class that • Extends java.lang.Thread, or • Implements java.lang.Runnable • At runtime, create and initialize an instance • Call the start method: • mythread.start(); or • new Thread(myrunnable).start(); Distributed Systems - Comp 655
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) {} } } } Distributed Systems - Comp 655
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(); } } Distributed Systems - Comp 655
Possible output java ThreadExample hey whats up hey [1] whats [1] up [1] hey [2] whats [2] up [2] … Distributed Systems - Comp 655
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 Distributed Systems - Comp 655
The most expensive operations Context switching Distributed Systems - Comp 655
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 Distributed Systems - Comp 655
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 Distributed Systems - Comp 655
Processes - outline • Process & thread basics • Multi-threaded clients • Server basics • Server design issues • Code Migration Distributed Systems - Comp 655
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 Distributed Systems - Comp 655
Processes - outline • Process & thread basics • Multi-threaded clients • Server basics • Server design issues • Code Migration Distributed Systems - Comp 655
Multi-threaded server Distributed Systems - Comp 655
Multi-threaded server – plan 1 Distributed Systems - Comp 655
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 Distributed Systems - Comp 655
Multi-threaded server – plan 1 synchronization Distributed Systems - Comp 655
Multi-threaded server – plan 2 Compared to plan 1, no synchronization, but more overhead for starting and stopping threads Distributed Systems - Comp 655
Processes - outline • Process & thread basics • Multi-threaded clients • Server basics • Server design issues • Code Migration Distributed Systems - Comp 655
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 Distributed Systems - Comp 655
The super-server Distributed Systems - Comp 655
The super-server: inetd Distributed Systems - Comp 655
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 Client Out-of-band control messages Server Regular (data) messages Distributed Systems - Comp 655
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 Distributed Systems - Comp 655
Object creation Per request Fixed-size pool Dynamic pool Threading Single Fixed-size thread pool Dynamic thread pool Thread per object Object activation policy options An object adapter is an implementation of an activation policy. Distributed Systems - Comp 655
For example, new object on a new thread for each request For example, single object on a single thread handles all requests Object adapters in an object server and two activation policies Distributed Systems - Comp 655
Code Migration • What it is: moving programs across a network on demand Distributed Systems - Comp 655
Why migrate code? • Performance improvement • Flexibility • Simplified administration Distributed Systems - Comp 655