The code below will spawn as many children as possible. Themselves won't fork further, and will become zombies once the parent process exits.
How many children processes will the parent process spawn ?
int main(int argc, char *arg[])
{
while(fork() > 0);
}
Update -- I was perhaps to fast, didn't see that there is no fork loop. Then it's probably depending on how expensive it is to fork on that machine. The zombies may also use system resources which will at some point be exhausted. And the ulimit command mentioned below is of course still valid.--
Update 2: I see this in some copy of /linux/kernel/fork.c which should keep a machine usable (max_threads apparently limits the number of processes as well since each process has at least one thread):
-- Original answer:
It will create as many processes as are physically possible (that is, it will quickly freeze the machine -- I have done that), or as many as are allowed for the current user or shell, if such a limit is imposed. In bash one can impose a limit through the built-in shell command
ulimit -u <number>
. Note that a process probably doesn't have to be started through bash (perhaps a cron job won't).The number of child processes can be limited with setrlimit(2) using
RLIMIT_NPROC
. Notice that fork(2) can fail for several reasons. You could usebash
builtinulimit
to set that limit.You can use
getrlimit
(or parse/proc/self/limits
, see proc(5)) to get that information.System-wide, you might use
/proc/sys/kernel/threads-max
since:There is also
/proc/sys/kernel/pid_max
However, there could be other limitations (notably swap space).
A task for the kernel is either a single-threaded process or some thread inside some process - e.g. created by low-level syscall clone(2) (or some kernel thread like
kworker
,ksoftirqd
etc...).BTW, the practical number of processes is much more limited by available resources. A typical Linux desktop has only a few hundreds of them (right now, my Debian/x86-64 desktop with 32Gb RAM & i5-4690S has 227 processes). So a process is a quite expensive resource (it needs RAM, it needs CPU...). If you have too many of them you'll experience thrashing. And in practice, you don't want to have too many runnable processes or schedulable tasks (probably only a few dozens of them at most, perhaps no more than a few per core).