Advertisement
3axap_010

Receiver.c

Mar 19th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <semaphore.h>
  7. #include <fcntl.h>
  8. #include <sys/stat.h>
  9.  
  10.  
  11. int running = 1;
  12.  
  13. void sig_handler(int signum)
  14. {
  15.     running = 0;
  16. }
  17.  
  18. int main(int argc, char** argv)
  19. {
  20.     struct sigaction act;
  21.     memset(&act, 0, sizeof(struct sigaction));
  22.     act.sa_handler = sig_handler;
  23.  
  24.     if (sigaction(SIGTERM, &act, NULL) == -1)
  25.     {
  26.         fprintf(stdout, "Sigaction failed\n");
  27.         exit(EXIT_FAILURE);
  28.     }
  29.  
  30.     sem_t* sem = sem_open("semaphore", 0);
  31.     if (sem == SEM_FAILED)
  32.     {
  33.         fprintf(stderr, "Can't open a semaphore\n");
  34.         exit(EXIT_FAILURE);
  35.     }
  36.  
  37.     while (running)
  38.     {
  39.         sem_wait(sem); 
  40.         fprintf(stdout, "%s\n", argv[1]);
  41.         sem_post(sem);
  42.     }
  43.  
  44.     sem_close(sem);
  45.  
  46.     exit(EXIT_SUCCESS);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement