-
-
Save krazylink/8811374 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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