Skip to content

Instantly share code, notes, and snippets.

@krazylink
Forked from anonymous/gist:8811035
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krazylink/8811374 to your computer and use it in GitHub Desktop.
Save krazylink/8811374 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
long int *memory;
void signal_handler(int signo)
{
free (memory);
exit(0);
}
size_t getTotalSystemMemory()
{
long pages = sysconf(_SC_AVPHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
return pages * page_size;
}
int main()
{
size_t memTotal = getTotalSystemMemory();
printf("System Memory: %zu\n", memTotal);
int inc = 1024;
memory = malloc(inc);
int total = inc;
while(memory != NULL)
{
memset(memory, 0, inc);
memory = malloc(inc);
total += inc;
sleep(.1);
}
printf("Resoruce limit hit!\n");
printf("Allocated %i out of %lu\n", total, memTotal);
while(1)
{
if (signal(SIGINT, signal_handler) == SIG_ERR)
{
printf("Couldn't get signal.\n");
exit(1);
}
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment