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