Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. Code to copy:
  2.  
  3.  
  4. /************** File name: Semaphore.java ***************/
  5. import java.util.Date;
  6.  
  7. class Semaphore
  8. {
  9. private int sem;
  10.  
  11. public synchronized void swait()
  12. {
  13. while (sem <= 0)
  14. {
  15. try
  16. {
  17. wait(500);
  18. } catch (Exception e)
  19. {
  20. System.exit(0);
  21. }
  22. ;
  23. } // II end while
  24. sem--; // II decrease value of sem
  25. }// end swait
  26. //
  27.  
  28. public synchronized void signal()
  29. {
  30. sem++;
  31. notify();
  32. } // end signal
  33. //
  34. // constructor
  35.  
  36. public Semaphore(int intval)
  37. {
  38. sem = intval; // initialize attribute sem
  39. } // end class Semaphore
  40. }
  41.  
  42. /************** File name: Producer.java ***************/
  43. class Producer extends Thread
  44. {
  45. SharedBuffer buffer;
  46. Semaphore full;
  47. Semaphore empty;
  48. Semaphore mutex;
  49.  
  50. Producer(SharedBuffer b, Semaphore f, Semaphore e, Semaphore m)
  51. {
  52. buffer = b;
  53. full = f;
  54. empty = e;
  55. mutex = m;
  56. }
  57.  
  58. public void run()
  59. {
  60. try
  61. {
  62. char value = 'a';
  63. while (true)
  64. {
  65. empty.swait();
  66. mutex.swait();
  67. buffer.insert(value);
  68. System.out.println("Producer Value:" + value);
  69. mutex.signal();
  70. full.signal();
  71. Thread.sleep(100);
  72. value += 2;
  73. if(value > 100 )
  74. {
  75. value = 0;
  76. }
  77. }
  78. } catch (InterruptedException e)
  79. {
  80. System.out.println("Producer: But I wasn't done!!!");
  81. }
  82. }
  83. }
  84.  
  85.  
  86. /************** File name: Consumer.java ***************/
  87.  
  88. class Consumer extends Thread {
  89. SharedBuffer buffer;
  90. Semaphore full;
  91. Semaphore empty;
  92. Semaphore mutex;
  93.  
  94. Consumer(SharedBuffer b, Semaphore f, Semaphore e, Semaphore m) {
  95. buffer = b;
  96. full = f;
  97. empty = e;
  98. mutex = m;
  99. }
  100.  
  101. public void run() {
  102. try {
  103. char value;
  104. while(true) {
  105. full.swait();
  106. mutex.swait();
  107. value = buffer.retrieve();
  108. mutex.signal();
  109. empty.signal();
  110. System.out.println("Consumer Value: " + value);
  111. Thread.sleep(200);
  112. }
  113. } catch (InterruptedException e) {
  114. System.out.println("Consumer: But I wasn't done!!!");
  115. }
  116. }
  117. }
  118.  
  119.  
  120. /************** File name: SharedBuffer.java ***************/
  121. // Create a class named SharedBuffer
  122. class SharedBuffer
  123. {
  124. // Create an array of type char
  125. private char[] buffer;
  126.  
  127. private int in = 0;
  128. private int out = 0;
  129. private int count = 0;
  130. private int size;
  131.  
  132. SharedBuffer(int size)
  133. {
  134. this.size = size;
  135. buffer = new char[size];
  136. }
  137.  
  138. public void insert(char o)
  139. {
  140. buffer[in] = o;
  141. ++count;
  142. in = (in + 1) % size;
  143. }
  144.  
  145. public char retrieve()
  146. {
  147. int ret_val = buffer[out];
  148. --count;
  149. out = (out + 1) % size;
  150. return ((char) ret_val);
  151. }
  152. }
  153.  
  154.  
  155. /************** File name: ProdconSync.java ***************/
  156. // Create a class named ProdconSync
  157. public class ProdconSync
  158. {
  159. // number of slots in buffer
  160. static final int N = 100;
  161. static SharedBuffer sharedBuff = new SharedBuffer(N);
  162.  
  163. // Create a main method to run the program.
  164. public static void main(String args[])
  165. {
  166. Semaphore mutex = new Semaphore(1);
  167. Semaphore full = new Semaphore(0);
  168. Semaphore empty = new Semaphore(N);
  169.  
  170. Producer prod = new Producer(sharedBuff, mutex, full, empty);
  171. Consumer cons = new Consumer(sharedBuff, mutex, full, empty);
  172. prod.start();
  173. cons.start();
  174.  
  175. } // end main
  176. } // end class ProdconSync
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement