jfcmacro

Agente.cpp

Mar 22nd, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <fcntl.h>           /* For O_* constants */
  2. #include <sys/stat.h>        /* For mode constants */
  3. #include <semaphore.h>
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <cerrno>
  8. #include "nombres.h"
  9.  
  10. using namespace std;
  11.  
  12. static void agente(sem_t *[]);
  13.  
  14. int
  15. main(void) {
  16.  
  17.   sem_t *semaforos[nSEM];
  18.  
  19.   for (int i = 0; i < nSEM; ++i) {
  20.     semaforos[i] = sem_open(nombres[i], 0);
  21.   }
  22.  
  23.   agente(semaforos);
  24.  
  25.   return EXIT_SUCCESS;
  26. }
  27.  
  28.  
  29. static void agente(sem_t *semaforos[]) {
  30.  
  31.   for (;;) {
  32.     cout << "Ofertando" << endl;
  33.     int fumador = rand() % 3;
  34.     sem_post(semaforos[fumador]);
  35.     sem_wait(semaforos[nSEM - 1]);
  36.   }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment