wait with priority An enhanced version of the wait operation accepts an optional priority argument: syntax: <variable>.wait <parameter> the smaller the value of the parameter, the highest the priority When the variable is signaled, the process with highest priority in the queue is activated the base wait implementation used a First-In-First-Out (FIFO) discipline
Example: Smallest job first procedure startprint; begin if NOT printerisbusy then jobavailable.wait; printer-file := buffer; end startprint; <print printer-file> procedure endprint; begin printerisbusy := false; OKtoprint.signal; end endprint; procedure enqueuejob(file); begin if printerisbusy then OKtoprint.wait sizeof(file); printerisbusy := true; buffer := file; jobavailable.signal end;
Monitors: pros and cons Pros: encapsulation provides automatic serialization flexibility in blocking and unblocking process execution within monitor procedures Cons lack of concurrency if monitor encapsulates shared resources possibility of deadlock with nested monitor calls
Multi-threaded programming in Java Java allows program to specify multiple threads of execution Provides instructions to ensure mutual exclusion, and selective blocking/unblocking of threads
Lessons learned Encapsulation of critical section of code is desirable provides automatic mutual exclusion single copy of code, single point of synchronization however would be nice to have some form of controlled concurrency Blocking/unblocking of processes is powerful tool basic ingredient are named queues, enqueue and dequeue operations enqueue and dequeue operations usually subject to condition
What is a thread in Java? A thread is a program-counter and a stack All threads share the same memory space A running thread can Yield Sleep Wait for I/O or notification Be pre-empted A key feature: Synchronized methods Allow an exclusive lock, e.g., in an update method
Basic Syntax Build a thread by extending the class java.lang.thread Must have a public void run() method it is executed at the start of the thread, and when it finishes, the thread finishes Synchronized statement Synchronized (obj) { block } Obtains a lock on obj before executing block, releases lock after executing block Wait() gives up lock and suspends the thread Notifyall() resumes all threads waiting on object, resumed tasks must reacquire lock before continuing
Producer Consumer Example Public class ProdCons { private boolean ready ; private Object obj ; public ProducerConsumer() { ready = false ; } public ProducerConsumer (Object o) { obj = o ; ready = true ; } Synchronized Object consume() { while (!ready) wait() ; ready = false ; notifyall() ; return obj ; } Synchronized void produce (object o) { while (ready) wait() ; obj = o ; ready = true ; notifyall() ; } }
Introduction to Distributed Systems First two topics Conceptual Issue: Local and Global Clocks Practical Issue: Process Communication Distributed Mutual Exclusion
Absence of Global Clock Problem: synchronizing the activities of different part of the system (e.g. process scheduling) What about using a single shared clock? two different processes can see the clock at different times due to unpredictable transmission delays What about using radio synchronized clocks? Propagation delays are unpredictable Software approaches Clock synchronization algorithms Logical clocks
Cristian's Algorithm Basic idea: get the current time from a time server. Issues: Error due to communication delay - can be estimated as (T 1 -T 2 -I)/2 Time correction on client must be gradual
The Berkeley Algorithm a) The time daemon asks all the other machines for their clock values b) The machines answer c) The time daemon tells everyone how to adjust their clock
Logical clocks The need to order events in a distributed system has motivated schemes for logical clocks These artificial clocks provide some but not all of the functionality of a real global clock They build a clock abstraction based on underlying physical events of the system
Happened before relation: definitions Happened before relation ( ): a b if a and b are in the same process and a occurred before b a b if a is the event of sending a message and b is the event of receiving the same message by another process if a b and b c then a c, i.e. the relation is transitive The happened before relation is a way of ordering events based on the behavior of the underlying computation
Happened before relation: definitions (2) Two distinct events a and b are said to be concurrent (a b) if and For any two events in the system, either a b, b a or a b Example: a / b b / a P 1 e 11 e 12 e 13 e 14 e 11 e 21 e 22 e 13, e 13 e 14 thus e 22 e 14 P 2 e 21 e 22 e 23 e 24 Global Time
Lamport s Logical Clocks: definitions A logical clock C i at each process P i is a function that assigns a number C i (a) to any event a, called timestamp timestamps are monotonically increasing values example: C i (a) could be implemented as a counter We want to build a logical clock C(a) such that: if a b then C(a) < C(b)
Lamport s Logical Clocks: implementation If we want a logical clock C(a) to satisfy: if a b then C(a) < C(b) the following conditions must be met: if a and b are in the same process and a occurred before b, then C i (a) < C i (b) if a is the event of sending a message in process P i and b is the event of receiving the same message by process P j then C i (a) < C j (b)
Lamport s Logical Clocks: implementation (2) Two implementations rules that satisfy the previous correctness conditions are: clock C i is incremented by d at each event in process P i: C i := C i + d (d > 0) if event a is the sending of a message m by process P i, then message m is assigned the timestamp t m = C i (a) (C i (a) is obtained after applying previous rule). Upon receiving message m, process P j sets its clock to: C j := max(c j, t m + d) (d > 0)
Lamport s Logical Clocks: example Fill the blanks e 11 e 12 e 13 e 14 e 15 e 16 e 17 P 1 ( ) ( ) ( ) ( ) ( ) ( ) ( ) P 2 ( ) ( ) ( ) ( ) ( ) e 21 e 22 e 23 e 24 e 25 Global Time
Lamport s Logical Clocks: example e 11 e 12 e 13 e 14 e 15 e 16 e 17 P 1 (1) (2) (3) (4) (5) (6) (7) P 2 (1) (2) (3) (4) (7) e 21 e 22 e 23 e 24 e 25 Global Time