Advertisement
Guest User

klient

a guest
Nov 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7. #include <sys/sem.h>
  8. #include <signal.h>
  9.  
  10. static struct sembuf op_lock1[1] = {0,-1,1};
  11. static struct sembuf op_unlock1[1] = {0,1,0};
  12.  
  13. static struct sembuf op_lock2[1] = {1,-1,0};
  14. static struct sembuf op_unlock2[1] = {1,1,0};
  15.  
  16. void blokuj_sem1(int semid){
  17. if(semop(semid, &op_lock1[0],1)<0)
  18. perror("blad blokowania semafora");
  19. }
  20.  
  21. void odblokuj_sem1(int semid){
  22. if(semop(semid,&op_unlock1[0],1)<0)
  23. perror("blad odblokowania semafora");
  24. }
  25.  
  26. void blokuj_sem2(int semid){
  27. if(semop(semid, &op_lock2[0],1)<0)
  28. perror("blad blokowania semafora");
  29. }
  30.  
  31. void odblokuj_sem2(int semid){
  32. if(semop(semid,&op_unlock2[0],1)<0)
  33. perror("blad odblokowania semafora");
  34. }
  35.  
  36. char *shmp;
  37. int semid;
  38. int shmid;
  39.  
  40. int main(){
  41. srand(getpid());
  42. int shmid = shmget(ftok("serwer.c",3),sizeof(int),0666);
  43. if(shmid == -1){
  44. perror("blad tworzenia pamieci dzielonej");
  45. exit(0);
  46. }
  47.  
  48. char *shmp = shmat(shmid,0,0);
  49. if(shmp == NULL){
  50. perror("blad dolaczenia pamieci dzielonej");
  51. exit(0);
  52. }
  53.  
  54. semid = semget(ftok("serwer.c",3), 2 ,0666);
  55. if(semid<0){
  56. perror("blad tworzenia zbioru semaforów");
  57. }
  58.  
  59. int liczba;
  60. int i;
  61. for(i = 0;i<10;i++){
  62. odblokuj_sem2(semid);
  63. blokuj_sem1(semid);
  64. liczba=*shmp;
  65. printf("liczba %d\n",liczba);
  66. int time = rand()%1000001;
  67. usleep(time);
  68. }
  69. shmdt(shmp);
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement