Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1.  
  2. binarysem buffer; // a binary (0 or 1) semaphore for unscrambling output
  3. binarysem consumidor; // a binary (0 or 1) semaphore for unscrambling output
  4. int contprod = 0;
  5. int cont = 50;
  6. void prod()
  7. {
  8. while (cont != 0) {
  9. cont--;
  10.  
  11. p(buffer); // obtain exclusive access to standard output
  12. cout << "Cont: " << cont << endl;
  13. contprod = contprod + 1;
  14. if (contprod == 1 ) {
  15.  
  16. v(buffer);
  17. }
  18. if (contprod == 2) {
  19.  
  20. v(consumidor);
  21. }
  22.  
  23.  
  24. }
  25. }
  26.  
  27. void cons()
  28. {
  29. while (cont != 0) {
  30. cont--;
  31.  
  32. p(consumidor); // obtain exclusive access to standard output
  33. cout << "Consume Cont: " << cont << endl;
  34.  
  35. contprod = 0;
  36. v(buffer);
  37.  
  38.  
  39. }
  40. } // decrement
  41.  
  42. main()
  43. {
  44.  
  45. initialsem(buffer,1);
  46. initialsem(consumidor,0);
  47. cobegin {
  48. prod(); cons();
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement