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

T

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.
 
J

jmcgill

Tom said:
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.
 
K

Knemon

Tom said:
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++.
 
S

Simon Biber

Tom said:
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("Unexpected 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
 
T

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.

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
 
T

Tom

Tom said:
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("Unexpected 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

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

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.
 
P

pete

Tom said:
Tom said:
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("Unexpected 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

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

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.
 
S

Simon Biber

Tom said:
[...]
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.
 
I

Ico

[ 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.
 
X

xenonysf

ok guys;

i prefer u to look at memory mapped i/o api functions
for such things at win32...
[ 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.
 
K

Kenny McCormack

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[/QUOTE]

Gee. ya think?
and providing an example of noportable code

I think the point is that Simon's code was entirely portable.
And, like most entirely portable code, utterly useless.
 
K

Kenny McCormack

Tom said:
[...]
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.

The thing about sarcasm is that it only works on a defenseless opponent.
Or, TPIAW, it fizzles when it is met in kind.
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.

Actually, there's no difference. The thing you are missing is that the
RAM disk driver is built-in in (more or less recent versions of) Linux.
So, all you are doing is invoking the user-land functions that control
the kernel driver (*).

Just as it is in DOS/Windows as long as the appropriated DEVICE=
line exists in CONFIG.SYS (or whatever the current equivalent is).

(*) Note that there exist similar drivers in DOS/Windows - where a
user-land utility controls the kernel driver.
 
K

Kenny McCormack

[ 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.

Yes. You.
 
F

Flash Gordon

Tom said:
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.

It's no more difficult and no less system specific than it was back
then. The difference is that if you are doing it to speed things up you
are quite possibly wasting your time and effort. Ever head of caching?
It's something some OSs are quite good at.
> 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.

True. If the user wants your data files on a RAM disk they can create
one and put them there. Such as on Linux making /tmp a ram disk.
2) The task is VERY sensitive to the operating system.
Yes.

3) Some folks in here absolutely hate usage of the ++ tag onto there
favorite language C.

Not necessarily. It is just that C++ is a very different language with
its own new group. So C++ is no more or less topical here than Fortran,
ML, TMS320C25 assembler or any of the other languages other than C that
I have used.
> Additionally they have little patience with
anyone who has diminutive skills compared to themselves. I certainly

No, people have a lot of patience with those who have little knowledge
or skill. We have recently had some threads where people with little
knowledge or skill but who wanted to learn and were asking topical (if
simple) questions where the posts were not merely answered but the
original posters complimented. The difference is they were asking C
questions.
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.

Complaining at us is not the best method I've ever seen of getting advice.
> 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.

C knows nothing of disks, file systems, space, caching, ram disks or
lots of other stuff. It does know about streams, but which the file name
means is entirely up to the system and nothing to do with C.
> Perhaps it is too cumbersome to create the
actual ram disk in C?

You can't in C and you never have been able to. You may be able to do it
with OS specific extensions but that discussion belongs in an OS
specific group.
> But that still leaves my need to determine the
free space within the C code and provide logic that
manages the ram drive appropriately.

You can't with standard C. You will have to ask how to do that in a
group dedicated to your system.

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.

Then profile it. Until you have *measured* and *know* what the bottle
neck is there is no point in doing anything.
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).

Since size_t is not a number I'm and SIZE_MAX is unlikely to be
0x7CFFFFFF I suspect you have misread it. Ask in a Windows news group
since all we know is it is allowing arrays a lot larger than the
standard says it has to.
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!!

<snip>

Then I *seriously* suggest you profile it. It is possible that even
Windows can manage caching well enough these days and that your problem
is something different.
 
I

Ico

Kenny McCormack said:
Tom said:
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.

Yes. You.

Re-reading the previous posts I must admit you are right. I guess for me
being a nerd with few social skills it is easier to recognize sarcasm in
code then in normal language. Scary.
 
S

Simon Biber

Kenny said:
I thought that Simon was being sarcastic

Gee. ya think?[/QUOTE]

Call it sarcasm, or a joke, or perhaps just taking things too literally.

I'm still not sure whether Tom's reply was being sarcastic in return or
if he was really thanking me without realising that the code was useless
to him.
I think the point is that Simon's code was entirely portable.
And, like most entirely portable code, utterly useless.

Well, if you're running it on a Linux system, it's not utterly useless.
It does achieve what it set out to do. The error handling could be
greatly improved, and there are certainly better ways to get the free
space on a file system than trying to parse the output of 'df'.

By the way: my program reported a value as bytes that is actually the
number of kilobytes free. If you want bytes, add a --block-size=1 option
to the df call.
 
J

jmcgill

Simon said:
system("mkdir -p /mnt/ramdisk");
system("/sbin/mke2fs -q /dev/ram");
system("mount /dev/ram /mnt/ramdisk");
system("df /mnt/ramdisk > tmp.txt");

I'm going to feel like a total asshole if this was actually what the OP
had in mind.
 
J

jmcgill

Flash said:
It's no more difficult and no less system specific than it was back
then. The difference is that if you are doing it to speed things up you
are quite possibly wasting your time and effort. Ever head of caching?
It's something some OSs are quite good at.

It still makes sense on diskless clients that boot over NFS and so on.
It's also indicated in some applications where security is sensitive, or
for devices that need to operate in environments where moving parts are
impractical.
 
F

Flash Gordon

jmcgill said:
It still makes sense on diskless clients that boot over NFS and so on.
It's also indicated in some applications where security is sensitive, or
for devices that need to operate in environments where moving parts are
impractical.

I agree there are good reasons for it sometimes. I just don't believe
that they necessarily apply to the OP. The maxim of measure first, on
the other hand, definitely does apply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top