你知道Linux的共享内存与tmpfs文件系统是什么样?.doc
《你知道Linux的共享内存与tmpfs文件系统是什么样?.doc》由会员分享,可在线阅读,更多相关《你知道Linux的共享内存与tmpfs文件系统是什么样?.doc(9页珍藏版)》请在三一文库上搜索。
1、你知道Linux的共享内存与tmpfs文件系统是什么样?前言共享内存主要用于进程间通信,Linux有两种共享内存(Shared Memory)机制:(1)* System V shared memory(shmget/shmat/shmdt) *Original shared memory mechanism, still widely usedSharing between unrelated processes.(2)*POSIX shared memory(shm_open/shm_unlink) *Sharing between unrelated processes, without
2、 overhead of filesystem I/OIntended to be simpler and better than older APIs.另外,在Linux中不得不提一下内存映射(也可用于进程间通信):* Shared mappings mmap(2) *lShared anonymous mappings:Sharing between related processes only (related via fork()lShared file mappings:Sharing between unrelated processes, backed by file in fi
3、lesystemSystem V共享内存历史悠久,使用也很广范,很多类Unix系统都支持。一般来说,我们在写程序时也通常使用第一种。这里不再讨论如何使用它们,关于POSIX共享内存的详细介绍可以参考这里1,这里2。*讲到那么多,那么问题来了,共享内存与tmpfs有什么关系?*The POSIX shared memory object implementaTIon on Linux 2.4 makes use of a dedicated filesystem, which is normally mounted under /dev/shm.从这里可以看到,POSIX共享内存是基于tmpfs
4、来实现的。实际上,更进一步,不仅PSM(POSIX shared memory),而且SSM(System V shared memory)在内核也是基于tmpfs实现的。tmpfs介绍下面是内核文档中关于tmpfs的介绍:tmpfs has the following uses:1) There is always a kernel internal mount which you will not see at all. This is used for shared anonymous mappings and SYSV shared memory.This mount does not
5、 depend on CONFIG_TMPFS. If CONFIG_TMPFS is not set, the user visible part of tmpfs is not build. But the internal mechanisms are always present.2) glibc 2.2 and above expects tmpfs to be mounted at /dev/shm for POSIX shared memory (shm_open, shm_unlink). Adding the following line to /etc/fstab shou
6、ld take care of this:tmpfs /dev/shm tmpfs defaults 0 0Remember to create the directory that you intend to mount tmpfs on if necessary.This mount isnotneeded for SYSV shared memory. The internal mount is used for that. (In the 2.3 kernel versions it was necessary to mount the predecessor of tmpfs (sh
7、m fs) to use SYSV shared memory)从这里可以看到tmpfs主要有两个作用:(1)用于SYSV共享内存,还有匿名内存映射;这部分由内核管理,用户不可见;(2)用于POSIX共享内存,由用户负责mount,而且一般mount到/dev/shm;依赖于CONFIG_TMPFS;到这里,我们可以了解,SSM与PSM之间的区别,也明白了/dev/shm的作用。下面我们来做一些测试:测试我们将/dev/shm的tmpfs设置为64M:# mount -size=64M -o remount /dev/shm# df -lhFilesystem Size Used Avail
8、Use% Mounted ontmpfs 64M 0 64M 0% /dev/shmSYSV共享内存的最大大小为32M:# cat /proc/sys/kernel/shmmax33554432(1)创建65M的system V共享内存失败:# ipcmk -M 68157440 ipcmk: create share memory failed: Invalid argument这是正常的。(2)将shmmax调整为65M# echo 68157440 /proc/sys/kernel/shmmax# cat /proc/sys/kernel/shmmax 68157440# ipcmk -
9、M 68157440 Shared memory id: 0# ipcs -m- Shared Memory Segments -key shmid owner perms bytes nattch status 0xef46b249 0 root 644 68157440 0 可以看到system v共享内存的大小并不受/dev/shm的影响。(3)创建POSIX共享内存点击(此处)折叠或打开/*gcc-o shmopen shmopen.c-lrt*/#include#include#include#include#include#include#include#define MAP_SI
10、ZE 68157440int main(intargc,char*argv)intfd;void*result;fd=shm_open(/shm1,O_RDWR|O_CREAT,0644);if(fd#include#include#include#include#include#include#define MAP_SIZE 68157440int main(intargc,char*argv)intfd;void*result;fd=shm_open(/shm1,O_RDWR|O_CREAT,0644);if(fdsize。# stat /dev/shm/shm2File:/dev/shm
11、/shm2Size: 68157440 Blocks: 0 IO Block: 4096 普通文件Device: 10h/16d Inode: 217177 Links: 1Access:(0644/-rw-r-r-)Uid:(0/ root)Gid:(0/ root)Access: 2015-03-03 15:24:28.025985167 +0800Modify: 2015-03-03 15:24:28.025985167 +0800Change: 2015-03-03 15:24:28.025985167 +0800(5)向SYS V共享内存写数据将System V共享内存的最大值调整为
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 知道 Linux 共享 内存 tmpfs 文件系统 什么样
链接地址:https://www.31doc.com/p-3381764.html