/* * ipcshm1.c - Uses shared memory to send data from * a child process to a parent process * NOTE: named shared memory segments are persistent */ #include // needed for pid_t #include // needed for wait system call #include // needed for parameter values for shm_open #include // needed for fork, getpid, getppid, kill system calls #include // needed for exit #include // needed for signal system call #include // needed for printf, perror #include // needed for mmap, munmap, shm system calls #include // needed for strcpy #define MAPPED_SIZE 128 int main() { // Create and open a shared memory segment int shmfd = shm_open("/CS3841MEMORY", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); if(shmfd == -1) { printf("COULD NOT OPEN SHARED MEMORY SEGMENT\n"); exit(EXIT_FAILURE); } // Set the size of the shared memory segment ftruncate(shmfd, MAPPED_SIZE); // Map the segment into the processes address space // NOTE: protection is set to allow reading and writing with a shared mapping void* mapped_space = mmap(NULL, MAPPED_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); if(mapped_space == MAP_FAILED) { printf("COULD NOT MMAP\n"); exit(EXIT_FAILURE); } pid_t pid = fork(); // fork into 2 processes if(pid < 0) // error { perror("fork"); exit(EXIT_FAILURE); } else if(pid == 0) // child { // Child writes to shared memory segment strcpy(mapped_space, "HELLO"); // Unmap the shared memory munmap(mapped_space, MAPPED_SIZE); // Close the shared memory segment close(shmfd); return 0; // Return success } else // parent { // Wait for child to finish wait(0); // Parent reads from shared memory segment printf("Parent reads %s from shared mapped segment\n", (char*)mapped_space); // Unmap the shared memory munmap(mapped_space, MAPPED_SIZE); // Close the shared memory segment close(shmfd); } return 0; // Return success }