Tobiahao

S01_PAMIEC_DZIELONA_06

Dec 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.21 KB | None | 0 0
  1. /*
  2. Stwórz trzy procesy, które b ę d ą wymienia ł y mi ę dzy sobą dane poprzez dwa obszary
  3. pami ę ci dzielonej (np. niech proces drugi po ś redniczy w wymianie danych pomi ę dzy
  4. pierwszym i drugim obszarem pami ę ci dzielonej).
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <sys/ipc.h>
  10. #include <sys/types.h>
  11. #include <sys/shm.h>
  12. #include <sys/sem.h>
  13. #include <unistd.h>
  14. #include <wait.h>
  15. #include <time.h>
  16.  
  17. void error_exit(const char *msg)
  18. {
  19.     perror(msg);
  20.     exit(EXIT_FAILURE);
  21. }
  22.  
  23. void lock_mutex(int mux)
  24. {
  25.     struct sembuf lock_mutex = {0, -1, 0};
  26.     if(semop(mux, &lock_mutex, 1) == -1)
  27.         error_exit("lock_mutex");
  28. }
  29.  
  30. void unlock_mutex(int mux)
  31. {
  32.     struct sembuf unlock_mutex = {0, 1, 0};
  33.     if(semop(mux, &unlock_mutex, 1) == -1)
  34.         error_exit("unlock_mutex");
  35. }
  36.  
  37. void first_process(int shmid, int mux)
  38. {
  39.     int *shared_memory;
  40.     int random_number = (rand() % 10) + 1;
  41.  
  42.     if((shared_memory = (int *)shmat(shmid, NULL, 0)) == (void *)-1)
  43.         error_exit("first_shmat");
  44.    
  45.     lock_mutex(mux);
  46.     (*shared_memory) = random_number;
  47.     unlock_mutex(mux);
  48.     printf("Proces 1: Zapisuje do pamieci dzielonej liczbe: %d\n", *shared_memory);
  49.  
  50.     if(shmdt((void *)shared_memory) == -1)
  51.         error_exit("first_shmdt");
  52. }
  53.  
  54. void second_process(int shmids[], int mux)
  55. {
  56.     int *first_shared_memory;
  57.     int *second_shared_memory;
  58.  
  59.     if((first_shared_memory = shmat(shmids[0], NULL, 0)) == (void *)-1)
  60.         error_exit("second_shmat");
  61.     if((second_shared_memory = shmat(shmids[1], NULL, 0)) == (void *)-1)
  62.         error_exit("second_shmat");
  63.     lock_mutex(mux);
  64.     (*second_shared_memory) = (*first_shared_memory);
  65.     unlock_mutex(mux);
  66.  
  67.     printf("Proces 2: Przekazuje liczbe %d z pierwszej pamieci dzielonej do drugiej\n",
  68.             *second_shared_memory);
  69.     if(shmdt(first_shared_memory) || shmdt(second_shared_memory) == -1)
  70.         error_exit("second_shmdt");
  71. }
  72.  
  73. void third_process(int shmid, int mux)
  74. {
  75.     int *shared_memory;
  76.    
  77.     if((shared_memory = (int *)shmat(shmid, NULL, 0)) == (void *)-1)
  78.         error_exit("third_shmat");
  79.     printf("Proces 3: Otrzymana wartosc: %d\n", *shared_memory);
  80.  
  81.     if(shmdt(shared_memory) == -1)
  82.         error_exit("third_shmdt");
  83. }
  84.  
  85. int main(void)
  86. {
  87.     pid_t pids;
  88.     int shmids[2];
  89.     int mux;
  90.  
  91.     srand(time((time_t *)NULL));
  92.     for(int i = 0; i < 2; i++)
  93.         if((shmids[i] = shmget(IPC_PRIVATE, SHMLBA, 0775 | IPC_CREAT | IPC_EXCL)) == -1)
  94.             error_exit("shmget");
  95.  
  96.     if((mux = semget(IPC_PRIVATE, 2, 0775 | IPC_CREAT | IPC_EXCL)) == -1)
  97.         error_exit("semget");
  98.  
  99.     for(int i = 0; i < 2; i++)
  100.         if(semctl(mux, i, SETVAL, 1) == -1)
  101.             error_exit("semctl");
  102.  
  103.     for(int i = 0; i < 3; i++){
  104.         pids = fork();
  105.         if(pids == -1)
  106.             error_exit("fork");
  107.         else if(pids == 0){
  108.             switch(i){
  109.                 case 0:
  110.                     first_process(shmids[0], mux);
  111.                     break;
  112.                 case 1:
  113.                     second_process(shmids, mux);
  114.                     break;
  115.                 case 2:
  116.                     third_process(shmids[1], mux);
  117.                     break;
  118.                 default:
  119.                     error_exit("switch");
  120.             }
  121.             return EXIT_SUCCESS;
  122.         }
  123.     }
  124.  
  125.     for(int i = 0; i < 3; i++)
  126.         if(wait(NULL) == -1)
  127.             error_exit("wait");
  128.  
  129.     if(semctl(mux, 0, IPC_RMID, NULL) == -1)
  130.         error_exit("semctl");
  131.  
  132.     for(int i = 0; i < 2; i++)
  133.         if(shmctl(shmids[i], IPC_RMID, NULL) == -1)
  134.             error_exit("shmctl_rmid");
  135.  
  136.     return EXIT_SUCCESS;
  137. }
Add Comment
Please, Sign In to add comment