Advertisement
kikosiak

Untitled

Nov 26th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include"bib.h"
  2.  
  3. int *shmp;
  4. int semid;
  5. int shmid;
  6.  
  7. int main(){
  8.     srand(time(NULL));
  9.     int shmid= shmget(ftok("server.c",1), sizeof(int),PERMS);
  10.     if (shmid==-1){
  11.         perror("Blad tworzenia pamieci dzielonej");
  12.         exit(0);
  13.     }
  14.  
  15.     int *shmp = shmat(shmid,0, 0);
  16.     if (shmp==NULL){
  17.         perror("Blad dolaczania pamieci dzielonej");
  18.         exit(0);
  19.     }
  20.  
  21.     semid = semget(ftok("server.c",1), 2, PERMS);
  22.     if  (semid < 0){
  23.         perror("blad tworzenia zbioru semaforow");
  24.     }
  25.  
  26.     int liczba;
  27.     int i;
  28.  
  29.     for(i=0;i<10;i++){
  30.         blokuj_semafor1(semid);
  31.         liczba=*shmp;
  32.         odblokuj_semafor2(semid);
  33. printf("Klient pid: %d ",getpid());
  34.         printf("Odebrana liczba= %d\n",liczba);
  35.         int time=rand()%1000001;
  36.         usleep(time);
  37.     }
  38.     shmdt(shmp);
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement