Hard
Calling malloc() from a signal handler
The main loop is in the middle of its own malloc()/free() call when SIGINT arrives and the handler below runs. What is the risk?
main.cpp
#include <csignal>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
void handler(int) {
char* buf = (char*)malloc(64);
write(1, buf, 14);
free(buf);
}
int main() {
signal(SIGINT, handler);
for (;;) {
void* p = malloc(32);
free(p);
}
}8 of 53 in Operating Systems