Advertisement
maszako

Untitled

Jan 19th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/shm.h>
  3. #include <sys/ipc.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/sem.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10.  
  11.  
  12.  
  13.  
  14.  
  15. /* id segmentu pamieci wspoldzielonej */
  16. int shmID;
  17. /* taka bedzie struktura danych segmentu */
  18. struct Dane {
  19.  
  20. int numer;
  21. char napis[20];
  22.  
  23. } *dane_ptr;
  24.  
  25.  
  26. int getKey()
  27. {
  28. return (ftok(".",'a'));
  29. }
  30.  
  31. void P(int zbior,int element){
  32. struct sembuf bs = {element,-1,0};
  33. if(semop(zbior,&bs,1)==-1){
  34. perror("Blad: funkcja P");
  35. exit(6);
  36. }
  37. }
  38.  
  39. void V(int zbior,int element){
  40. struct sembuf bs = {element,1,0};
  41. if(semop(zbior,&bs,1)==-1){
  42. perror("Blad: funkcja V");
  43. exit(7);
  44.  
  45.  
  46. }
  47. }
  48.  
  49. int creatSem(int number)
  50. {
  51. int semid = semget(getKey(), number, 0644 | IPC_CREAT);
  52. return (semid);
  53. }
  54.  
  55. int creatMemory(){
  56.  
  57. shmID=shmget(getKey(),sizeof(struct Dane),IPC_CREAT | SHM_R | SHM_W | SHM_R >> 3);
  58.  
  59. if(shmID == -1){
  60. perror("Blad utworzenia pamieci wspoldzielonej");
  61. return 1;
  62. }
  63.  
  64.  
  65. }
  66.  
  67. /*dołączenie segmentu pamięci współdzielonej i rzutowanie wskaźnika na "nasz" typ danych przechowywanych w pamięci wpółdzielonej*/
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. int main(){
  76.  
  77. shmID=shmget(getKey(),sizeof(struct Dane),IPC_CREAT | SHM_R | SHM_W | SHM_R >> 3);
  78.  
  79. if(shmID == -1){
  80. perror("Blad utworzenia pamieci wspoldzielonej");
  81. return 1;
  82. }
  83. dane_ptr = (struct Dane *) shmat(shmID, 0, 0);
  84. if(((long)dane_ptr)==-1){
  85. perror("Blad dolaczenia");
  86. return 2;
  87. }
  88.  
  89. int semid=creatSem(2);
  90. P(semid,1);
  91.  
  92. printf("%s\n",&dane_ptr->napis);
  93.  
  94. V(semid,0);
  95.  
  96. return 0;
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement