// to compile: $gcc -o actualizador actualizador.c -lrt -lpthread #include // printf #include // exit #include //O_CREAT #include #include //mmap #include int main(void) { const char *nameShm = "/ELO330_C1_2s24_SHM"; /* file name */ const int SIZE = sizeof(double); /* file 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 *price; /* base address, used with mmap() */ double newPrice; sem_t * free_sem, *new_sem; /* create the shared memory segment as if it were a file */ shm_fd = shm_open(nameShm, O_CREAT | O_RDWR, 0666); /* configure the size of the shared memory segment */ ftruncate(shm_fd, SIZE); /* map the shared memory segment to the address space of the process */ price = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0); // just for writing /** Create two named semaphores /* first remove the semaphore if each one already exists */ sem_unlink(nameFree); sem_unlink(nameNew); /* create and initialize the semaphore */ free_sem = sem_open(nameFree, O_CREAT, 0666, 1); new_sem = sem_open(nameNew, O_CREAT, 0666, 0); do { printf("New price: "); scanf("%lf", &newPrice); sem_wait(free_sem); /** Write to the mapped shared memory region. */ *price=newPrice; sem_post(new_sem); sem_post(free_sem); } while (newPrice > 0); sem_close(free_sem); sem_close(new_sem); /* remove the mapped 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); exit(0); }