shmem
简介
POSIX 共享内存
int shm_open(const char *name, int oflag, mode_t mode);
int shm_unlink(const char *name);#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
struct stat stat_buf;
char *buf;
int fd;
fd = shm_open("shmtest", O_RDWR | O_CREAT, 0777);
ftruncate(fd, 4096);
fstat(fd, &stat_buf);
printf("st_size 0x%lx\n", stat_buf.st_size);
buf = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
*buf = 0x11;
printf("buf 0x%x\n", *buf);
munmap(buf, 4096);
shm_unlink("shmtest");
return 0;
}System V 共享内存
Last updated