petersmith221
petersmith221

Reputation: 111

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?

Upvotes: 11

Views: 3347

Answers (5)

chmod222
chmod222

Reputation: 5722

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>\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;
}

Upvotes: 0

Matt J
Matt J

Reputation: 45193

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.

Upvotes: 0

jdizzle
jdizzle

Reputation: 4154

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

Upvotes: 1

user50049
user50049

Reputation:

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.

Upvotes: 19

a_m0d
a_m0d

Reputation: 12195

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.

Upvotes: 3

Related Questions