GNU >> C/C++
shmget、shmat、shmdt、shmctl
參考資訊:
1. shmget-for-ipc-in-linux
server.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/shm.h> #include <sys/time.h> #include <unistd.h> int main(int argc, char **argv) { int id = 0; char *p = NULL; id = shmget(9999, 1024, IPC_CREAT | 0666); p = shmat(id, NULL, 0); strcpy(p, "ipc test\n"); usleep(1000000); shmdt(p); printf("server:%d\n", shmctl(id, IPC_RMID, 0)); return 0; }
client.c
#include <stdio.h> #include <sys/shm.h> int main(int argc, char **argv) { int id = 0; char *p = NULL; id = shmget(9999, 1024, IPC_CREAT | 0666); p = shmat(id, NULL, 0); for (; *p; p++) { printf("%c", *p); } shmdt(p); printf("client:%d\n", shmctl(id, IPC_RMID, 0)); return 0; }
編譯、執行
$ gcc server.c -o server $ ./server & ./client [1] 1729 client:0 server:0