Untitled
unknown
plain_text
3 years ago
678 B
5
Indexable
#include <stdio.h> #include <stdlib.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <string.h> #include <unistd.h> int main() { char message[] = "hello"; // parent process will write this message to the shared memory key_t key = ftok("shmfile", 65); int shmid = shmget(key, 1024, 0666 | IPC_CREAT); char *shmem = (char *)shmat(shmid, (void *)0, 0); memcpy(shmem, message, sizeof(message)); int pid = fork(); if (pid == 0) { memcpy(shmem, message, sizeof(message)); printf("Child wrote: %s\n", shmem); } else { wait(NULL); printf("Parent read: %s\n", shmem); } }
Editor is loading...