






Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!







e che c’e una sola sequenza possibile,a immediatamente (B=true). Dunque l’output osservato saraa di A q1: ordina II meta di A r1: wait(S1)u grande. Da un certo punto in poi, P sara bloccato in una posizionea piua piu signal(ri). L’unico motivo pero accaderee che un altro processo, diciamo Q, dopo aver acquisito ri, nono solo bloccarsi su un semaforo rj con j > i. Dunque Qe starved e bloccato1 /∗ Copyright (C) 2006 M. Ben−Ari. See copyright.txt ∗/ 2 /∗ Programmed by Panu Pitk¨am¨aki ∗/ 3 4 import java. util. concurrent. Semaphore; 5 import java. util. Queue; 6 import java. util. LinkedList ; 7 8 /∗ Producer−consumer using semaphore ∗/ 9 class ProducerConsumer { 10 /∗ Size of the finite queue ∗/ 11 static final int N = 3; 12 /∗ Semaphores: at the start N empty slots, 0 used slots. ∗/ 13 static Semaphore notFull = new Semaphore(N); 14 static Semaphore notEmpty = new Semaphore(0); 15 /∗ Predefined type: finite queue of produced items ∗/ 16 17 static Queue
1 /∗ Copyright (C) 2006 M. Ben−Ari. See copyright.txt ∗/ 2 /∗ Programmed by Panu Pitk¨am¨aki ∗/ 3 4 import java. util. concurrent. Semaphore; 5 6 /∗ Dining philosophers solution ∗/ 7 class Dining { 8 /∗ Semaphore for each fork ∗/ 9 static Semaphore[] fork = new Semaphore[5]; 10 11 class Philosopher extends Thread { 12 /∗ Philosopher id ∗/ 13 int i ; 14 Philosopher (int i ) { this. i = i; } 15 16 public void run() { 17 for (int j = 0; j < 10; j ++) { 18 /∗ Think ∗/ 19 try { 20 fork [ i ]. acquire (); 21 System.out.println (”Philosopher ” + i + ” takes fork ” + i +”.”); 22 fork [( i + 1) % 5].acquire (); 23 System.out.println (”Philosopher ” + i + ” takes fork ” + ((i + 1) % 5) +”.”); 24 } catch (InterruptedException e) { 25 } 26 /∗ Eat ∗/ 27 System.out.println (”Philosopher ” + i + ” is eating. ”); 28 fork [ i ]. release (); 29 System.out.println (”Philosopher ” + i + ” releases fork ” + i +”.”); 30 fork [( i + 1) % 5].release (); 31 System.out.println (”Philosopher ” + i + ” releases fork ” + ((i + 1) % 5) +”.”); 32 } 33 } 34 } 35 36 Dining() { 37 for (int i =0; i < 5; i ++) { 38 fork [ i ] = new Semaphore(1); 39 } 40 Philosopher P0 = new Philosopher(0); 41 Philosopher P1 = new Philosopher(1); 42 Philosopher P2 = new Philosopher(2); 43 Philosopher P3 = new Philosopher(3); 44 Philosopher P4 = new Philosopher(4); 45 P0.start (); 46 P1.start (); 47 P2.start (); 48 P3.start (); 49 P4.start (); 50 try { 51 P0.join (); P1.join (); P2.join (); P3.join (); P4.join (); 52 } 53 catch (InterruptedException e) { 54 }; 55 System.out.println (”Program terminated correctly! ”); 56 } 57 58 public static void main(String[] args ) { 59 new Dining(); 60 } 61 }
1 2 DiningAsymmetric() { 3 for (int i =0; i < 5; i ++) { 4 fork [ i ] = new Semaphore(1); 5 } 6 Philosopher P0 = new Philosopher(0); 7 Philosopher P1 = new Philosopher(1); 8 Philosopher P2 = new Philosopher(2); 9 Philosopher P3 = new Philosopher(3); 10 PhilosopherReversed P4 = new PhilosopherReversed(4); 11 P0.start (); 12 P1.start (); 13 P2.start (); 14 P3.start (); 15 P4.start (); 16 try { 17 P0.join (); P1.join (); P2.join (); P3.join (); P4.join (); 18 } 19 catch (InterruptedException e) { 20 }; 21 System.out.println (”Program terminated correctly! ”); 22 } 23 24 public static void main(String[] args ) { 25 new DiningAsymmetric(); 26 } 27 }
1 /∗ Travel Agency ∗/ 2 import java. util. concurrent. Semaphore; 3 4 class Travel { 5 /∗ Semaphores ensure correct order of execution ∗/ 6 static Semaphore[] s = new Semaphore[4]; 7 8 class Client extends Thread { 9 public void run() { 10 for (int j = 0; j < 4; j ++) { 11 try { 12 s[0]. acquire (); 13 System.out.println (”Client : provideData.”); 14 s[1]. release (); 15 s[2]. release (); 16 s[0]. acquire (); 17 s[0]. acquire (); 18 System.out.println (”Client : pay.”); 19 s[3]. release (); 20 } 21 catch (InterruptedException e) {} 22 } 23 } 24 } 25 26 class Flight extends Thread { 27 public void run() { 28 for (int j = 0; j < 4; j ++) { 29 try { 30 s[1]. acquire (); 31 System.out.println (”Flight : bookF.”); 32 s[0]. release (); 33 } 34 catch (InterruptedException e) {} 35 } 36 } 37 } 38 39 class Hotel extends Thread { 40 public void run() { 41 for (int j = 0; j < 4; j ++) { 42 try { 43 s[2]. acquire (); 44 System.out.println (”Hotel: bookH.”); 45 s[0]. release (); 46 } 47 catch (InterruptedException e) {} 48 } 49 } 50 }