Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4. #include<time.h>
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7. #include <sys/sem.h>
  8. #include <sys/msg.h>
  9. #include <values.h>
  10. #include <sys/types.h>
  11.  
  12. static int brdretvi;
  13. static int brstolova;
  14. int *BROJ;
  15. int *ULAZ;
  16. int *STOLOVI;
  17.  
  18. void PocetakKO(int i)
  19. {
  20.    int j,n;
  21.    ULAZ[i] = 1;
  22.    BROJ[i] = BROJ[0];
  23.    for (j=1;j<n;j++){
  24.         if (BROJ[i]>BROJ[j])
  25.              BROJ[i]=BROJ[j];
  26.    }
  27.    BROJ[i]++;
  28.    ULAZ[i] = 0;
  29.  
  30.    for ( j = 0; j<brdretvi;j++){
  31.       while (ULAZ[j] != 0);
  32.       while (BROJ[j] != 0 && (BROJ[j] < BROJ[i] || (BROJ[j] == BROJ[i] && j < i)));
  33.    }
  34. }
  35.  
  36.  
  37.  
  38. void KrajKO(int i){
  39. BROJ[i]=0;
  40. }
  41.  
  42.  
  43.  
  44. void *Rezervacija(void *rbr)
  45. {
  46.         int i = *((int*) rbr);
  47.         int zauzeti = 0;
  48.         int random = 0;
  49.         int j,brslobodnih=0;
  50.         int RegistarSlobodnih[brstolova];
  51.         srand(time(NULL));
  52.         sleep(1);
  53.  
  54. //Petlja koja ne staje do kad svi stolovi nisu popunjeni
  55.         while(zauzeti < brstolova)
  56.         {
  57.          //izrada registra sa popisom slobodnih stolova
  58.          brslobodnih=0;
  59.          j=0;
  60.         while(j<brstolova){
  61.                 if(STOLOVI[j] == 0)
  62.                         {              
  63.                         RegistarSlobodnih[brslobodnih] = j;
  64.                         brslobodnih++;
  65.                         }
  66.                 j++;
  67.         }
  68.         if(brslobodnih >0) random = rand()%brslobodnih;
  69.     else exit(0);
  70.         printf("Dretva %d: odabirem stol %d\n", i+1, RegistarSlobodnih[random]+1);
  71.         sleep(1);
  72.         PocetakKO(i);
  73.         if(STOLOVI[RegistarSlobodnih[random]] == 0){
  74.                
  75.                 STOLOVI[RegistarSlobodnih[random]] = i+1;
  76.                 printf("Dretva %d: rezerviram stol %d\n", i+1, RegistarSlobodnih[random]+1);
  77.                         zauzeti++;
  78.                
  79.          }else {printf("Dretva %d: neuspjela rezervacija stola %d\n",  i+1,
  80.                         RegistarSlobodnih[random]+1);
  81.                 }
  82.         printf("Trenutno stanje stolova:\n");
  83.         for(j=0;j<brstolova; ++j)
  84.         {
  85.                 if(STOLOVI[j] == 0)
  86.                         printf("-");
  87.                 else
  88.                         printf("%d",STOLOVI[j]);              
  89.                
  90.         }
  91.         printf("\n");
  92.         KrajKO(i);
  93.         }
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100. int main (int argc, char*argv[])
  101. {
  102.  
  103.     brdretvi = atoi(argv[1]);
  104.     brstolova = atoi(argv[2]);
  105.     pthread_t t[brdretvi];
  106.     BROJ = calloc(brdretvi, sizeof(int));
  107.     ULAZ = calloc(brdretvi, sizeof(int));
  108.     STOLOVI = calloc(brstolova, sizeof(int));
  109.     int i,br[brdretvi];
  110.  
  111.     for (i=0;i<brdretvi;i++){
  112.                 br[i]=i;
  113.                 if (pthread_create (&t[i],NULL,Rezervacija,(void*)&br[i])){
  114.                         printf ("Greska\n");
  115.                         exit (0);
  116.                 }
  117.         }
  118.     for (i=0;i<brdretvi;i++) pthread_join (t[i],NULL);
  119.  
  120.  
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement