In a small script I have, I'm starting a shell and then setting RLIMIT_NPROC on it to some small number, like 5, using prlimit
Then, I try to execute a shell script that only contains:
#!/bin/bash
echo hi
sleep 3
by sending "./test.sh\n" through stdin and waiting for the result. At the same time, I'm using ps --ppid <somepid> -o pid=
to list the child PIDs of the first.
This always fails with the error bash: fork: retry: No child processes
. I've tried setting the limit to RLIM_INFINITY to see how many child processes are created, but only one is listed (naturally the shell processing test.sh).
How can I properly limit the number of child processes (or more generally, restrict some sort of fork bomb)? I know about cgroups and other similar alternatives, but I'm trying to write this to learn, and this behavior really confuses me.
I think it might be due to RLIMIT_NPROC's documentation stating that it limits "The maximum number of processes (or, more precisely on Linux, threads)" and large numbers of threads being created, but the program only works once I increase RLIMIT_NPROC up to 500 or so, and that doesn't seem very reasonable.
If this behavior is unexpected, it might be a coding error, in which case I'll try to clean up the testing code to post on here.