Some add-ons to cigar's answer
µÇ¼ | ÂÛ̳µ¼º½ -> »ªÐÂÏÊÊ -> Çóѧʨ³Ç | ±¾Ìû¹²ÓÐ 2 Â¥£¬·Ö 1 Ò³, µ±Ç°ÏÔʾµÚ 1 Ò³ : ±¾ÌûÊ÷ÐÎÁбí : ˢР: ·µ»ØÉÏÒ»Ò³
×÷ÕߣºFlying (µÈ¼¶£º18 - »ªÐÂË®³µ£¬·¢Ìû£º16849) ·¢±í£º2003-11-05 11:10:17¡¡ Â¥Ö÷¡¡ ¹Ø×¢´ËÌûÆÀ·Ö£º
¹ØÓÚCºÍUNIX£¨MORE QUESTION£©1)ÔÚCS2281µÄTEST2ÖУ¬ÓÐÕâÑùÒ»¸öÎÊÌ⣺ How many lines are printed by the following program? main(){ if(fork()||fork()&&fork()) printf("%d\n", getpid()); } ´ð°¸ÊÇ2¡£ ÄÄλ¸ßÊÖÄÜÖ¸½ÌÒ»ÏÂΪʲôÊÇ2Â𣿠ÎÒÔÚUNIXÏ·ֱðÊÔÑéÁË main(){ if(fork()||fork()) printf("%d\n", getpid()); } /*print 2 lines*/ ------------------------------ main(){ if(fork()&&fork()) printf("%d\n", getpid()); } /*print 1 lines*/ ------------------------------ main(){ if(fork()&&fork()&&fork()) printf("%d\n", getpid()); } /*print 1 lines*/ ------------------------------ main(){ if(fork()||fork()||fork()) printf("%d\n", getpid()); } /*print 3 lines*/ ÎÒÊÇÖ»ÖªÆäÈ»£¬²»ÖªÆäËùÒÔÈ»¡£Èç¹ûÄÄλÐǪ̈Äܽâ»óÒ»¶þ£¬ÔÚÏÂÕæµÄºÜ¸Ð¼¤£¬ÎÞÂÛ×îºóÄÃA»¹ÊÇÄÃD¡£ 2£©»¹ÓÐÒ»Ì⣺ What output valus are possible by the following program? main(){ if(!fork()) printf("%d\n", getpid()) } (A)>1 (B) >=1 (C)2 (D)3 (E)NONE OF THE ABOVE ´ð°¸ÊÇB£¬¿ÉÎÒÈÏΪÊÇA¡£ Õâ¸öÒ»¶¨ÊǸöCHILD PROCESS£¬Ë (more...)
Some add-ons to cigar's answer
1)
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 exclusively to hash here instead... (just joking :^)

¡°Hash¡±Ö±ÒëΪ¡°¹þÏ£¡±£¬²»¹ýÊÇÒ»¸öÎÞÓõÄÒôÒë¡£

¡°Hash¡±ÓÃÓÚÃû´Ê£¬Í¨³£Îª¡°hashº¯Êý¡±»ò¡°hashËã·¨¡±£¨¡°Hashº¯Êý¡±Ö»²»¹ýÊÇ¡°hashËã·¨¡±µÄÊýѧ±íÊö£©¡£hashÊÇÖ¸Ò»À൥Ïòº¯Êý£¬ÔÚ¼ÆËã»ú¿ÆѧÁìÓòÖжàÓÃÓÚÊý¾ÝÈÏÖ¤¡£hashµÄ²»¿ÉÄæÐÔ±£Ö¤Á˼´Ê¹¹«¿ª»ùÓÚij¸öÊäÈëµÄº¯ÊýÖµ£¬Ò²Ã»Óк÷½·¨¿ÉÒÔ»¹Ô­²¢ÕÒ³öÔ­À´µÄÊäÈëÖµ¡£

¡°Hash¡±ÓÃÓÚ¶¯´Ê£¬¿ÉÒëΪ¡°É¢ÁС±»ò¡°»ìÔÓ¡±¡£ÕâÊÇ»ùÓÚ¡°hash¡±Ò»´ÊµÄÓ¢ÎÄÔ­ÒâµÄ¡£ÔÚ¼ÆËã»ú¿ÆѧÖжàָͨ¹ý¶ÔһϵÁÐÊäÈë½øÐÐÔËË㣬¶ø°ÑËüÃÇ·ÖÉ¢µ½²»Í¬µÄ¼¯ºÏÀïÃæÈ¥¡£

4)
Indeed, you can do the following and verify it yourself:
cd /
ls -ld . ..

You shall see that both . and .. for the directory / are pointing to the same inode.
Flying @way …Ç·f•Ÿ
»¶Ó­À´µ½»ªÐÂÖÐÎÄÍø£¬Ó»Ô¾·¢ÌûÊÇÖ§³ÖÎÒÃǵÄ×îºÃ·½·¨!Ô­ÎÄ / ´«Í³°æ / WAP°æËùÓлظ´´ÓÕâÀïÕ¹¿ªÊÕÆðÁбí
×÷ÕߣºFlying (µÈ¼¶£º18 - »ªÐÂË®³µ£¬·¢Ìû£º16849) ·¢±í£º2003-11-05 18:09:41¡¡ 2Â¥
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.
Hmm...
As long as the compiler adheres to the standard, it should perform short-circuiting...
»¶Ó­À´µ½»ªÐÂÖÐÎÄÍø£¬Ó»Ô¾·¢ÌûÊÇÖ§³ÖÎÒÃǵÄ×îºÃ·½·¨!Ô­ÎÄ / ´«Í³°æ / WAP°æËùÓлظ´´ÓÕâÀïÕ¹¿ªÊÕÆðÁбí
ÂÛ̳µ¼º½ -> »ªÐÂÏÊÊ -> Çóѧʨ³Ç | ·µ»ØÉÏÒ»Ò³ | ±¾Ö÷Ìâ¹²ÓÐ 2 ƪÎÄÕ£¬·Ö 1 Ò³, µ±Ç°ÏÔʾµÚ 1 Ò³ | »Øµ½¶¥²¿
<<ʼҳ¡¡ [1]¡¡ Ä©Ò³>>

ÇëµÇ¼ºó»Ø¸´£ºÕʺŠ¡¡ ÃÜÂë ¡¡