Reputation: 461
I have a program where the parent process attempts to allocate 45 GB of memory using malloc on a 4 TB server. After successful allocation, the parent forks 100 child processes. Since fork uses copy-on-write, I expected the program to run successfully without duplicating the memory.
However, the program fails to run with the error:
Failed to fork 0 (total of 0GB allocated): Cannot allocate memory
Why is this happening despite copy-on-write being used, do i need to enable any setting at the OS to enable copy on write ? Any insights are appreciated!
I was expected that total 45G will be shared with all the forked processes as processes are not doing any operation on "mem" array.
OS: RockyLinux 8.10
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#define GB 45
#define FORKS 100
void main() {
unsigned char *mem[GB];
int i;
for (i = 0; i < GB; i++) {
mem[i] = malloc(1024*1024*1024);
if (mem[i] == NULL) {
printf("Failed to alloc %d: %s\n", i, strerror(errno));
return;
}
}
int pids[FORKS];
for (i =0; i < FORKS; i++) {
int pid = fork();
if (pid == 0) {
sleep(20);
exit(0);
}
if (pid == -1) {
printf("Failed to fork %d (total of %dGB allocated): %s\n", i, i*GB, strerror(errno));
for (int j = 0; j < pid; j++) {
kill(pids[j], SIGTERM);
}
return;
}
pids[i] = pid;
}
for (int j = 0; j < FORKS; j++) {
kill(pids[j], SIGTERM);
}
printf("Success\n");
}
Upvotes: 0
Views: 23