Notice that if (fork()) /*etc*/
is exactly the same as
pid_t newtmp = fork();
if (newtmp != 0) /*etc*/
where newtmp
is a fresh (new) variable name not occurring in your program (you could use x1
, x2
etc.... provided it has no occurrence in your program), and pid_t
is some integral type (probably int
).
Once you rewrote your code with explicit and unique names given to result of fork
you'll understand it better.
BTW, the code is poor taste. When you use fork
you need to handle three cases:
the fork
failed (e.g. because your system has not enough memory, or because you exceeded some limits) and gives -1
the fork
succeeded in the child so gives 0
the fork
succeeded in the parent, so gives the pid of the child.
But when you code if (fork())
you are forgetting -or handling incorrectly- the first case (failure). It can rarely happen.
Read carefully (and several times) the fork(2) man page. Notice that fork is difficult to understand.
Regarding limits, be aware of setrlimit(2) (and the ulimit
bash builtin).