I have to use fork()
recursively, but limit the number of forked processes (including children and descendants) to (for example) 100. Considering this code snippet:
void recursive(int n) {
for(int i=0; i<n; i++) {
if(number_of_processes() < 100) {
if(fork() == 0) {
number_of_processes_minus_one();
recursive(i);
exit(0);
}
}
else
recursive(i);
}
}
How to implement number_of_processes()
and number_of_processes_minus_one()
? Do I have to use IPC? I tried to pre-create a file, write PROC_MAX
into it and lock-read-write-unlock it in number_of_processes()
but it still eat all my pids.