Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. /**
  2.  
  3. * Original code by Silberschatz, Galvin, and Gagne
  4.  
  5. * from Operating System Concepts with Java, 7th Edition
  6.  
  7. * Modified by William Albritton
  8.  
  9. *
  10.  
  11. * This creates the buffer and the producer and consumer threads.
  12.  
  13. *
  14.  
  15. */
  16.  
  17. import java.util.Date;
  18.  
  19. // import java.util.concurrent.Semaphore;
  20.  
  21.  
  22.  
  23. class Semaphore {
  24.  
  25. private int sem;
  26.  
  27. //
  28.  
  29. public synchronized void swait() {
  30.  
  31. while ( sem <= 0) {
  32.  
  33. try {
  34.  
  35. wait (500);
  36.  
  37. } catch (Exception e) { System.exit(0);};
  38.  
  39. }// II end while
  40.  
  41. sem--; // II decrease value of sem
  42.  
  43. }// end swait
  44.  
  45. //
  46.  
  47. public synchronized void signal() {
  48.  
  49. sem++;
  50.  
  51. notify();
  52.  
  53. } // end signal
  54.  
  55. //
  56.  
  57. // constructor
  58.  
  59. public Semaphore ( int intval) {
  60.  
  61. sem = intval; // initialize attribute sem
  62.  
  63. } // end class Semaphore
  64.  
  65. }
  66.  
  67. public class Solution{
  68.  
  69. public static void main(String args[]) {
  70.  
  71. //instantiate (create) buffer shared by Producer & Consumer
  72.  
  73. Buffer sharedBuffer = new BoundedBuffer();
  74.  
  75. // create the producer and consumer threads
  76.  
  77. Thread producerThread = new Thread(new Producer(sharedBuffer));
  78.  
  79. Thread consumerThread = new Thread(new Consumer(sharedBuffer));
  80.  
  81. //start() method allocates memory for a new thread in the JVM,
  82.  
  83. //and calls the run() method
  84.  
  85. producerThread.start();
  86.  
  87. consumerThread.start();
  88.  
  89. }
  90.  
  91. }
  92.  
  93. //*****************************************************************
  94.  
  95. /**
  96.  
  97. * An interface for buffers
  98.  
  99. *
  100.  
  101. */
  102.  
  103. interface Buffer{
  104.  
  105. /**
  106.  
  107. * insert an item into the Buffer.
  108.  
  109. * Note this may be either a blocking
  110.  
  111. * or non-blocking operation.
  112.  
  113. */
  114.  
  115. public abstract void insert(Object item);
  116.  
  117. /**
  118.  
  119. * remove an item from the Buffer.
  120.  
  121. * Note this may be either a blocking
  122.  
  123. * or non-blocking operation.
  124.  
  125. */
  126.  
  127. public abstract Object remove();
  128.  
  129. }
  130.  
  131. //******************************************************************
  132.  
  133. /**
  134.  
  135. * This program implements the bounded buffer using shared memory.
  136.  
  137. */
  138.  
  139. class BoundedBuffer implements Buffer{
  140.  
  141. private static final int BUFFER_SIZE = 500; //max size of buffer array
  142.  
  143. private int count; //number of items currently in the buffer
  144.  
  145. private int in; // points to the next free position in the buffer
  146.  
  147. private int out; // points to the first filled position in the buffer
  148.  
  149. private Object[] buffer; //array of Objects
  150.  
  151. private Semaphore mutex; //provides limited access to the buffer (mutual exclusion)
  152.  
  153. private Semaphore empty; //keep track of the number of empty elements in the array
  154.  
  155. private Semaphore full; //keep track of the number of filled elements in the array
  156.  
  157. public BoundedBuffer(){
  158.  
  159. // buffer is initially empty
  160.  
  161. count = 0;
  162.  
  163. in = 0;
  164.  
  165. out = 0;
  166.  
  167. buffer = new Object[BUFFER_SIZE];
  168.  
  169. mutex = new Semaphore(1); //1 for mutual exclusion
  170.  
  171. empty = new Semaphore(BUFFER_SIZE); //array begins with all empty elements
  172.  
  173. full = new Semaphore(0); //array begins with no elements
  174.  
  175. }
  176.  
  177. // producer calls this method
  178.  
  179. public void insert(Object item) {
  180.  
  181. empty.swait(); //keep track of number of empty elements (value--)
  182.  
  183. //This provides synchronization for the producer,
  184.  
  185. //because this makes the producer stop running when buffer is full
  186.  
  187. mutex.swait(); //mutual exclusion
  188.  
  189. // add an item to the buffer
  190.  
  191. ++count;
  192.  
  193. buffer[in] = item;
  194.  
  195. //modulus (%) is the remainder of a division
  196.  
  197. //for example, 0%3=0, 1%3=1, 2%3=2, 3%3=0, 4%3=1, 5%3=2
  198.  
  199. in = (in + 1) % BUFFER_SIZE;
  200.  
  201. //buffer information feedback
  202.  
  203. if (count == BUFFER_SIZE){
  204.  
  205. System.out.println("BUFFER FULL "
  206.  
  207. + "Producer inserted \"" + item
  208.  
  209. + "\" count=" + count + ", "
  210.  
  211. + "in=" + in + ", out=" + out);
  212.  
  213. }
  214.  
  215. else{
  216.  
  217. System.out.println("Producer inserted \"" + item
  218.  
  219. + "\" count=" + count + ", "
  220.  
  221. + "in=" + in + ", out=" + out);
  222.  
  223. }
  224.  
  225. mutex.signal(); //mutual exclusion
  226.  
  227. full.signal(); //keep track of number of elements (value++)
  228.  
  229. //If buffer was empty, then this wakes up the Consumer
  230.  
  231. }
  232.  
  233. // consumer calls this method
  234.  
  235. public Object remove() {
  236.  
  237. Object item=null;
  238.  
  239. /*
  240.  
  241. while (count == 0){
  242.  
  243. //if nothing in the buffer, then do nothing
  244.  
  245. //the buffer array cannot be used (because empty)
  246.  
  247. }
  248.  
  249. */
  250.  
  251. full.swait(); //keep track of number of elements (value--)
  252.  
  253. //This provides synchronization for consumer,
  254.  
  255. //because this makes the Consumer stop running when buffer is empty
  256.  
  257. mutex.swait(); //mutual exclusion
  258.  
  259. // remove an item from the buffer
  260.  
  261. --count;
  262.  
  263. item = buffer[out];
  264.  
  265. //modulus (%) is the remainder of a division
  266.  
  267. //for example, 0%3=0, 1%3=1, 2%3=2, 3%3=0, 4%3=1, 5%3=2
  268.  
  269. out = (out + 1) % BUFFER_SIZE;
  270.  
  271. //buffer information feedback
  272.  
  273. if (count == 0){
  274.  
  275. System.out.println("BUFFER EMPTY "
  276.  
  277. + "Consumer removed \"" + item
  278.  
  279. + "\" count=" + count + ", "
  280.  
  281. + "in=" + in + ", out=" + out);
  282.  
  283. }
  284.  
  285. else{
  286.  
  287. System.out.println("Consumer removed \"" + item
  288.  
  289. + "\" count=" + count + ", "
  290.  
  291. + "in=" + in + ", out=" + out);
  292.  
  293. }
  294.  
  295. mutex.signal(); //mutual exclusion
  296.  
  297. empty.signal(); //keep track of number of empty elements (value++)
  298.  
  299. //if buffer was full, then this wakes up the Producer
  300.  
  301. return item;
  302.  
  303. }
  304.  
  305. }
  306.  
  307. //***************************************************************
  308.  
  309. /**
  310.  
  311. * This is the producer thread for the bounded buffer problem.
  312.  
  313. */
  314.  
  315. class Producer extends Thread{
  316.  
  317. private Buffer buffer;
  318.  
  319. public Producer(Buffer b) {
  320.  
  321. buffer = b;
  322.  
  323. }
  324.  
  325. public void run(){
  326.  
  327. Date message;
  328.  
  329. while (true) {
  330.  
  331. System.out.println("Producer napping");
  332.  
  333. SleepUtilities.nap();
  334.  
  335. // produce an item & enter it into the buffer
  336.  
  337. message = new Date();
  338.  
  339. System.out.println("Producer produced \"" + message + "\"");
  340.  
  341. buffer.insert(message);
  342.  
  343. }
  344.  
  345. }
  346.  
  347. }
  348.  
  349. //*******************************************************
  350.  
  351. /**
  352.  
  353. * This is the consumer thread for the bounded buffer problem.
  354.  
  355. */
  356.  
  357. class Consumer extends Thread{
  358.  
  359. private Buffer buffer;
  360.  
  361. public Consumer(Buffer b) {
  362.  
  363. buffer = b;
  364.  
  365. }
  366.  
  367. public void run(){
  368.  
  369. Date message = null;
  370.  
  371. while (true){
  372.  
  373. System.out.println("Consumer napping");
  374.  
  375. SleepUtilities.nap();
  376.  
  377. // consume an item from the buffer
  378.  
  379. System.out.println("Consumer wants to consume");
  380.  
  381. message = (Date)buffer.remove();
  382.  
  383. System.out.println("Consumer consumed \"" + message + "\"");
  384.  
  385. }
  386.  
  387. }
  388.  
  389. }
  390.  
  391. //*********************************************************
  392.  
  393. /**
  394.  
  395. * Utilities for causing a thread to sleep.
  396.  
  397. * Note, we should be handling interrupted exceptions
  398.  
  399. * but choose not to do so for code clarity.
  400.  
  401. *
  402.  
  403. */
  404.  
  405. class SleepUtilities{
  406.  
  407. private static final int NAP_TIME = 5; //max nap time in seconds
  408.  
  409. /**
  410.  
  411. * Nap between zero and NAP_TIME seconds.
  412.  
  413. */
  414.  
  415. public static void nap() {
  416.  
  417. nap(NAP_TIME);
  418.  
  419. }
  420.  
  421. /**
  422.  
  423. * Nap between zero and duration seconds.
  424.  
  425. */
  426.  
  427. public static void nap(int duration) {
  428.  
  429. int sleeptime = (int) (NAP_TIME * Math.random() );
  430.  
  431. System.out.println("Nap for " + sleeptime + " seconds");
  432.  
  433. //Causes the currently executing thread to sleep (cease execution)
  434.  
  435. //for the specified number of milliseconds,
  436.  
  437. //subject to the precision and accuracy of system timers and schedulers.
  438.  
  439. try { Thread.sleep(sleeptime*1000); }
  440.  
  441. catch (InterruptedException e) {
  442.  
  443. //method sleep() throws InterruptedException - if any thread has interrupted the current thread.
  444.  
  445. System.out.println("ERROR in nap(): " + e);
  446.  
  447. }
  448.  
  449. }
  450.  
  451. }
  452.  
  453. /*output:-
  454.  
  455. "Thu Oct 19 04:11:05 IST 2017"
  456.  
  457. Producer inserted "Thu Oct 19 04:11:05 IST 2017" count=11, in=104, out=93
  458.  
  459. Producer napping
  460.  
  461. Nap for 3 seconds
  462.  
  463. Producer produced "Thu Oct 19 04:11:08 IST 2017"
  464.  
  465. Producer inserted "Thu Oct 19 04:11:08 IST 2017" count=12, in=105, out=93
  466.  
  467. Producer napping
  468.  
  469. Nap for 4 seconds
  470.  
  471. Consumer wants to consume
  472.  
  473. Consumer removed "Thu Oct 19 04:10:45 IST 2017" count=11, in=105, out=94
  474.  
  475. Consumer consumed "Thu Oct 19 04:10:45 IST 2017"
  476.  
  477. Consumer napping
  478.  
  479. Nap for 0 seconds
  480.  
  481. Consumer wants to consume
  482.  
  483. Consumer removed "Thu Oct 19 04:10:46 IST 2017" count=10, in=105, out=95
  484.  
  485. Consumer consumed "Thu Oct 19 04:10:46 IST 2017"
  486.  
  487. Consumer napping
  488.  
  489. Nap for 1 seconds
  490.  
  491. Consumer wants to consume
  492.  
  493. Consumer removed "Thu Oct 19 04:10:46 IST 2017" count=9, in=105, out=96
  494.  
  495. Consumer consumed "Thu Oct 19 04:10:46 IST 2017"
  496.  
  497. Consumer napping
  498.  
  499. Nap for 3 seconds
  500.  
  501. Producer produced "Thu Oct 19 04:11:12 IST 2017"
  502.  
  503. Producer inserted "Thu Oct 19 04:11:12 IST 2017" count=10, in=106, out=96
  504.  
  505. Producer napping
  506.  
  507. Nap for 4 seconds
  508.  
  509. Consumer wants to consume
  510.  
  511. Consumer removed "Thu Oct 19 04:10:46 IST 2017" count=9, in=106, out=97
  512.  
  513. Consumer consumed "Thu Oct 19 04:10:46 IST 2017"
  514.  
  515. Consumer napping
  516.  
  517. Nap for 2 seconds
  518.  
  519. Consumer wants to consume
  520.  
  521. Consumer removed "Thu Oct 19 04:10:47 IST 2017" count=8, in=106, out=98
  522.  
  523. Consumer consumed "Thu Oct 19 04:10:47 IST 2017"
  524.  
  525. Consumer napping
  526.  
  527. Nap for 2 seconds
  528.  
  529. Producer produced "Thu Oct 19 04:11:16 IST 2017" .... continue.
  530.  
  531. until you terminate the program
  532.  
  533. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement