¹ØÓÚ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
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