English 中文(简体)
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. the any key] is pressed on a Linux 2.6 32 bit system.

*
/.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.

Any ideas on how to do this?

问题回答

You want to use malloc() to do this. 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.
  2. Prevent the memory from being paged out (swapped), the mlock() / mlockall() functions can help you with this.
  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()).

In most realities, malloc() and memset() (or, calloc() which effectively does the same) will suit your needs.

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? That will reserve that RAM for you, and then you are free to do whatever you wish with it.

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). If you aren t on linux, have a look at the online man page.

calloc() is what you want. 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).

You will need:

  • malloc() to allocate however many bytes you need (malloc(200000000) or malloc(20 * (1 << 20))).
  • getc() to wait for a keypress.
  • free() to deallocate the memory.

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.

#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>
", argv[0]);
      return EXIT_FAILURE;
   }   

   amount = atoi(argv[1]);

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

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

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

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

   return EXIT_SUCCESS;
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签