简体   繁体   English

在C中保留RAM

[英]Reserve RAM in C

I need ideas on how to write a C program that reserve a specified amount of MB RAM until a key [ex. 我需要有关如何编写一个C程序的想法,该程序保留指定数量的MB RAM直到一个键[ex。 the any key] is pressed on a Linux 2.6 32 bit system. 在Linux 2.6 32位系统上按下任意键。

*
/.eat_ram.out 200

# If free -m is execute at this time, it should report 200 MB more in the used section, than before running the program.

[Any key is pressed]

# Now all the reserved RAM should be released and the program exits.
*

It is the core functionality of the program [reserving the RAM] i do not know how to do, getting arguments from the commandline, printing [Any key is pressed] and so on is not a problem from me. 它是程序的核心功能[保留RAM]我不知道怎么做,从命令行获取参数,打印[按任意键]等等对我来说不是问题。

Any ideas on how to do this? 关于如何做到这一点的任何想法?

You want to use malloc() to do this. 您想使用malloc()来执行此操作。 Depending on your need, you will also want to: 根据您的需要,您还需要:

  1. Write data to the memory so that the kernel actually guarantees it. 将数据写入内存,以便内核实际保证它。 You can use memset() for this. 你可以使用memset()。
  2. Prevent the memory from being paged out (swapped), the mlock() / mlockall() functions can help you with this. 防止内存被分页(交换),mlock()/ mlockall()函数可以帮助你解决这个问题。
  3. Tell the kernel how you actually intend to use the memory, which is accomplished via posix_madvise() (this is preferable to an explicit mlockall()). 告诉内核你实际打算如何使用内存,这是通过posix_madvise()完成的(这比显式的mlockall()更好)。

In most realities, malloc() and memset() (or, calloc() which effectively does the same) will suit your needs. 在大多数情况下,malloc()和memset()(或者有效地执行相同操作的calloc())将满足您的需求。

Finally, of course, you want to free() the memory when it is no longer needed. 最后,当然,你想在不再需要时释放()内存。

Can't you just use malloc() to allocate that ram to your process? 你不能只使用malloc()将ram分配给你的进程吗? That will reserve that RAM for you, and then you are free to do whatever you wish with it. 那将为你保留RAM,然后你就可以自由地做任何你想做的事了。

Here's an example for you: 这是给你的一个例子:

#include <stdlib.h>
int main (int argc, char* argv[]) {
    int bytesToAllocate;
    char* bytesReserved = NULL;

    //assume you have code here that fills bytesToAllocate

    bytesReserved = malloc(bytesToAllocate);
    if (bytesReserved == NULL) {
        //an error occurred while reserving the memory - handle it here
    }

    //when the program ends:
    free(bytesReserved);

    return 0;
}

If you want more information, have a look at the man page ( man malloc in a linux shell). 如果您想了解更多信息,请查看手册页(linux shell中的man malloc )。 If you aren't on linux, have a look at the online man page . 如果您不在Linux上,请查看在线手册页

calloc() is what you want. calloc()就是你想要的。 It will reserve memory for your process and write zero's to it. 它将为您的进程保留内存并向其写入零。 This ensures that the memory is actually allocated for your process. 这可确保为您的进程实际分配内存。 If you malloc() a large portion of memory, the OS may be lazy about actually allocating memory for you, only actually allocating it when it is written to (which will never happen in this case). 如果malloc()占用了很大一部分内存,那么操作系统可能会为你实际分配内存而懒,只有在写入时才实际分配(在这种情况下永远不会发生)。

You will need: 你会需要:

  • malloc() to allocate however many bytes you need ( malloc(200000000) or malloc(20 * (1 << 20)) ). malloc()分配你需要的许多字节( malloc(200000000)malloc(20 * (1 << 20)) )。
  • getc() to wait for a keypress. getc()等待按键。
  • free() to deallocate the memory. free()释放内存。

The information on these pages should be helpful. 这些 页面 的信息应该会有所帮助。

Did this, should work. 这是否应该有效。 Although I was able to reserve more RAM than I have installed, this should work for valid values, tho. 虽然我能够保留比我安装的RAM更多的RAM,但这应该适用于有效值。

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

enum
{
   MULTIPLICATOR = 1024 * 1024 // 1 MB
};


int
main(int argc, char *argv[])
{
   void *reserve;
   unsigned int amount;

   if (argc < 2)
   {   
      fprintf(stderr, "usage: %s <megabytes>\n", argv[0]);
      return EXIT_FAILURE;
   }   

   amount = atoi(argv[1]);

   printf("About to reserve %ld MB (%ld Bytes) of RAM...\n", amount, amount * MULTIPLICATOR);

   reserve = calloc(amount * MULTIPLICATOR, 1);
   if (reserve == NULL)
   {   
      fprintf(stderr, "Couldn't allocate memory\n");
      return EXIT_FAILURE;
   }   

   printf("Allocated. Press any key to release the memory.\n");

   getchar();
   free(reserve);
   printf("Deallocated reserved memory\n");

   return EXIT_SUCCESS;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM