Some add-ons to cigar's answer1)
You just need four key points for this question:
a) Fork() returns a positive PID of the child process spawned in the parent process, 0 in the child process, and negative value in parent process if it fails;
b) In C, anything non-zero is considered logical true;
c) && takes precedence over || in C, that is (fork() || fork() && fork()) == (fork() || (fork() && fork()));
d) Logical expressions in C are short-circuited.
So the program can be re-writed as:
if(fork())
{
printf("%d\n", getpid());
}
else
{
if (fork())
{
if (fork())
{
printf("%d\n", getpid());
}
}
You can try to trace the program through its parent-child hierarchy, and you should be able to get the answer.
2)
In general, I agree with you that it is A. But then, there is one point to note: the assignment of PID's is dependent on the underlying implementation's policy. Therefore, if one implementation choose to assign PID's the other way round, you can still get an ouput of "1."
3)
You should put this question exclu (more...)
One point:
Logical expression will probably be short-circuited, depends on the platform, OS, compiler as well as optimization level set for the compiler. But for most compilers, to generate less machine code, the logical expressions are short-circuited.
Yeah!