>>11
It's because when you call
fork(), the OS copies the process to a new part of memory* and then starts it at the instruction immediately following the call to fork(). So, when you call fork() in your example, the child process starts at
cnt++;, goes back to re-evaluate the loop (
i < 10) and then does another loop. Putting
break fixes it because
fork() returns zero to the child process (hence the
if (!fork())), but it returns the PID of the child to the parent process. So, in the child process,
if (!fork()) evaluates to true, so it executes the
break instruction and exits the loop. In the parent process,
if (!fork()) evaluates to false, so it continues in the loop.
* Actually, on most UNIXes, the memory is shared, and the kernel doesn't copy anything until one of the processes tries to write to the shared memory, which is called copy-on-write. You don't need to worry about it, though, because it's completely transparent.