Create ramdisk in C ++ on linux

I need to make ramfs mount it in a directory on Linux using C ++. I want to make it as a user (no sudo ).

I need to call the application to the file that I created, and it will be frequent. Writing to the hard drive is very slow.

I found it simple:

 system("mkdir /mnt/ram"); system("mount -t ramfs -o size=20m ramfs /mnt/ram"); 

but it is bad. I want to be a regular user, and the mount command can be called as root. What can I do?

+4
source share
2 answers

I check if /tmp is ramfs, but it is not. It creates files on the hard drive. but when I run df -h it produces:

 rootfs 25G 9,4G 15G 40% / devtmpfs 1,9G 0 1,9G 0% /dev tmpfs 1,9G 1,6G 347M 83% /dev/shm tmpfs 1,9G 1,3M 1,9G 1% /run /dev/mapper/vg_micro-root 25G 9,4G 15G 40% / tmpfs 1,9G 0 1,9G 0% /sys/fs/cgroup tmpfs 1,9G 0 1,9G 0% /media /dev/mapper/vg_micro-stack 289G 191M 274G 1% /stack /dev/mapper/vg_micro-home 322G 40G 266G 14% /home /dev/sda2 485M 89M 371M 20% /boot /dev/sda1 200M 19M 182M 10% /boot/efi 

This means tmpfs (ramdisks): /dev/shm , /run , /sys/fs/cgroup and /media . But only one of them is designed for temporary ramdisk for communication between processes using files. Here is a description and use of /dev/shm . The only thing is that tmpfs will not dynamically grow , but for my purposes this will be enough (20 MB - 1 GB).

-1
source

To solve ramfs for user space, you can use python-fuse-ramfs .

0
source

Source: https://habr.com/ru/post/1436583/


All Articles