0

So I have a recursive function that is going to create multiple processes and write an integer from parent process to child process.

Everything works great when numbers are small like several hundreds/thousands.

But when numbers are large the code would always be stuck before the write statement. I have included an error checking for the statement but no perror was printed.

Does anyone know what's the possible cause for this?

Pig
  • 2,002
  • 5
  • 26
  • 42
  • Don't create more than a few dozens of simultaneous child processes. `fork`-ing a thousand of them is almost certainly wrong. Also, check *every* syscall (including `fork` and `pipe` and `write` and `read` ...). At last, compile with all warnings and debug info (`gcc -Wall -g`) and use a debugger (`gdb`) – Basile Starynkevitch Mar 11 '14 at 09:27
  • @BasileStarynkevitch this is a homework and the algorithm provided asks us to do it this way .. – Pig Mar 11 '14 at 09:28

1 Answers1

0

When you write(2) to a pipe(7) the writing can be blocked if the process on the other end of the pipe did not read from it (and the pipe is full).

You could use a multiplexing syscall like poll(2) in your event loop. See also this answer.

But you should avoid creating more than a few dozens of simultaneous child processes. See also setrlimit(2) with RLIMIT_NPROC (and the bash ulimit builtin).

Read also Advanced Linux Programming.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547