Advertisement
Guest User

Untitled

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