#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <assert.h>
#include <sys/wait.h>
#include <sys/mman.h>

char* p[110000];

int main()
{
  for (size_t i = 0; i < 110000; ++i)
  {
    // void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset);
    p[i] = mmap(0,1024ull*1024ull*1024ull,PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,-1,0);
    printf("%zu GB allocated, got pointer %p now\n",i,p[i]);
    if (p[i] == 0)
      goto error;
  }
  
  puts("let's fill the memory!\n");

  for (size_t i = 0; i < 5; ++i)
  {
    for (size_t j = 0; j < 1024ull*1024ull*1024ull; j += 128)
      p[i][j] = 37 * j;

    printf("%zu GB filled\n",i);
  }

  success:
  puts("now the system will clean up\n");
  return 0;

  error:
  puts("malloc returned 0 so we are out of memory now!\n");
  return -1;
}