Advertisement
kikosiak

Untitled

Nov 25th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  1. ----------server----------
  2. #include "naglowek.h"
  3. int *shmp;
  4. int semid;
  5. int shmid;
  6.  
  7.  
  8. void koniec()
  9. {
  10.     printf("Koniec!\n");
  11.     //run=0;
  12.     if(shmdt(shmp)<0) perror("Blad odlaczania pamieci dzielonej");
  13.     if(semctl(semid,0,IPC_RMID,0)<0) perror("Blad usuwania zbioru semaforow");
  14.     if(shmctl(shmid,IPC_RMID,NULL)<0) perror("Blad usuwania pamieci dzielonej");
  15.     exit(1);
  16. }
  17.  
  18. int main(){
  19.  
  20.     signal(SIGINT,koniec);
  21.     srand(time(NULL));
  22.  
  23.     shmid= shmget(ftok("tmp",7), sizeof(int),IPC_CREAT | IPC_EXCL | PERMS);
  24.  
  25.     if (shmid==-1){
  26.  
  27.         perror("Blad tworzenia pamieci dzielonej");
  28.  
  29.     }
  30.  
  31.     shmp = shmat(shmid,0, 0);
  32.  
  33.     if (shmp==NULL){
  34.  
  35.         perror("Blad dolaczania pamieci dzielonej");
  36.  
  37.     }
  38.  
  39.  
  40.  
  41.     semid = semget(ftok("tmp",7), 2, IPC_CREAT | IPC_EXCL | PERMS);
  42.  
  43.     if  (semid < 0){
  44.  
  45.         perror("blad tworzenia zbioru semaforow");
  46.  
  47.     }
  48.  
  49.  
  50.         odblokuj_semafor2(semid);
  51.     int liczba=1;
  52.  
  53.     while(1){
  54.  
  55.         blokuj_semafor2(semid);
  56.  
  57.         int time=rand()%1000001;
  58.      //usleep(time);
  59.  
  60.         //printf("Wylosowany czas: %0.2f\n",(float)time/1000000);
  61.  
  62.         *shmp=liczba;
  63.  
  64.         liczba++;
  65.  
  66.         odblokuj_semafor1(semid);
  67.  
  68.      
  69.     }
  70.  
  71.     return 0;
  72.  
  73. }
  74.  
  75. --------------client------------------------
  76. #include "naglowek.h"
  77. int *shmp;
  78. int semid;
  79. int shmid;
  80.  
  81. int main(){
  82.  
  83.     srand(time(NULL));
  84.  
  85.     int shmid= shmget(ftok("tmp",7), sizeof(int),PERMS);
  86.  
  87.     if (shmid==-1){
  88.  
  89.         perror("Blad tworzenia pamieci dzielonej");
  90.  
  91.         exit(0);
  92.  
  93.     }
  94.  
  95.     int *shmp = shmat(shmid,0, 0);
  96.  
  97.     if (shmp==NULL){
  98.  
  99.         perror("Blad dolaczania pamieci dzielonej");
  100.  
  101.         exit(0);
  102.  
  103.     }
  104.  
  105.     semid = semget(ftok("tmp",7), 2, PERMS);
  106.  
  107.     if  (semid < 0){
  108.  
  109.         perror("blad tworzenia zbioru semaforow");
  110.  
  111.     }
  112.  
  113.     int liczba;
  114.  
  115.     int i;
  116.  
  117.     for(i=0;i<10;i++){
  118.  
  119.         blokuj_semafor1(semid);
  120.  
  121.         liczba=*shmp;
  122.  
  123.         odblokuj_semafor2(semid);
  124.  
  125.         printf("Odebrana liczba= %d\n",liczba);
  126.  
  127.         int time=rand()%1000001;
  128.  
  129.         usleep(time);
  130.  
  131.     }
  132.  
  133.     shmdt(shmp);
  134.  
  135.     return 0;
  136.  
  137. }
  138. ---------------biblioteka-------------------
  139. #include <stdio.h>
  140.  
  141. #include <stdlib.h>
  142.  
  143. #include <unistd.h>
  144.  
  145. #include <sys/types.h>
  146.  
  147. #include <sys/ipc.h>
  148.  
  149. #include <sys/shm.h>
  150.  
  151. #include <sys/sem.h>
  152.  
  153. #include  <signal.h>
  154.  
  155. #define PERMS 0666
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. static struct sembuf op_lock1[1] = {
  166.  
  167.     0, -1, 0
  168.  
  169. };
  170.  
  171.  
  172.  
  173. static struct sembuf op_unlock1[1] = {
  174.  
  175.     0, 1, 0
  176.  
  177. };
  178.  
  179.  
  180.  
  181. static struct sembuf op_lock2[1] = {
  182.  
  183.     1, -1, 0
  184.  
  185. };
  186.  
  187.  
  188.  
  189. static struct sembuf op_unlock2[1] = {
  190.  
  191.     1, 1, 0
  192.  
  193. };
  194.  
  195. void blokuj_semafor1(int semid)
  196.  
  197. {
  198.  
  199.     if (semop(semid, &op_lock1[0], 1)<0)
  200.  
  201.         perror("blad lokowania semafora");
  202.  
  203. }
  204.  
  205.  
  206.  
  207. void odblokuj_semafor1(int semid)
  208.  
  209. {
  210.  
  211.     if (semop(semid, &op_unlock1[0], 1) < 0)
  212.  
  213.         perror("blad odlokowania semafora");
  214.  
  215. }
  216.  
  217.  
  218.  
  219. void blokuj_semafor2(int semid)
  220.  
  221. {
  222.  
  223.     if (semop(semid, &op_lock2[0], 1)<0)
  224.  
  225.         perror("blad lokowania semafora");
  226.  
  227. }
  228.  
  229.  
  230.  
  231. void odblokuj_semafor2(int semid)
  232.  
  233. {
  234.  
  235.     if (semop(semid, &op_unlock2[0], 1) < 0)
  236.  
  237.         perror("blad odlokowania semafora");
  238.  
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement