Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. klient--------------------------------
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/ipc.h>
  8. #include <sys/shm.h>
  9. #include <sys/sem.h>
  10. #include <signal.h>
  11. #include <time.h>
  12. // operacja blokowania 1 semafora
  13. static struct sembuf op_lock1[1] =
  14. {
  15. {0, -1, 0}
  16. };
  17. // drugi sem block
  18. static struct sembuf op_unlock2[1] =
  19. {
  20. {1, 1, 0}
  21. };
  22. // fun wykoanie struktury
  23. void lock_sem1(int semid)
  24. {
  25. if (semop(semid, &op_lock1[0], 1)<0)
  26. perror("Semaphor1 lock error");
  27. }
  28. // anlokwanie sem
  29. void unlock_sem2(int semid)
  30. {
  31. if (semop(semid, &op_unlock2[0], 1) < 0)
  32. perror("Semaphor2 unlock error");
  33. }
  34.  
  35. int *shmp;
  36. int semid;
  37. int shmid;
  38.  
  39. int main(){
  40.  
  41. int number;
  42. int i;
  43. int time_to_wait;
  44.  
  45. srand(time(0));
  46. // uzykanie dostepu pamieci dzielonej
  47. int shmid= shmget(ftok("/tmp/",123), sizeof(int),0666 | IPC_EXCL);
  48. if (shmid==-1){
  49. perror("Shmget error");
  50. exit(0);
  51. }
  52. // przylacza pamiec dzielona gdzie ma byc przylaczona wsk
  53. shmp = shmat(shmid,0, 0);
  54. if (shmp==NULL){
  55. perror("Shmat error");
  56. exit(0);
  57. }
  58. // uzykanie dostepu semafora tworzy 2
  59. semid = semget(ftok("/tmp/",123), 2, 0666 | IPC_EXCL);
  60. if (semid < 0){
  61. perror("Semget error");
  62. }
  63.  
  64. for(i=0;i<10;i++){
  65.  
  66. // lokuje pierwszy sem
  67. lock_sem1(semid);
  68. number=*shmp;//
  69. unlock_sem2(semid);//anlokuje sem 2
  70. printf("Number: %d\n",number);
  71. time_to_wait=rand()%1000001;
  72. usleep(time_to_wait);
  73. }
  74. shmdt(shmp);// odlaczenie pamiec dzielonej
  75.  
  76. return 0;
  77. }
  78.  
  79.  
  80. -------------------------------------serwer-----------------------------
  81.  
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84. #include <unistd.h>
  85. #include <sys/types.h>
  86. #include <sys/ipc.h>
  87. #include <sys/shm.h>
  88. #include <sys/sem.h>
  89. #include <signal.h>
  90. #include <time.h>
  91.  
  92. // struktoura odblokwanie sam 1
  93. static struct sembuf op_unlock1[1] =
  94. {
  95. {0, 1, 0}
  96. };
  97.  
  98. static struct sembuf op_lock2[1] =
  99. {
  100. {1, -1, 0}
  101. };
  102. // zeby na poczatku odblokowac
  103. static struct sembuf op_unlock2[1] =
  104. {
  105. {1, 1, 0}
  106. };
  107. // fun do odblokowania
  108. void unlock_sem1(int semid)
  109. {
  110. if (semop(semid, &op_unlock1[0], 1) < 0)
  111. perror("Semaphor1 unlock error");
  112. }
  113.  
  114. void lock_sem2(int semid)
  115. {
  116. if (semop(semid, &op_lock2[0], 1)<0)
  117. perror("Semaphor2 lock error");
  118. }
  119.  
  120. void unlock_sem2(int semid)
  121. {
  122. if (semop(semid, &op_unlock2[0], 1) < 0)
  123. perror("Semaphor2 unlock error");
  124. }
  125.  
  126. int *shmp;
  127. int semid;
  128. int shmid;
  129. // funkcja oblsugi syhnalu ctrl c
  130. void signal_function(int nr_sig)
  131. {
  132. if(shmdt(shmp)<0)// odlaczana pamiec
  133. perror("Shmdt error");
  134. if(semctl(semid,0,IPC_RMID,0)<0)//usuwanie sem
  135. perror("Semctl error");
  136. if(shmctl(shmid,IPC_RMID,NULL)<0) // usuwanie pamiec dzielonej
  137. perror("Shmctl error");
  138. exit(0);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement