Re: the different effect of system call fork()

From: Nick Andrew
Date: Thu Apr 09 2009 - 08:38:18 EST


On Wed, Apr 08, 2009 at 03:08:48PM +0200, Bernd Petrovitsch wrote:
> > if (pid_t pid = fork() < 0)
>
> I don't think that this line does what you want it to do.
> Hint: Using a "pid_t" for the boolean result for a comparison seems
> strange at best.

The "problem" is due to a misunderstanding of C/C++ operator precedence.

"(pid = fork() < 0)" binds like "pid = (fork() < 0)" and so pid always
has a value of zero and so both parent and child process run the "first
level child process" code ... soon there are 4 processes all confused
about who they are.

Nick.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/