Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a RAM disk programmatically?

Tags:

windows

ram

I am not looking for a code that invokes a command line utility, which does the trick. I am actually interested to know the API used to create a RAM disk.

EDIT

Motivation: I have a third party library which expects a directory name in order to process the files in that directory in a certain way. I have these files zipped in an archive. I wish to extract the archive into a RAM disk and pass the third party the path to the respective directory on that RAM disk. As you can see, memory mapped files are of no use to me.

like image 249
mark Avatar asked Jan 01 '12 09:01

mark


People also ask

How do I make a RAM disk?

To create a RAM disk, you would install a third-party program that creates a virtual drive in Windows. This program would reserve a section of your RAM — so if you had 4 GB of files in your RAM disk, the disk would take up 4 GB of RAM. All the files on your disk would be stored in your RAM.

How do I create a RAM disk in Linux?

Create a folder to use as a mount point for your RAM disk. Then use the mount command to create a RAM disk. Substitute the following attirbutes for your own values: [TYPE] is the type of RAM disk to use; either tmpfs or ramfs.

How do I create a Windows RAM disk?

Run ImDisk. On the Basic tab [1], select the size of RAM [2] you want to allocate to the RAM Disk. You can also opt for Dynamic Memory feature by checking 'Allocate Memory Dynamically' [3]. Leave the rest of the settings on default and click on OK [4] to create the RAM Disk.

What is RAM disk in Linux?

The initial RAM disk (initrd) is an initial root file system that is mounted prior to when the real root file system is available. The initrd is bound to the kernel and loaded as part of the kernel boot procedure.


2 Answers

ImDisk is a RAM disk app that creates a virtual drive from a sector of memory, and has an API that can be called from .NET.

class RamDisk {     public const string MountPoint = "X:";      public void createRamDisk()     {          try         {             string initializeDisk   = "imdisk -a ";             string imdiskSize       = "-s 1024M ";             string mountPoint       = "-m "+ MountPoint + " ";               ProcessStartInfo procStartInfo  = new ProcessStartInfo();             procStartInfo.UseShellExecute   = false;             procStartInfo.CreateNoWindow    = true;             procStartInfo.FileName          = "cmd";             procStartInfo.Arguments         = "/C " + initializeDisk + imdiskSize + mountPoint;             Process.Start(procStartInfo);              formatRAMDisk();          }         catch (Exception objException)         {             Console.WriteLine("There was an Error, while trying to create a ramdisk! Do you have imdisk installed?");             Console.WriteLine(objException);         }      }      /**      * since the format option with imdisk doesn't seem to work      * use the fomat X: command via cmd      *       * as I would say in german:      * "Von hinten durch die Brust ins Auge"      * **/     private void formatRAMDisk(){          string cmdFormatHDD = "format " + MountPoint + "/Q /FS:NTFS";          SecureString password = new SecureString();         password.AppendChar('0');         password.AppendChar('8');         password.AppendChar('1');         password.AppendChar('5');          ProcessStartInfo formatRAMDiskProcess   = new ProcessStartInfo();         formatRAMDiskProcess.UseShellExecute    = false;         formatRAMDiskProcess.CreateNoWindow     = true;         formatRAMDiskProcess.RedirectStandardInput     = true;         formatRAMDiskProcess.FileName           = "cmd";         formatRAMDiskProcess.Verb               = "runas";         formatRAMDiskProcess.UserName           = "Administrator";         formatRAMDiskProcess.Password           = password;         formatRAMDiskProcess.Arguments          = "/C " + cmdFormatHDD;         Process process                         = Process.Start(formatRAMDiskProcess);          sendCMDInput(process);     }      private void sendCMDInput(Process process)     {         StreamWriter inputWriter = process.StandardInput;         inputWriter.WriteLine("J");         inputWriter.Flush();         inputWriter.WriteLine("RAMDisk for valueable data");         inputWriter.Flush();     }      public string getMountPoint()     {         return MountPoint;     } } 
like image 192
Robin Rodricks Avatar answered Sep 24 '22 23:09

Robin Rodricks


you didnt mentioned the language , sO my answer is in c# :

A memory-mapped file contains the contents of a file in virtual memory. This mapping between a file and memory space enables an application, including multiple processes, to modify the file by reading and writing directly to the memory. Starting with the .NET Framework version 4, you can use managed code to access memory-mapped files in the same way that native Windows functions access memory-mapped files, as described in Managing Memory-Mapped Files in Win32 in the MSDN Library.

http://msdn.microsoft.com/en-us/library/dd997372.aspx

MemoryMappedFile MemoryMapped = MemoryMappedFile.CreateOrOpen(        new FileStream(@"C:\temp\Map.mp", FileMode.Create), // Any stream will do it              "MyMemMapFile",                                     // Name        1024 * 1024,                                        // Size in bytes        MemoryMappedFileAccess.ReadWrite);                  // Access type 

// Create the map view and read it:

using (MemoryMappedViewAccessor FileMap = MemoryMapped.CreateViewAccessor()) {        Container NewContainer = new Container();        FileMap.Read<Container>(4, out NewContainer); } 
like image 36
Royi Namir Avatar answered Sep 22 '22 23:09

Royi Namir