CS 194: Distributed Systems Processes, Threads, Code Migration
CS 194: Distributed Systems Processes, Threads, Code Migration
Distributed Systems
Processes, Threads, Code Migration
Process
What is a Thread?
• Execution context
– Program counter (PC) Stack (T1)
SP (T1)
– Stack pointer (SP) Stack (T2)
– Data registers SP (T2)
Heap
Static Data
PC (T1)
PC (T2) Code
Process
Process vs. Thread (1)
• Process: unit of allocation
– Resources, privileges, etc
• Thread: unit of execution
– PC, SP, registers
• Each process has one or more threads
• Each thread belong to one process
Process vs. Thread (2)
• Processes
– Inter-process communication is expensive: need to
context switch
– Secure: one process cannot corrupt another process
Process vs. Thread (3)
• Threads
– Inter-thread communication cheap: can use process
memory and may not need to context switch
– Not secure: a thread can write the memory used by
another thread
User Level vs. Kernel Level Threads
• User level: use user-level thread package; totally
transparent to OS
– Light-weight
– If a thread blocks, all threads in the process block
• Kernel level: threads are scheduled by OS
– A thread blocking won’t affect other threads in the same
process
– Can take advantage of multi-processors
– Still requires context switch, but cheaper than process
context switching
Thread Creation Example (Java)
final List list; // some sort unsorted list of objects
// A Thread class for sorting a List in the background
class Sorter extends Thread {
List l;
public Sorter(List l) { this.l = l; } // constructor
public void run() { Collections.sort(l); } // Thread body
}
Model Characteristics
Threads Parallelism, blocking system calls
Single-threaded process No parallelism, blocking system calls
Event driven
Parallelism, nonblocking system calls
(Finite state machine)
Servers: General Design Issues
a) Client-to-server binding using a daemon as in DCE
b) Client-to-server binding using a superserver as in UNIX
3.7
Code Migration: Motivation
• Performance
– Move code on a faster machine
– Move code closer to data
• Flexibility
– Allow to dynamically configure a distributed system
Dynamically Configuring a Client
• The client first fetches the necessary software, and then invokes the
server
Code Migration Model
• Process model for code migration (Fugetta et al., 98)
– Code segment: set of instructions that make up the
program
– Resource segment: references to external resources
– Execution segment: store current execution state
• Type of mobility
– Weak mobility: migrate only code segment
– Strong mobility: migrate execution segment and resource
segment
Models for Code Migration
Migration and Local Resources
• Types of process-to-resource binding
– Binding by identifier (e.g., URL, (IPaddr:Port))
– Binding by value (e.g., standard libraries)
– Binding by type (e.g., monitor, printer)
• Type of resources
– Unattached resources: can be easily moved (e.g., data
files)
– Fastened resources: can be used but at a high cost (e.g.,
local databases, web sites)
– Fixed resources: cannot be moved (e.g., local devices)
Migration and Local Resources
Resource-to machine binding
proc factorial n {
if ($n 1) { return 1; } # fac(1) = 1
expr $n * [ factorial [expr $n – 1] ] # fac(n) = n * fac(n – 1)
}
set number … # tells which factorial to compute
set machine … # identify the target machine