Tobiahao

S01_SEMAFORY_04

Dec 17th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. /*
  2. Zademonstruj synchronizację operacji zapisu i odczytu dla łącza nazwa- nego, przy pomocy semaforów.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sys/ipc.h>
  9. #include <sys/types.h>
  10. #include <sys/sem.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <sys/wait.h>
  14.  
  15. #define FIFO_DIR "fifo"
  16. #define MESSAGE "Jakas wiadomosc!"
  17.  
  18. void sem_up(int semid)
  19. {
  20.     struct sembuf up_operation = {0, 1, 0};
  21.  
  22.     if(semop(semid, &up_operation, 1) == -1){
  23.         perror("up_semop");
  24.         exit(EXIT_FAILURE);
  25.     }
  26. }
  27.  
  28. void sem_down(int semid)
  29. {
  30.     struct sembuf down_operation = {0, -1, 0};
  31.  
  32.     if(semop(semid, &down_operation, 1) == -1){
  33.         perror("down_semop");
  34.         exit(EXIT_FAILURE);
  35.     }
  36. }
  37.  
  38. int main(void)
  39. {
  40.     int fifofd, semid;
  41.     pid_t pid;
  42.    
  43.     if(mkfifo(FIFO_DIR, 0775) == -1){
  44.         perror("mkfifo");
  45.         return EXIT_FAILURE;
  46.     }
  47.  
  48.     if((semid = semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT | IPC_EXCL)) == -1){
  49.         perror("semget");
  50.         return EXIT_FAILURE;
  51.     }
  52.  
  53.     if(semctl(semid, 0, SETVAL, 1) == -1){
  54.         perror("setval_semctl");
  55.         return EXIT_FAILURE;
  56.     }
  57.    
  58.     if((fifofd = open(FIFO_DIR, O_RDWR)) == -1){
  59.         perror("open");
  60.         return EXIT_FAILURE;
  61.     }
  62.  
  63.     pid = fork();
  64.     if(pid == -1){
  65.         perror("fork");
  66.         return EXIT_FAILURE;
  67.     }
  68.     else if(pid == 0){
  69.         sem_down(semid);
  70.        
  71.         for(int i = 0; i < 10; i++){
  72.             if(write(fifofd, &i, sizeof(int)) == -1){
  73.                 perror("write");
  74.                 return EXIT_FAILURE;
  75.             }  
  76.         }
  77.        
  78.         if(close(fifofd) == -1){
  79.             perror("child_close");
  80.             return EXIT_FAILURE;
  81.         }
  82.  
  83.         sem_up(semid);
  84.     }
  85.     else{
  86.         for(int i = 0; i < 10; i++){
  87.             int buffer;
  88.             if(read(fifofd, &buffer, sizeof(int)) == -1){
  89.                 perror("read");
  90.                 return EXIT_FAILURE;
  91.             }
  92.             printf("%d ", buffer);
  93.         }
  94.         printf("\n");
  95.    
  96.         if(close(fifofd) == -1){
  97.             perror("parent_close");
  98.             return EXIT_FAILURE;
  99.         }
  100.  
  101.         if(semctl(semid, 0, IPC_RMID) == -1){
  102.             perror("semctl");
  103.             return EXIT_FAILURE;
  104.         }
  105.  
  106.         if(unlink(FIFO_DIR) == -1){
  107.             perror("unlink");
  108.             return EXIT_FAILURE;
  109.         }
  110.     }
  111.  
  112.     return EXIT_SUCCESS;
  113. }
Add Comment
Please, Sign In to add comment