当前位置: 动力学知识库 > 问答 > 编程问答 >

c - Create mmap within virtual ramdisk

问题描述:

How can I use mmap to reserve memory within a virtual ramdisk? Lets say I do:

mkdir /mnt/ramdisk

mount -t tmpfs -o size=10m tmpfs /mnt/ramdisk

I now have a virtual ramdisk, but how do I map to it within my C program?

int main() {

// ....

addr = mmap(/* ramdisk address? */, size, PROT_READ_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);

网友答案:

The fact that /mnt/ramdisk is a tmpfs doesn't have any bearing on how you use it.

If you wanted to memory-map a file on that device, you'd open() a file and map it with MAP_FILE (not MAP_ANONYMOUS) just as if it were a normal file. Don't pass an address; an appropriate one will be chosen for you.

分享给朋友:
您可能感兴趣的文章:
随机阅读: