Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include<time.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/sem.h>
- #include <sys/msg.h>
- #include <values.h>
- #include <sys/types.h>
- static int brdretvi;
- static int brstolova;
- int *BROJ;
- int *ULAZ;
- int *STOLOVI;
- void PocetakKO(int i)
- {
- int j,n;
- ULAZ[i] = 1;
- BROJ[i] = BROJ[0];
- for (j=1;j<n;j++){
- if (BROJ[i]>BROJ[j])
- BROJ[i]=BROJ[j];
- }
- BROJ[i]++;
- ULAZ[i] = 0;
- for ( j = 0; j<brdretvi;j++){
- while (ULAZ[j] != 0);
- while (BROJ[j] != 0 && (BROJ[j] < BROJ[i] || (BROJ[j] == BROJ[i] && j < i)));
- }
- }
- void KrajKO(int i){
- BROJ[i]=0;
- }
- void *Rezervacija(void *rbr)
- {
- int i = *((int*) rbr);
- int zauzeti = 0;
- int random = 0;
- int j,brslobodnih=0;
- int RegistarSlobodnih[brstolova];
- srand(time(NULL));
- sleep(1);
- //Petlja koja ne staje do kad svi stolovi nisu popunjeni
- while(zauzeti < brstolova)
- {
- //izrada registra sa popisom slobodnih stolova
- brslobodnih=0;
- j=0;
- while(j<brstolova){
- if(STOLOVI[j] == 0)
- {
- RegistarSlobodnih[brslobodnih] = j;
- brslobodnih++;
- }
- j++;
- }
- if(brslobodnih >0) random = rand()%brslobodnih;
- else exit(0);
- printf("Dretva %d: odabirem stol %d\n", i+1, RegistarSlobodnih[random]+1);
- sleep(1);
- PocetakKO(i);
- if(STOLOVI[RegistarSlobodnih[random]] == 0){
- STOLOVI[RegistarSlobodnih[random]] = i+1;
- printf("Dretva %d: rezerviram stol %d\n", i+1, RegistarSlobodnih[random]+1);
- zauzeti++;
- }else {printf("Dretva %d: neuspjela rezervacija stola %d\n", i+1,
- RegistarSlobodnih[random]+1);
- }
- printf("Trenutno stanje stolova:\n");
- for(j=0;j<brstolova; ++j)
- {
- if(STOLOVI[j] == 0)
- printf("-");
- else
- printf("%d",STOLOVI[j]);
- }
- printf("\n");
- KrajKO(i);
- }
- }
- int main (int argc, char*argv[])
- {
- brdretvi = atoi(argv[1]);
- brstolova = atoi(argv[2]);
- pthread_t t[brdretvi];
- BROJ = calloc(brdretvi, sizeof(int));
- ULAZ = calloc(brdretvi, sizeof(int));
- STOLOVI = calloc(brstolova, sizeof(int));
- int i,br[brdretvi];
- for (i=0;i<brdretvi;i++){
- br[i]=i;
- if (pthread_create (&t[i],NULL,Rezervacija,(void*)&br[i])){
- printf ("Greska\n");
- exit (0);
- }
- }
- for (i=0;i<brdretvi;i++) pthread_join (t[i],NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement