>>
µÇ¼ | ÂÛ̳µ¼º½ -> »ªÐÂÏÊÊ -> Çóѧʨ³Ç | ±¾Ìû¹²ÓÐ 2 Â¥£¬·Ö 1 Ò³, µ±Ç°ÏÔʾµÚ 1 Ò³ : ±¾ÌûÊ÷ÐÎÁбí : ˢР: ·µ»ØÉÏÒ»Ò³
×÷Õߣºcigar (µÈ¼¶£º2 - ³õ³ö鮣¬·¢Ìû£º296) ·¢±í£º2003-11-05 07:47:05¡¡ Â¥Ö÷¡¡ ¹Ø×¢´ËÌûÆÀ·Ö£º
¹ØÓÚ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...)
>>
1.

The parent process P creates a child process P1, then print a line. The return value of the (1st) fock() for process P1 is 0,
so the second part of OR (||) is executed. As a result, P1 creates a new process P2. For P1, (fork()||fork()) is true, so the second part of AND(&&) should be executed: a new process P3 is created. For p1 the if condition is true, so p1 prints a line. For p2, fork()||fork() is false, so the if condition is false(note the 3rd fork() will not be executed), p2 doesn't execute printf statement. For p3, the 3rd fork() return 0, so the if condition is false. It skips printf statement. please correct me if i am wrong.

2.

I also choose A. PID 1 : init, is the parent process of all other processes in unix.


4.

For root directory, both . and .. refer to itself. so they have the same inode number. Note the parent of root is itself.


If i am wrong, Please correct me
Put your OWN COOL signature here!
»¶Ó­À´µ½»ªÐÂÖÐÎÄÍø£¬Ó»Ô¾·¢ÌûÊÇÖ§³ÖÎÒÃǵÄ×îºÃ·½·¨!Ô­ÎÄ / ´«Í³°æ / WAP°æËùÓлظ´´ÓÕâÀïÕ¹¿ªÊÕÆðÁбí
×÷Õߣºcigar (µÈ¼¶£º2 - ³õ³ö鮣¬·¢Ìû£º296) ·¢±í£º2003-11-05 14:36:21¡¡ 2Â¥
>>1. The parent process P creates a child process P1, then print a line. The return value of the (1st) fock() for process P1 is 0, so the second part of OR (||) is executed. As a result, P1 creates a new process P2. For P1, (fork()||fork()) is true, so the second part of AND(&&) should be executed: a new process P3 is created. For p1 the if condition is true, so p1 prints a line. For p2, fork()||fork() is false, so the if condition is false(note the 3rd fork() will not be executed), p2 doesn't execute printf statement. For p3, the 3rd fork() return 0, so the if condition is false. It skips printf statement. please correct me if i am wrong. 2. I also choose A. PID 1 : init, is the parent process of all other processes in unix. 4. For root directory, both . and .. refer to itself. so they have the same inode number. Note the parent of root is itself. If i am wrong, Please correct me
I made a mistake, i thought && and || were in
the same precedence level
»¶Ó­À´µ½»ªÐÂÖÐÎÄÍø£¬Ó»Ô¾·¢ÌûÊÇÖ§³ÖÎÒÃǵÄ×îºÃ·½·¨!Ô­ÎÄ / ´«Í³°æ / WAP°æËùÓлظ´´ÓÕâÀïÕ¹¿ªÊÕÆðÁбí
ÂÛ̳µ¼º½ -> »ªÐÂÏÊÊ -> Çóѧʨ³Ç | ·µ»ØÉÏÒ»Ò³ | ±¾Ö÷Ìâ¹²ÓÐ 2 ƪÎÄÕ£¬·Ö 1 Ò³, µ±Ç°ÏÔʾµÚ 1 Ò³ | »Øµ½¶¥²¿
<<ʼҳ¡¡ [1]¡¡ Ä©Ò³>>

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