Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 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.     #define PERMS 0666
  10.      
  11.      
  12.      
  13.      
  14.     static struct sembuf op_lock1[1] = {
  15.         0, -1, 0  
  16.     };
  17.      
  18.     static struct sembuf op_unlock1[1] = {
  19.         0, 1, 0
  20.     };
  21.      
  22.     static struct sembuf op_lock2[1] = {
  23.         1, -1, 0  
  24.     };
  25.      
  26.     static struct sembuf op_unlock2[1] = {
  27.         1, 1, 0
  28.     };
  29.      
  30.      
  31.     void blokuj_semafor1(int semid)
  32.     {
  33.         if (semop(semid, &op_lock1[0], 1)<0)
  34.             perror("blad lokowania semafora");
  35.     }
  36.      
  37.     void odblokuj_semafor1(int semid)
  38.     {
  39.         if (semop(semid, &op_unlock1[0], 1) < 0)
  40.             perror("blad odlokowania semafora");
  41.     }
  42.      
  43.     void blokuj_semafor2(int semid)
  44.     {
  45.         if (semop(semid, &op_lock2[0], 1)<0)
  46.             perror("blad lokowania semafora");
  47.     }
  48.      
  49.     void odblokuj_semafor2(int semid)
  50.     {
  51.         if (semop(semid, &op_unlock2[0], 1) < 0)
  52.             perror("blad odlokowania semafora");
  53.     }
  54.      
  55.      
  56.     char *shmp;
  57.     int semid;
  58.     int shmid;
  59.     int run=1;
  60.      
  61.     void oblsluga_zakonczenia_serwera(int nr_sig)
  62.     {
  63.         printf("Koncze serwer\n");
  64.         run=0;
  65.      
  66.         if(shmdt(shmp)<0) perror("Blad odlaczania pamieci dzielonej");
  67.         if(semctl(semid,0,IPC_RMID,0)<0) perror("Blad usuwania zbioru semaforow");
  68.         if(shmctl(shmid,IPC_RMID,NULL)<0) perror("Blad usuwania pamieci dzielonej");
  69.         exit(1);
  70.     }
  71.      
  72.      
  73.     int main(){
  74.         signal(SIGINT,oblsluga_zakonczenia_serwera);
  75.         srand(getpid());
  76.      
  77.         shmid= shmget(ftok("serwer.c",3), sizeof(int),IPC_CREAT | IPC_EXCL | PERMS);
  78.         if (shmid==-1){
  79.             perror("Blad tworzenia pamieci dzielonej");
  80.         }
  81.      
  82.         shmp = shmat(shmid,0, 0);
  83.         if (shmp==NULL){
  84.             perror("Blad dolaczania pamieci dzielonej");
  85.      
  86.         }
  87.      
  88.         semid = semget(ftok("serwer.c",3), 2, IPC_CREAT | PERMS);
  89.         if  (semid < 0){
  90.             perror("blad tworzenia zbioru semaforow");
  91.         }
  92.      
  93.         int liczba=1;
  94.         while(run){
  95.      
  96.             blokuj_semafor2(semid);
  97.      
  98.             int time=rand()%1000001;
  99.             printf("Spie przez %d\n",time);
  100.             *shmp=liczba;
  101.             liczba++;
  102.      
  103.             odblokuj_semafor1(semid);
  104.             usleep(time);
  105.      
  106.         }
  107.      
  108.      
  109.         return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement