共享内存写端:
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
int main() {
key_t key = ftok("/dev/shm/myshm2", 0);
int shm_id = shmget(key, 0x400000, IPC_CREAT | 0666);
char *p = (char *)shmat(shm_id, NULL, 0);
memset(p, 'A', 0x400000);
shmdt(p);
return 0;
}
共享内存读端:
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include <iostream>
int main() {
key_t key = ftok("/dev/shm/myshm2", 0);
int shm_id = shmget(key, 0x400000, 0666);
char *p = (char *)shmat(shm_id, NULL, 0);
printf("%c %c %c %c\n", p[0], p[1], p[2], p[3]);
shmdt(p);
return 0;
}