// to compile: $gcc -o cajero cajero.c -lrt -lpthread #include #include #include #include #include #include int main(void) { const char *name = "/ELO330_C1_2s24_SHM"; /* file name */ const int SIZE = sizeof(double); /* share memory size */ const char *nameFree = "/ELO330_C1_2s24_FREE"; int shm_fd; // file descriptor, used with shm_open() double *price; /* base address, used with mmap() */ double dollars, total; sem_t * free_sem, *new_sem; /* open the shared memory segment as if it were a file */ shm_fd = shm_open(name, O_RDONLY, 0666); /* map the shared memory segment to the address space of the process */ price = 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); do { printf("Amount in dollars: "); scanf("%lf", &dollars); sem_wait(free_sem); /* read from the mapped shared memory segment */ total = dollars*(*price); sem_post(free_sem); printf("Amount in pesos: %.2f\n", total); } while (total > 0); sem_close(free_sem); sem_unlink(nameFree); /* remove the mapped shared memory segment from the address space of the process */ munmap(price, SIZE); /* close the shared memory segment as if it was a file */ close(shm_fd); /* remove the shared memory segment from the file system */ shm_unlink(name); exit(0); }