Critical section

Mutual exclusion

The shared update problem

Specifying synchronization

Implementing await

Liseberg problem revisited

Achieving mutex

The mutual exclusion problem

Attempt 1

Attempt 2

Attempt 3

1 + 3 = Peterson's algorithm

How do we know it works?

Complex instructions

Right for the job?

Peterson's algorithm in Java

Beyond busy waiting

Semaphores (an overview)

Semaphores in Java

Semaphore mutex = new Semaphore(1);

public void run() {
   try {
      while (true) {
         //Non-critical section
         mutex.acquire();
         //Critical Section
         mutex.release();
         //Non-critical section 
   }} catch(InterruptedException e) {
}}

Binary semaphores and locks

Java built-in locks

Liseberg example using semaphores

code

for(int j = 0; j<5000000; j++) {
   //System.out.println("Process "+x+" enters "+j);
   try {mutex.acquire() ;} catch (InterruptedException e) {} 
   enter();
   mutex.release() ; 
} ;

Summary