Tobiahao

S01_PAMIEC_DZIELONA_04a

Dec 19th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. /*
  2.    Napisz dwa programy, które b ę d ą komunikować się poprzez pamięć dzielon ą .
  3.    W trakcie dzia ł ania programów (np. po dwóch wys ł anych i odebranych komunikatach) niech program b ę d ą cy w ł a ś cicielem pami ę ci zamieni jej prawa dost ę pu, tak, aby tylko on móg ł z niej korzysta ć . Poka ż co si ę wtedy stanie.
  4.    */
  5.  
  6. // Program pierwszy
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/ipc.h>
  11. #include <sys/types.h>
  12. #include <sys/sem.h>
  13. #include <sys/shm.h>
  14. #include <unistd.h>
  15.  
  16. #define FTOK_PATH "/tmp"
  17. #define FTOK_ID 0x712
  18.  
  19. union semun {
  20.     int              val;
  21.     struct semid_ds *buf;
  22.     unsigned short  *array;
  23.     struct seminfo  *__buf;
  24. };
  25.  
  26.  
  27. void error_handler(const char *msg)
  28. {
  29.     perror(msg);
  30.     exit(EXIT_FAILURE);
  31. }
  32.  
  33. void mux_up(int mux)
  34. {
  35.     struct sembuf up_operation = {0, 1, 0};
  36.     if(semop(mux, &up_operation, 1) == -1)
  37.         error_handler("semop_up");
  38. }
  39.  
  40. void mux_down(int mux)
  41. {
  42.     struct sembuf down_operation = {0, -1, 0};
  43.     if(semop(mux, &down_operation, 1) == -1)
  44.         error_handler("semop_down");
  45. }
  46.  
  47.  
  48.  
  49. int main(void)
  50. {
  51.     int shmid, mux;
  52.     int *shared_memory;
  53.     key_t key;
  54.     struct shmid_ds shm;
  55.     union semun semopts;
  56.  
  57.     if((key = ftok(FTOK_PATH, FTOK_ID)) == -1)
  58.         error_handler("ftok");
  59.  
  60.     if((mux = semget(key, 1, 0660 | IPC_CREAT)) == -1)
  61.         error_handler("semget");
  62.     semopts.val = 0;
  63.     if(semctl(mux, 0, SETVAL, semopts) == -1)
  64.         error_handler("semctl");
  65.  
  66.     if((shmid = shmget(key, SHMLBA, 0660 | IPC_CREAT)) == -1)
  67.         error_handler("semctl");
  68.     if((shared_memory = (int *)shmat(shmid, NULL, 0)) == (void *)-1)
  69.         error_handler("shmat");
  70.  
  71.     for(int i = 0; i < 5; i++){
  72.         mux_up(mux);
  73.         printf("Zapisano do pamieci dzielonej liczbe: %d\n", i+1);
  74.         shared_memory[i] = i+1;
  75.         sleep(1);
  76.         if(i == 1){
  77.             char *mode = "000";
  78.             sscanf(mode, "%ho", &shm.shm_perm.mode);
  79.             shmctl(shmid, IPC_SET, &shm);
  80.             printf("Nowe prawa dostepu : %o\n", shm.shm_perm.mode);
  81.         }  
  82.     }
  83.  
  84.     if(shmdt(shared_memory) == -1)
  85.         error_handler("shmdt");
  86.     if(shmctl(shmid, IPC_RMID, NULL) == -1)
  87.         error_handler("shmctl");
  88.  
  89.     if(semctl(mux, 0, IPC_RMID, NULL) == -1)
  90.         error_handler("semctl");
  91.  
  92.  
  93.     return EXIT_SUCCESS;
  94. }
Add Comment
Please, Sign In to add comment