Advertisement
brsjak

OS - Criminal Transport Problem

Jan 19th, 2020
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. import java.util.HashSet;
  2. import java.util.concurrent.Semaphore;
  3.  
  4.  
  5.  
  6. public class CriminalTransport extends Thread{
  7.    
  8.     static Semaphore pEnter = null;
  9.     static Semaphore cEnter = null;
  10.     static Semaphore lock = null;
  11.     static int police=0;
  12.     static int criminal=0;
  13.     static Semaphore canDrive = null;
  14.     static Semaphore canExit = null;
  15.    
  16.     public static void init() {
  17.         pEnter = new Semaphore(2);
  18.         cEnter = new Semaphore(0);
  19.         lock = new Semaphore(1);
  20.         canExit = new Semaphore(0);
  21.         canDrive = new Semaphore(0);
  22.     }
  23.    
  24.  
  25. static class Policeman extends Thread{
  26.    
  27.     int threadID;
  28.    
  29.     public Policeman(int threadID) {
  30.         this.threadID=threadID;
  31.     }
  32.    
  33.     public void execute() throws InterruptedException {
  34.     // waits until it is valid to enter the car
  35.     pEnter.acquire();
  36.     System.out.println("Policeman enters in the car");
  37.     lock.acquire();
  38.     police++;
  39.     if(police == 2) {
  40.         if(criminal == 0) {
  41.             cEnter.release(2);
  42.         }
  43.     }
  44.     if(police == 3 && criminal == 0) {
  45.         cEnter.release();
  46.     }
  47.    
  48.     if((police+criminal == 4 ) || (police==4 && criminal==0)) {
  49.         canDrive.release(4);
  50.         System.out.println("Start driving.");
  51.     }
  52.     lock.release();
  53.     canDrive.acquire();    
  54.     Thread.sleep(10);
  55.     lock.acquire();
  56.     if(police+criminal == 4) {
  57.         System.out.println("Arrived.");
  58.         canExit.release(4);
  59.     }
  60.     lock.release();
  61.     canExit.acquire();
  62.     System.out.println("Policeman exits from the car");
  63.     lock.acquire();
  64.     police--;
  65.     if(police == 0 && criminal == 0) {
  66.         pEnter.release(2);
  67.         System.out.println("RELEASED 2 POLICEMAN");
  68.     }
  69.     lock.release();
  70.     // when the four passengers are inside, one policeman prints the starting command
  71.    
  72.     // one policeman prints the this command to notice that everyone can exit
  73.    
  74.     // the exit from the car is allowed after the "Arrived." message is printed
  75.    
  76.    
  77.    
  78.   }
  79.  
  80.   public void run(){
  81.       try {
  82.           execute();
  83.          
  84.       }catch(InterruptedException e) {
  85.           e.printStackTrace();
  86.       }
  87.   }
  88.  
  89. }
  90.  
  91. static class Criminal extends Thread{
  92.    
  93.     int threadID;
  94.    
  95.     public Criminal(int threadID) {
  96.         this.threadID=threadID;
  97.     }
  98.  
  99.   public void execute() throws InterruptedException {
  100.     // waits until it is valid to enter the car
  101.     cEnter.acquire();
  102.     System.out.println("Criminal enters in the car");
  103.     lock.acquire();
  104.     criminal++;
  105.     lock.release();
  106.     canDrive.acquire();
  107.     Thread.sleep(10);
  108.     canExit.acquire();
  109.     // the exit from the car is allowed after the "Arrived." message is printed
  110.     System.out.println("Criminal exits from the car");
  111.     lock.acquire();
  112.     criminal--;
  113.     lock.release();
  114.   }
  115.  
  116.   public void run() {
  117.      
  118.       try {
  119.           execute();
  120.          
  121.       }catch(InterruptedException e) {
  122.           e.printStackTrace();
  123.       }
  124.   }
  125. }
  126.  
  127.  
  128.  
  129.   public static void main(String[] args) throws InterruptedException {
  130.     HashSet<Thread> threads = new HashSet<Thread>();
  131.     for (int i = 0; i < 16; i++) {
  132.       Policeman red = new Policeman(i);
  133.       threads.add(red);
  134.       Criminal green = new Criminal(i);
  135.       threads.add(green);
  136.     }
  137.     // run all threads in background
  138.     init();
  139.     for(Thread p : threads) {
  140.         p.start();
  141.     }
  142.     // after all of them are started, wait each of them to finish for maximum 1_000 ms
  143.     for(Thread p : threads) {
  144.         p.join(1000);
  145.     }
  146.     // for each thread, terminate it if it is not finished
  147.    
  148.     for(Thread p : threads) {
  149.         if(p.isAlive()) {
  150.             p.interrupt();
  151.             System.out.println("Possible Deadlock!");
  152.         }
  153.     }
  154.  
  155.   }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement