/* Milutin Aleksic, Dragutin Ostojic, 2022 */ #include #include #include #include typedef struct lista { int br; struct lista *next; } lista; int listaMax, brojRadnika, brojDana; int currentLista = 0; lista *posao; int Insert(int value) { if(currentLista < listaMax) { lista *pom = posao; lista *novi = (lista*)malloc(sizeof(lista)); novi->br = value; novi->next = posao; if(pom == NULL) { novi->next = novi; posao = novi; currentLista++; return 1; } while(pom->next != posao) { pom = pom->next; } pom->next = novi; currentLista++; return 1; } return -1; } int Search(int index) { if(index < currentLista) { lista *pom = posao; while(index--) { pom = pom->next; } return pom->br; } return -1; } int Delete(int index) { if(index < currentLista) { lista *pom = posao; if(index == 0) { while(pom->next != posao) pom = pom->next; lista *temp = posao; posao = posao->next; pom->next = posao; currentLista--; free(temp); return 1; } index--; while(index--) { pom = pom->next; } lista *temp = pom->next; pom->next = temp->next; free(temp); currentLista--; return 1; } return -1; } pthread_mutex_t listaLock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t listaCond = PTHREAD_COND_INITIALIZER; int brisu = 0, citaju = 0, upisuju = 0; int SyncSearch(long id, int index) { pthread_mutex_lock(&listaLock); while(brisu > 0) pthread_cond_wait(&listaCond, &listaLock); citaju++; printf("IZVRSAVANJE: Radnik %ld naredba CITANJE - %d\n", id, index); pthread_mutex_unlock(&listaLock); int res = Search(index); pthread_mutex_lock(&listaLock); citaju--; pthread_cond_broadcast(&listaCond); pthread_mutex_unlock(&listaLock); return res; } int SyncInsert(long id, int value) { pthread_mutex_lock(&listaLock); while(brisu > 0 && upisuju > 0) pthread_cond_wait(&listaCond, &listaLock); upisuju++; printf("IZVRSAVANJE: Radnik %ld naredba UPISIVANJE - %d\n", id, value); pthread_mutex_unlock(&listaLock); int res = Insert(value); pthread_mutex_lock(&listaLock); upisuju--; pthread_mutex_unlock(&listaLock); return res; } int SyncDelete(long id, int index) { pthread_mutex_lock(&listaLock); while(brisu > 0 && upisuju > 0 && citaju > 0) pthread_cond_wait(&listaCond, &listaLock); brisu++; printf("IZVRSAVANJE: Radnik %ld naredba BRISANJE - %d\n", id, index); pthread_mutex_unlock(&listaLock); int res = Delete(index); pthread_mutex_lock(&listaLock); brisu--; pthread_mutex_unlock(&listaLock); return res; } pthread_mutex_t prijava; pthread_barrier_t cekanjePrijave; #define CITANJE 0 #define UPISIVANJE 1 #define BRISANJE 2 int *poslovi, *argumenti; int brojPrijavljenih = 0; char nazivi[3][12] = {"CITANJE", "UPISIVANJE", "BRISANJE"}; void* Manager(void* t) { for(int i=0;i