majczel23000

[SO] Pamięć współdzielona 05-12-2017

Dec 5th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <stdlib.h>
  4. #include <sys/sem.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>
  7. #include <sys/ipc.h>
  8. #include <sys/shm.h>
  9.  
  10. int shmCreate(key_t key, int sizeInBytes)
  11. {
  12.     int semId = shmget(key,sizeInBytes,IPC_CREAT|IPC_EXCL|0700);
  13.     if(semId == -1)
  14.     {
  15.         perror("Blad tworzenia");
  16.         exit(1);
  17.     }
  18.     return semId;
  19. }
  20. int shmOpen(key_t key)
  21. {
  22.     int semId=shmget(key,0,0700);
  23.     if(semId == -1)
  24.     {
  25.         perror("Blad otwarcia");
  26.         exit(1);
  27.     }
  28.     return semId;
  29. }
  30. void shmRemove(int shmid)
  31. {
  32.     if(shmctl(shmid,IPC_RMID,NULL) == -1)
  33.     {
  34.         perror("Blad usuniecia");
  35.         exit(1);
  36.     }
  37. }
  38. void *shmAt(int shmid)
  39. {
  40.     int* score=shmat(shmid,NULL,0);
  41.     if(*score == -1)
  42.     {
  43.         perror("Blad at");
  44.         exit(1);
  45.     }
  46.     return shmat(shmid,NULL,0);
  47. }
  48. void shmDt(void*addr)
  49. {
  50.     if(shmdt(addr) == -1)
  51.     {
  52.         perror("Blad dt");
  53.         exit(1);
  54.     }
  55. }
  56.  
  57.  
  58.  
  59.  
  60. #include "pamiecwspoldzielona.h"
  61.  
  62. int main()//-zapis
  63. {
  64.     key_t key =  ftok(".",0);
  65.     int a=shmCreate(key,sizeof(long double));
  66.     long double *bufor;
  67.     bufor=(long double*)shmAt(a);
  68.     *bufor=69.696969;
  69.     shmDt(bufor);
  70.     return 0;
  71. }
  72.  
  73.  
  74.  
  75.  
  76. #include "pamiecwspoldzielona.h"
  77.  
  78.  
  79. int main()//-odczyt
  80. {
  81.     key_t key =  ftok(".",0);
  82.     int a=shmOpen(key);
  83.     long double*b=shmAt(a);
  84.     printf("%Lf\n",*b);
  85.     shmDt(b);
  86.     shmRemove(a);
  87.     return 0;
  88. }
Add Comment
Please, Sign In to add comment