Skip to content

Instantly share code, notes, and snippets.

@bmorphism
Created August 19, 2023 08:07
Show Gist options
  • Save bmorphism/ba641802f353e91d8dd136c2645adb0d to your computer and use it in GitHub Desktop.
Save bmorphism/ba641802f353e91d8dd136c2645adb0d to your computer and use it in GitHub Desktop.
wat.c
#include <thread>
#include <vector>
#include <sys/mman.h>
constexpr size_t kPageSize = 65536;
constexpr size_t k4G = 0x100000000;
void allocate_memory() {
// Attempt to allocate 4G of memory repeatedly
while (true) {
void* addr = mmap(nullptr, k4G, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (addr == MAP_FAILED) {
// Handle error
return;
}
}
}
int main() {
// Launch multiple threads that all attempt to allocate 4G of memory
std::vector<std::thread> threads;
for (int i = 0; i < 10; ++i) {
threads.emplace_back(allocate_memory);
}
for (auto& thread : threads) {
thread.join();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment