#include // Defines fork system call #include // Defines exit system call #include // Defines prinf and sprintf #include // Defines the shared memory API #include // Defines the flags IR_USR and IW_USR #include // Defines the wait system call int main() { int segment_id; char *parent_shared_memory; const int size=4096; pid_t child; // Create the shared memory segment of size size bytes for Read/Write segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR); // Attach the shared memory segment to this process parent_shared_memory = shmat(segment_id, NULL, 0); // Write a message to shared memory sprintf(parent_shared_memory, "%s", "Hello there!"); child = fork(); if (child < 0) { fprintf(stderr, "Error forking child process: %d\n", child); exit(1); } else if (child == 0) { // Attach the shared memory segment to this process char *child_shared_memory = shmat(segment_id, NULL, 0); // Read the message and write to the terminal printf("*%s", child_shared_memory); // Detach shared memory shmdt(child_shared_memory); } else { wait(NULL); // Detach shared memory shmdt(parent_shared_memory); // Release the shared memory segment shmctl(segment_id, IPC_RMID, NULL); } return(0); }