Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- binarysem buffer; // a binary (0 or 1) semaphore for unscrambling output
- binarysem consumidor; // a binary (0 or 1) semaphore for unscrambling output
- int contprod = 0;
- int cont = 50;
- void prod()
- {
- while (cont != 0) {
- cont--;
- p(buffer); // obtain exclusive access to standard output
- cout << "Cont: " << cont << endl;
- contprod = contprod + 1;
- if (contprod == 1 ) {
- v(buffer);
- }
- if (contprod == 2) {
- v(consumidor);
- }
- }
- }
- void cons()
- {
- while (cont != 0) {
- cont--;
- p(consumidor); // obtain exclusive access to standard output
- cout << "Consume Cont: " << cont << endl;
- contprod = 0;
- v(buffer);
- }
- } // decrement
- main()
- {
- initialsem(buffer,1);
- initialsem(consumidor,0);
- cobegin {
- prod(); cons();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement