Haifisch7734

Untitled

Nov 12th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1.  #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/shm.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/sem.h>
  7. #include <time.h>
  8. #include <unistd.h>
  9. #include <signal.h>
  10.  
  11. int id_shmget,semid=-1;
  12. int*adres;
  13. struct shmid_ds *buf;
  14.  
  15. static struct sembuf op_lock[1]={
  16. {0,-1,0}
  17. };
  18.  
  19. static struct sembuf op_unlock[1]={
  20. {1,1,0}
  21. };
  22.  
  23. void blokuj(int semid)
  24. {
  25. if(semop(semid,&op_lock[0],1)<0)
  26.         perror("blad blokowania semafora");
  27. }
  28.  
  29. void odblokuj(int semid)
  30. {
  31. if(semop(semid,&op_unlock[0],1)<0)
  32.         perror("blad odlokowania semafora");
  33. }
  34.  
  35. int main()
  36. {
  37.     srand(time(NULL));
  38.  
  39.     id_shmget=shmget(1337,sizeof(int),0666);
  40. if(id_shmget==-1)
  41. {
  42.         exit(0);
  43. }
  44. else printf ("Utworzono segment pamieci wspolnej \n");
  45.  
  46. if((semid = semget(ftok("serwer.c",0),2, 0666))<0)
  47.         perror("Blad tworzenia semafora");
  48.  
  49.     adres=shmat(id_shmget,0,0);
  50. int czas;
  51. int liczba=1;
  52. int i = 0;
  53. for (i = 0; i<10;i++)
  54. {
  55.         odblokuj(semid);
  56.     blokuj(semid);
  57.     liczba = (*adres);
  58.     printf("%d\n",*adres);
  59.         czas=rand()%1000000;
  60.         usleep(czas);
  61. }
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment