Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <semaphore.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6. #include "lib.h"
  7. #include <time.h>
  8.  
  9. typedef struct{
  10.         int muzi;
  11.         int zeny;
  12.         sem_t fmuzi;
  13.         sem_t fzeny;
  14.         sem_t m;
  15.         sem_t dot;
  16.  
  17. }DATA;
  18.  
  19. DATA *data;
  20.  
  21. void muz(int i){
  22.         sem_wait(&data->m);
  23.  
  24.         if(data->zeny > 0){
  25.                 data->zeny--;
  26.                 sem_post(&data->fzeny);
  27.         }else{
  28.                 data->muzi++;
  29.                 sem_post(&data->m);
  30.                 sem_wait(&data->fmuzi);
  31.         }
  32.  
  33.         sem_wait(&data->dot);
  34.  
  35.         printf("tanec muz\n");
  36.  
  37.         sem_post(&data->m);
  38.  
  39. }
  40.  
  41. void zena(int i){
  42.         sem_wait(&data->m);
  43.  
  44.         if(data->muzi > 0){
  45.                 data->muzi--;
  46.                 sem_post(&data->fmuzi);
  47.         }else{
  48.                 data->zeny++;
  49.                 sem_post(&data->m);
  50.                 sem_wait(&data->fzeny);
  51.         }
  52.  
  53.         sem_post(&data->dot);
  54.  
  55.         printf("tanec zena\n");
  56.  
  57. }
  58.  
  59. int main(){
  60.  
  61.  
  62.         data = (DATA*) malloc_shared(sizeof(DATA));
  63.         if(data == NULL){
  64.                 perror("malloc shared \n");
  65.                 exit(-1);
  66.         }
  67.  
  68.         int mcnt = rand()%10 + 5;
  69.         int wcnt = rand()%10 + 5;
  70.         int i;
  71.  
  72.         data->muzi = 0;
  73.         data->zeny = 0;
  74.         sem_init(&data->fmuzi,  1 , 0);
  75.         sem_init(&data->fzeny,  1 , 0);
  76.         sem_init(&data->m,              1 , 1);
  77.         sem_init(&data->dot,    1 , 0);
  78.         srand(time(NULL));
  79.  
  80.  
  81.         for(i = 0; i < mcnt; i++){
  82.                 switch(fork()){
  83.                         case 0:
  84.                                 muz(i);
  85.                                 return 0;
  86.                         case -1:
  87.                                 perror("fork muz \n");
  88.                                 exit(-1);
  89.                         default:
  90.                                 break;
  91.                 }
  92.         }
  93.         for(i = 0; i < wcnt; i++){
  94.                 switch(fork()){
  95.                         case 0:
  96.                                 zena(i);
  97.                                 return 0;
  98.                         case -1:
  99.                                 perror("fork muz \n");
  100.                                 exit(-1);
  101.                         default:
  102.                                 break;
  103.                 }
  104.         }
  105.  
  106.         for(i = 0; i < wcnt + mcnt; i++){
  107.                 wait(NULL);
  108.         }
  109.         free_shared(data);
  110.  
  111.         return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement