Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <cstdlib>
- #include <cerrno>
- #include <cstring>
- using namespace std;
- static void usage(const char *progname);
- // crearSemaforo <keysem>
- int
- main(int argc, char *argv[]) {
- if (argc != 2) usage(argv[0]);
- int key = atoi(argv[1]);
- int semid = semget(key, 1, 0660);
- if (semid == -1) {
- cerr << "Error abriendo el semaforo: "
- << strerror(errno) << endl;
- return EXIT_FAILURE;
- }
- for (;;) {
- struct sembuf ops[1]; // = { { 0, -1, 0} };
- // wait(sem)
- ops[0].sem_num = 0;
- ops[0].sem_op = -1;
- ops[0].sem_flg = 0;
- cout << "Esperando por mensajes de impresion" << endl;
- semop(semid, ops, 1);
- cout << "Operacion recibida" << endl;
- }
- return EXIT_SUCCESS;
- }
- static void
- usage(const char *progname) {
- cerr << "Uso: " << progname
- << " <semkey> " << endl;
- exit(EXIT_FAILURE);
- }
Advertisement
Add Comment
Please, Sign In to add comment