/** * Simple program demonstrating shared memory in POSIX systems. * * This is the producer process that writes to the shared memory region. * * Figure 3.17 * * @author Silberschatz, Galvin, and Gagne * Operating System Concepts - Ninth Edition * Copyright John Wiley & Sons - 2013 * * modifications by dheller@cse.psu.edu, 31 Jan. 2014 */ #define _POSIX_C_SOURCE 200112L #include #include #include #include #include #include #include #include #include #include void display(char *prog, char *bytes, int n); int main(void) { const char *name = "/shm-example"; // file name const off_t SIZE = 4096; // file size const char *message0 = "Studying "; const char *message1 = "Operating Systems "; const char *message2 = "Is Fun!"; const char *msg_end = "\n"; int shm_fd; // file descriptor, from shm_open() char *shm_base; // base address, from mmap() char *ptr; // shm_base is fixed, ptr is movable /* create the shared memory segment as if it were a file */ shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666); if (shm_fd == -1) { printf("prod: Shared memory failed: %s\n", strerror(errno)); exit(1); } /* configure the size of the shared memory segment */ ftruncate(shm_fd, SIZE); /* map the shared memory segment to the address space of the process */ shm_base = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); if (shm_base == MAP_FAILED) { printf("prod: Map failed: %s\n", strerror(errno)); // close and shm_unlink? exit(1); } /** * Write to the mapped shared memory region. * * We increment the value of ptr after each write, but we * are ignoring the possibility that sprintf() fails. */ display("prod", shm_base, 64); ptr = shm_base; ptr += sprintf(ptr, "%s", message0); ptr += sprintf(ptr, "%s", message1); ptr += sprintf(ptr, "%s", message2); ptr += sprintf(ptr, "%s", msg_end); display("prod", shm_base, 64); /* remove the mapped memory segment from the address space of the process */ if (munmap(shm_base, SIZE) == -1) { printf("prod: Unmap failed: %s\n", strerror(errno)); exit(1); } /* close the shared memory segment as if it was a file */ if (close(shm_fd) == -1) { printf("prod: Close failed: %s\n", strerror(errno)); exit(1); } /* remove the shared memory segment from the file system after delay */ printf("You have 10 seconds to run the consumer\n"); sleep(10); // we don't care if this fails, because its not there... shm_unlink(name); return EXIT_SUCCESS; } void display(char *prog, char *bytes, int n) { printf("display: %s\n", prog); for (int i = 0; i < n; i++) { printf("%02x%c", bytes[i], ((i+1)%16) ? ' ' : '\n'); } printf("\n"); }