// to compile: $gcc -o display display.c -lrt -lpthread #include #include #include #include #include #include int main(void) { const char *nameShm = "/ELO330_C1_2s24_SHM"; /* file name */ const int SIZE = sizeof(double); /* share memory size */ const char *nameFree = "/ELO330_C1_2s24_FREE"; const char *nameNew = "/ELO330_C1_2s24_NEW"; int shm_fd; // file descriptor, used with shm_open() double *newPrice; /* base address, used with mmap() */ sem_t * free_sem, *new_sem; /* open the shared memory segment as if it were a file */ shm_fd = shm_open(nameShm, O_RDONLY, 0666); /* map the shared memory segment to the address space of the process */ newPrice = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0); /** Open the two named semaphores */ /* create and initialize the semaphore */ free_sem = sem_open(nameFree, 0); new_sem = sem_open(nameNew, 0); do { sem_wait(new_sem); sem_wait(free_sem); /* read from the mapped shared memory segment */ printf("%.2f\n", *newPrice); sem_post(free_sem); } while (*newPrice > 0); sem_close(free_sem); sem_unlink(nameFree); sem_close(new_sem); sem_unlink(nameNew); /* remove the mapped shared memory segment from the address space of the process */ munmap(newPrice, SIZE); /* close the shared memory segment as if it was a file */ close(shm_fd); exit(0); }