jfcmacro

demonioImp.cpp

Mar 15th, 2019
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <sys/types.h>
  3. #include <sys/ipc.h>
  4. #include <sys/sem.h>
  5. #include <cstdlib>
  6. #include <cerrno>
  7. #include <cstring>
  8.  
  9. using namespace std;
  10.  
  11. static void usage(const char *progname);
  12.  
  13. // crearSemaforo <keysem>
  14. int
  15. main(int argc, char *argv[]) {
  16.  
  17.   if (argc != 2) usage(argv[0]);
  18.  
  19.   int key = atoi(argv[1]);
  20.  
  21.   int semid = semget(key, 1, 0660);
  22.  
  23.   if (semid == -1) {
  24.     cerr << "Error abriendo el semaforo: "
  25.      << strerror(errno) << endl;
  26.     return EXIT_FAILURE;
  27.   }
  28.  
  29.   for (;;) {
  30.     struct sembuf ops[1]; //  = { { 0, -1, 0} };
  31.  
  32.     // wait(sem)
  33.     ops[0].sem_num = 0;
  34.     ops[0].sem_op  = -1;
  35.     ops[0].sem_flg = 0;
  36.     cout << "Esperando por mensajes de impresion" << endl;
  37.     semop(semid, ops, 1);
  38.     cout << "Operacion recibida" << endl;
  39.   }
  40.  
  41.   return EXIT_SUCCESS;
  42. }
  43.  
  44. static void
  45. usage(const char *progname) {
  46.   cerr << "Uso: " << progname
  47.        << " <semkey> " << endl;
  48.   exit(EXIT_FAILURE);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment