// Jack Sangdahl // CSCI3753 SP24 // PA4 Shared Array #include "array.h" int array_init(array *s) { s->top = 0; sem_init(&s->mutex, 0, 1); sem_init(&s->avail, 0, ARRAY_SIZE); sem_init(&s->empty, 0, 0); return 0; } int array_put(array *s, char *hostname) { sem_wait(&s->avail); sem_wait(&s->mutex); s->data[s->top++] = hostname; sem_post(&s->mutex); sem_post(&s->empty); return 0; } int array_get(array *s, char **hostname) { sem_wait(&s->empty); sem_wait(&s->mutex); *hostname = s->data[--s->top]; sem_post(&s->mutex); sem_post(&s->avail); return 0; } void array_free(array *s) { sem_destroy(&s->mutex); sem_destroy(&s->avail); sem_destroy(&s->empty); }