473,695 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I create a Ram Disk within C/C++ program?

Tom
I'd greatly appreciate advice and code snippets on how to create a ram
disk within a C/C++ program.

I also need to be able to determine the free space.

Thanks in advance for any help.
Aug 28 '06 #1
34 29878
Tom wrote:
I'd greatly appreciate advice and code snippets on how to create a ram
disk within a C/C++ program.
The particular semantics of a "ram disk" depend entirely on how your
operating system implements filesystems and/or storage devices.

I could go into great detail of how ram disks are implemented in C, in
the linux driver, but it would be way off topic for comp.lang.c.

Do you actually want a RAM disk, or do you merely want a memory
structure and access methods that work like a RAM disk but are local to
your program? Maybe what you really want is an in-memory database.

Or maybe you really do want to write a RAM disk. In that case you'd
better decide what platform you're using and ask in a forum appropriate
to it.

Somewhere, I have example code that includes a Windows NT RAM Disk
driver which is implemented in C++ and inline ASM.
Aug 28 '06 #2
Tom wrote:
I'd greatly appreciate advice and code snippets on how to create a ram
disk within a C/C++ program.

I also need to be able to determine the free space.
Any ram disk you create with the fictional language C/C++ will be
fictional as well. As the author of your short story, you can create
that ram disk and determine the free space any way you wish.

If you choose to use an actual programming language like C (or its
bastard brother C++), you will find that such implementation-specific
details are not part of C (or C++). You will need to use
implementation- or platform-specific functionality, for which the
correct newsgroups are concerned with your implementation or platform,
not with the languages C or C++.
Aug 28 '06 #3
Tom wrote:
I'd greatly appreciate advice and code snippets on how to create a ram
disk within a C/C++ program.

I also need to be able to determine the free space.
#include <stdio.h>
#include <stdlib.h>

long long create_ramdisk( void)
{
FILE *fp;
int ch;
long long space = 0;
system("mkdir -p /mnt/ramdisk");
system("/sbin/mke2fs -q /dev/ram");
system("mount /dev/ram /mnt/ramdisk");
system("df /mnt/ramdisk tmp.txt");
fp = fopen("tmp.txt" , "r");
if(!fp)
{
printf("Can't open file\n");
return 0;
}
while((ch = fgetc(fp)) != EOF && ch != '\n');
while((ch = fgetc(fp)) != EOF && ch != ' ');
while((ch = fgetc(fp)) != EOF && ch == ' ');
while((ch = fgetc(fp)) != EOF && ch != ' ');
while((ch = fgetc(fp)) != EOF && ch == ' ');
while((ch = fgetc(fp)) != EOF && ch != ' ');
while((ch = fgetc(fp)) != EOF && ch == ' ');
ungetc(ch, fp);
if(feof(fp))
{
printf("Unexpec ted read\n");
return 0;
}
fscanf(fp, "%lld", &space);
fclose(fp);
remove("tmp.txt ");
return space;
}

void remove_ramdisk( void)
{
system("umount /mnt/ramdisk");
}

int main(void)
{
long long space = create_ramdisk( );
printf("Ramdisk has %lld bytes free\n", space);
remove_ramdisk( );
return 0;
}

[sbiber@eagle c]$ gcc -std=c99 -pedantic -Wall -W -O2 ramdisk.c
[sbiber@eagle c]$ sudo ./a.out
Ramdisk has 14904 bytes free

--
Simon.

--
Posted via a free Usenet account from http://www.teranews.com

Aug 28 '06 #4
Tom
On Mon, 28 Aug 2006 05:10:48 GMT, Tom <Th********@ear thlink.net>
wrote:
>I'd greatly appreciate advice and code snippets on how to create a ram
disk within a C/C++ program.

I also need to be able to determine the free space.

Thanks in advance for any help.
Yikes! -- I just realized how specific and problematic something I use
to do back in 1986 has become! Anybody remember the $1986 AT&T model
6300 sold in the year 1986? With that dual floppy 8 k byte ram green
monochrome monitor system I used a ram disk to speed things up. Floppy
drive reads were very slow. From the comments received I have gleaned
at least a few things.

1) The route I was hoping to take may not be the best choice.

2) The task is VERY sensitive to the operating system.

3) Some folks in here absolutely hate usage of the ++ tag onto there
favorite language C. Additionally they have little patience with
anyone who has diminutive skills compared to themselves. I certainly
apologized, if needed, for using the ++ and I really am trying to
become less stupid.

Even though it would be ideal for there to be an established news
group of experts for my particular challenge ... I will again wish for
some additional advice here in this group. After all ... my program
(that I have worked years on) is about 96% C and 4% ++. Additionally,
the ++ groups seem to be even faster in slamming up barriers and
crying out that you are in the wrong place! But again, what I hope to
do is within a C program and not something done purely at the
operating system level. Perhaps it is too cumbersome to create the
actual ram disk in C? But that still leaves my need to determine the
free space within the C code and provide logic that
manages the ram drive appropriately.

=============== =============== =============== =============== =========
Additional information for those kind enough to have read this far >>

1) - I have a very large sequential data file. Approximately 400 M
byte in size. As I process this data I need to dynamically optimize on
a moving window of smaller size (approximately 20 M byte window size).
Specifically, the large file contains 20+ years of S&P index data and
the window represents a smaller time frame of this same data. The
optimization program is iterative and is used to generate settings for
forward performance evaluation. The program and the method are thus
totally blind to the future data and this provides a realistic test of
the method's long term robustness.

The optimization method iterates through the moving window 100's of
times before declaring the multi-dimensional, non-linear topology
space adequately searched. I have been using file pointers to reset to
the beginning of the moving window for each iteration. This works but
I have no clue about how efficient the cache management system is
working. I have no information on how many times blocks of data are
being read from the hard drive. I do know the program is slow. It can
take 60 hours to process 7 years of data while moving the edge of the
window every 3 months.

In the past I tried using very large storage arrays. My system has
sufficient memory ... but once I set the array size above
approximately 600,000 the program will not compile. This is
troublesome because Microsoft's help reference indicates that arrays
can be of size_t which provides a limit of 0x7CFFFFFF (or
2,097,151,999 -- if I correctly converted).

Most likely there are better ways to solve the problem than using a
Ram disk, but I am clueless to what they may be. I am certainly open
to all suggestions!!

The program does work when digesting the full data file. I have tuned
up the entire 20+ years to one set of parameters successfully;
however, this in itself does not demonstrate dynamic tuning
efficiency.

I was hoping to put the moving window data into ram to speed up data
throughput and possible reduce hard drive wear and tear. When I run
the program; about 1/2 of my system's 1 Gig of memory is not being
used ... So there is plenty of memory available. The program is to
clear the memory space and reload data only when the window is moved.
Not when simply iterating through the data for the tuning of that
specific time period. Also the program is to include logic to prevent
writing too much data to the ram disk.

2) I am using Win 2000 and Visual C++.NET.

3) Whether you can provide helpful suggestions or not ... I thank you
having read the above. Best wishes to all.

-- Tom
Aug 28 '06 #5
Tom
On Mon, 28 Aug 2006 20:30:31 +1000, Simon Biber <te******@ralmi n.cc>
wrote:
>Tom wrote:
>I'd greatly appreciate advice and code snippets on how to create a ram
disk within a C/C++ program.

I also need to be able to determine the free space.

#include <stdio.h>
#include <stdlib.h>

long long create_ramdisk( void)
{
FILE *fp;
int ch;
long long space = 0;
system("mkdir -p /mnt/ramdisk");
system("/sbin/mke2fs -q /dev/ram");
system("mount /dev/ram /mnt/ramdisk");
system("df /mnt/ramdisk tmp.txt");
fp = fopen("tmp.txt" , "r");
if(!fp)
{
printf("Can't open file\n");
return 0;
}
while((ch = fgetc(fp)) != EOF && ch != '\n');
while((ch = fgetc(fp)) != EOF && ch != ' ');
while((ch = fgetc(fp)) != EOF && ch == ' ');
while((ch = fgetc(fp)) != EOF && ch != ' ');
while((ch = fgetc(fp)) != EOF && ch == ' ');
while((ch = fgetc(fp)) != EOF && ch != ' ');
while((ch = fgetc(fp)) != EOF && ch == ' ');
ungetc(ch, fp);
if(feof(fp))
{
printf("Unexpec ted read\n");
return 0;
}
fscanf(fp, "%lld", &space);
fclose(fp);
remove("tmp.txt ");
return space;
}

void remove_ramdisk( void)
{
system("umount /mnt/ramdisk");
}

int main(void)
{
long long space = create_ramdisk( );
printf("Ramdisk has %lld bytes free\n", space);
remove_ramdisk( );
return 0;
}

[sbiber@eagle c]$ gcc -std=c99 -pedantic -Wall -W -O2 ramdisk.c
[sbiber@eagle c]$ sudo ./a.out
Ramdisk has 14904 bytes free

--
Simon.
=============== =============== =============== =============== ======

Holy smokes!!!

While I was writing a reply to the first two responses ... Simon
posted what appears to be a total solution!!

I am groggy from lack of sleep and must rest before I test it ... but
I absolutely must acknowledge Simon's help.

Thank you, Thank you, Thank you !!!
Truthfully, some of the syntax and instructions in Simon's example are
new to me. I am already learning just from having read through it!

Please, where did you get this code? If you are the author ... you da
man! :) If you have an online source you referenced for this example
.... oh please share that too!

Redundantly, but well deserved ... Thank you again.

Aug 28 '06 #6
Tom wrote:
>
On Mon, 28 Aug 2006 20:30:31 +1000, Simon Biber <te******@ralmi n.cc>
wrote:
Tom wrote:
I'd greatly appreciate advice and
code snippets on how to create a ram
disk within a C/C++ program.

I also need to be able to determine the free space.
#include <stdio.h>
#include <stdlib.h>

long long create_ramdisk( void)
{
FILE *fp;
int ch;
long long space = 0;
system("mkdir -p /mnt/ramdisk");
system("/sbin/mke2fs -q /dev/ram");
system("mount /dev/ram /mnt/ramdisk");
system("df /mnt/ramdisk tmp.txt");
fp = fopen("tmp.txt" , "r");
if(!fp)
{
printf("Can't open file\n");
return 0;
}
while((ch = fgetc(fp)) != EOF && ch != '\n');
while((ch = fgetc(fp)) != EOF && ch != ' ');
while((ch = fgetc(fp)) != EOF && ch == ' ');
while((ch = fgetc(fp)) != EOF && ch != ' ');
while((ch = fgetc(fp)) != EOF && ch == ' ');
while((ch = fgetc(fp)) != EOF && ch != ' ');
while((ch = fgetc(fp)) != EOF && ch == ' ');
ungetc(ch, fp);
if(feof(fp))
{
printf("Unexpec ted read\n");
return 0;
}
fscanf(fp, "%lld", &space);
fclose(fp);
remove("tmp.txt ");
return space;
}

void remove_ramdisk( void)
{
system("umount /mnt/ramdisk");
}

int main(void)
{
long long space = create_ramdisk( );
printf("Ramdisk has %lld bytes free\n", space);
remove_ramdisk( );
return 0;
}

[sbiber@eagle c]$ gcc -std=c99 -pedantic -Wall -W -O2 ramdisk.c
[sbiber@eagle c]$ sudo ./a.out
Ramdisk has 14904 bytes free

--
Simon.

=============== =============== =============== =============== ======

Holy smokes!!!

While I was writing a reply to the first two responses ... Simon
posted what appears to be a total solution!!

I am groggy from lack of sleep and must rest before I test it ... but
I absolutely must acknowledge Simon's help.

Thank you, Thank you, Thank you !!!

Truthfully, some of the syntax and instructions in Simon's example are
new to me. I am already learning just from having read through it!
I thought that Simon was being sarcastic
and providing an example of noportable code
for the purpose of illustrating that this was
a system specific problem and not a C problem.

--
pete
Aug 28 '06 #7
Tom wrote:
On Mon, 28 Aug 2006 20:30:31 +1000, Simon Biber <te******@ralmi n.cc>
wrote:
[...]
> system("mkdir -p /mnt/ramdisk");
system("/sbin/mke2fs -q /dev/ram");
system("mount /dev/ram /mnt/ramdisk");
system("df /mnt/ramdisk tmp.txt");

While I was writing a reply to the first two responses ... Simon
posted what appears to be a total solution!!
If you had actually read and understood my code, you'd see that it is a
bit of a joke. It is pure C code, and does actually create a ramdisk,
but it only works on Linux. All the real work is done by Linux commands
that are called through the 'system' function.

Unfortunately I don't believe a similar solution is possible on Windows.
You need to actually write a driver or use someone's existing ramdisk
driver. That's rather difficult, and of course, very system specific.

I recommend you ask in a place that specifically discusses Windows
programming.

--
Simon.

--
Posted via a free Usenet account from http://www.teranews.com

Aug 28 '06 #8
Ico
Tom <Th********@ear thlink.netwrote :
On Mon, 28 Aug 2006 20:30:31 +1000, Simon Biber <te******@ralmi n.cc>
wrote:
>>Tom wrote:
>>I'd greatly appreciate advice and code snippets on how to create a ram
disk within a C/C++ program.

I also need to be able to determine the free space.
[ snip code ]

Holy smokes!!!
>
While I was writing a reply to the first two responses ... Simon
posted what appears to be a total solution!!

I am groggy from lack of sleep and must rest before I test it ... but
I absolutely must acknowledge Simon's help.

Thank you, Thank you, Thank you !!!
I guess somebody here is oblivious of sarcasm.
--
:wq
^X^Cy^K^X^C^C^C ^C
Aug 28 '06 #9
ok guys;

i prefer u to look at memory mapped i/o api functions
for such things at win32...

Ico wrote:
Tom <Th********@ear thlink.netwrote :
On Mon, 28 Aug 2006 20:30:31 +1000, Simon Biber <te******@ralmi n.cc>
wrote:
>Tom wrote:
I'd greatly appreciate advice and code snippets on how to create a ram
disk within a C/C++ program.

I also need to be able to determine the free space.

[ snip code ]

Holy smokes!!!

While I was writing a reply to the first two responses ... Simon
posted what appears to be a total solution!!

I am groggy from lack of sleep and must rest before I test it ... but
I absolutely must acknowledge Simon's help.

Thank you, Thank you, Thank you !!!

I guess somebody here is oblivious of sarcasm.
--
:wq
^X^Cy^K^X^C^C^C ^C
Aug 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1786
by: codecraig | last post by:
Hi, Is there a way to create one .exe using py2exe (or some other extension/utility that can do it)? Basically i want to generate one .exe that contains everything my python app needs to run instead of having a .exe, some .zips, etc. thanks
5
7571
by: Yasaswi Pulavarti | last post by:
does a command like, db2 drop table tabschema.tabname when run from the Aix prompt reclaim the disk space? Are there any other options? How can we make sure the disk space is reclaimed? Thanks, Yasaswi
7
8859
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
4
1282
by: ad | last post by:
We can use System.IO namespace to create a directory in win-base program. But when I want to create a directory in the server with web-base program, System.IO namespace can't work? How can I do that?
6
1236
by: David Thielen | last post by:
Hi; On my home computer (work computer is fine), when I create a new ASP .NET project, it takes minutes (no exageration) for it to create the project. It is paused forever on a dialog that says something like connectiong to localhost/project. And when I start the debugger, it takes about 20 seconds for the page to come up.
11
2440
by: VJ | last post by:
I have these functions in my application. FileInfo.CopyTo DirectoryInfo.Create File.Copy Directory.Create
2
5487
by: yxq | last post by:
Hello I want to create and delete the folder share, i found that it is ok for generic folder, but it does not work for Root directory(i.e c:\, d:\) The code **************************************************** Dim mc As New ManagementClass("Win32_Share") Dim mo As ManagementObject Dim inParams As ManagementBaseObject Dim outParams As ManagementBaseObject
8
4227
by: Foodbank | last post by:
Hi, Has anyone ever hashed a text file to disk before? I'm trying to convert an in-memory hash to disk hash and I can't find any relevant information to help me. Also, I'd like to use lseek, read, write, and fd if anyone is familiar with them. Any info would be greatly helpful. Thanks, James
2
1869
by: marco.minerva | last post by:
Hi all! Within my PHP page (on an Apache webserver), I must call a shell program that saves a file to disk. The program is executed correctly, but nothing is saved; I think this is a permission problem. I can't modify the program, so there is a way to solve the problem? Thanks in advance. -- Marco Minerva, marco.minerva@gmail.com
0
8553
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9112
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8822
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8815
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6483
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5827
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4332
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4570
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.