如何在C / C ++程序中创建Ram磁盘? [英] How can I create a Ram Disk within C/C++ program?

查看:94
本文介绍了如何在C / C ++程序中创建Ram磁盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常感谢有关如何在C / C ++程序中创建ram

磁盘的建议和代码片段。


我还需要能够确定可用空间。


提前感谢您的帮助。

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.

推荐答案

Tom写道:
Tom wrote:

我非常感谢有关如何在C / C ++程序中创建ram

磁盘的建议和代码片段。
I''d greatly appreciate advice and code snippets on how to create a ram
disk within a C/C++ program.



ram disk的特定语义。完全取决于你的操作系统如何实现文件系统和/或存储设备。


我可以详细了解如何在C中实现ram磁盘,在

的linux驱动程序中,但它会偏离comp.lang.c的主题。


你真的想要一个RAM磁盘,还是你呢?只是想要一个内存

结构和访问方法,就像RAM磁盘一样,但是你的程序是本地的吗?也许你真正想要的是一个内存数据库。


或许你真的想写一个RAM磁盘。在这种情况下,你会更好地决定你正在使用什么平台,并在论坛中向它询问




某处,我的示例代码包含一个Windows NT RAM磁盘

驱动程序,它是用C ++和内联ASM实现的。

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.


Tom写道:
Tom wrote:

我非常感谢有关如何在C / C ++程序中创建ram

磁盘的建议和代码片段。


我还需要能够确定可用空间。
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.



您使用虚构语言C / C ++创建的任何ram磁盘也将是虚构的。作为你的短篇小说的作者,你可以创建

ram磁盘并以你想要的任何方式确定可用空间。


如果你选择使用实际的编程语言如C(或它的b / b bastard兄弟C ++),你会发现这种特定于实现的

细节不是C(或C ++)的一部分。您将需要使用

实现或平台特定的功能,

正确的新闻组与您的实现或平台有关,

不是用C或C ++语言。

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


Tom写道:
Tom wrote:

我非常感谢建议和关于如何在C / C ++程序中创建ram

磁盘的代码片段。


我还需要能够确定可用空间。
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(不能打开文件\ n);

返回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 == );

,而((CH =龟etc(FP))= EOF和放大器;!&安培; ch!='''');

while((ch = fgetc(fp))!= EOF&& ch =='''');

ungetc(ch,fp);

if(feof(fp))

{

printf(" Unexpected read\\\
" );

返回0;

}

fscanf(fp,"%lld",& space);

fclose(fp);

删除(" tmp.txt");

返回空格;

}


void remove_ramdisk(void)

{

system(" umount / mnt / ramdisk");

}


int main(无效)

{

long long space = create_ramdisk();

printf(Ramdisk有%lld字节free\\\
,空格);

remove_ramdisk();

返回0;

}


[sbiber @ eagle c]

#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]


这篇关于如何在C / C ++程序中创建Ram磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆