posix.1提供了两种在无亲缘关系的进程间共享内存的方式
(1)内存映射文件:由open函数打开一个文件,由mmap函数把得到的描述符映射到当前进程地址空间中。
(2)共享内存区对象:由shm_open打开一个Posix.1 IPC名字(通常是文件系统中的一个路径名),所返回的描述符由mmap函数映射到当前进程地址空间。
posix的消息队列和信号量都是一次调用直接返回,mq_open返回mqd_t值,sem_open返回sem_t值的指针,而posix的共享内存却需要先shm_open(或者open)然后再mmap,这是因为posix在发明自己的共享内存形式时,已经存在mmap...
/* shm_open, shm_unlink - create/open or unlink POSIX shared memory objects */ #include <sys/mman.h> #include <sys/stat.h> /* For mode constants */ #include <fcntl.h> /* For O_* constants */ int shm_open(const char *name, int oflag, mode_t mode); int shm_unlink(const char *name); /* mmap, munmap - map or unmap files or devices into memory */ #include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); int munmap(void *addr, size_t length); /* truncate, ftruncate - truncate a file to a specified length */ #include <unistd.h> #include <sys/types.h> int truncate(const char *path, off_t length); int ftruncate(int fd, off_t length); Link with -lrt.注:可以调用ftruncate来修改共享内存区的大小
注:shmctl的cmd可以取值如下:
IPC_RMID: 删除指定共享内存区
IPC_SET: 支持设置指定共享内存区的 shmid_ds结构体中的 shm_prem.uid, shm_prem.gid 和 shm_prem.mode
IPC_STAT: 返回指定共享内存区的shmid_ds 结构
System V 共享内存区的限制
Posix 共享内存和System V共享内存的区别
Posix共享内存区对象的大小可在任何时刻通过调用ftruncate修改,而System V共享内存的大小是在调用shmget创建时固定下来的。
转载于:https://www.cnblogs.com/xiaokuang/p/4629232.html
相关资源:IPC共享内存