Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. const int N=10;
  2. int countvalues = 1;
  3.  
  4. monitor monitor1
  5. {
  6. int buffer[N];
  7. int write_pos;
  8. int read_pos;
  9. int count;
  10. condition full,empty;
  11.  
  12. void queue_add(int item)
  13. {
  14.  
  15. if(count == N)
  16. {
  17. waitc(full);
  18. }
  19.  
  20.  
  21. buffer[write_pos] = item;
  22. write_pos = (write_pos + 1) % N;
  23. count++;
  24.  
  25. signalc(empty);
  26.  
  27. }
  28.  
  29.  
  30. int queue_get()
  31. {
  32. int item;
  33.  
  34. if(count == 0)
  35. {
  36. waitc(empty);
  37. }
  38.  
  39. item = buffer[read_pos];
  40. read_pos = (read_pos + 1) % N;
  41. count--;
  42. countvalues++;
  43. signalc(full);
  44. return item;
  45. }
  46.  
  47. init
  48. {
  49. read_pos = 0;
  50. write_pos = 0;
  51. count = 0;
  52. }
  53.  
  54. }
  55.  
  56. void producer(int size)
  57. {
  58. int i;
  59.  
  60. for(i=0; i<size; i++)
  61. {
  62. queue_add(i);
  63.  
  64. }
  65. }
  66.  
  67.  
  68.  
  69. void consumer(int size)
  70. {
  71. int i;
  72.  
  73. for(i=0; i<size; i++)
  74. {
  75. cout<<"Value number "<<countvalues<<": "<<queue_get()<<endl;
  76. }
  77. }
  78.  
  79.  
  80. main()
  81. {
  82. cout<<"N="<<N<<endl;
  83. cout<<endl;
  84. cobegin
  85. {
  86. producer(N);
  87. producer(N);
  88. producer(N);
  89. consumer(3*N);
  90. }
  91.  
  92. cout<<"All values consumed."<<endl;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement