关于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,所以我觉得应该至少大于1。
不知高手如何看?
3)关于HASH(这个是CS3235 COMPUTER SECURITY)的问题
对于HASH,在CS3223(DATABSE MANAGEMENT)和其他一些MODULE当中都有MENTION过,但本人愚笨,直到现在还不知道HASH的确切含义。
这个东东好象比较广泛的用到,今天问了TUTOR,他说HASH是“简要”的意思(他用中文说的),好象就是MAIN POINTS。可我实在是很BLUR,不怕各位笑话,把我的想法都写在上面了,呵呵。
如果哪位能告知一点,我真的感激的不知道说啥了,呵呵。
(回答问题3可以尽量用中文吗)
4)In any unix directory, the names dot and dot dot have different inode numbers because dot is this directory and dot dot is parent directory.
(a)true (b)false
我选的是A,答案是B。高手,救救我吧!!!!
好了,先问这么多,明天继续问哈。
^_^
[一屁致命 (11-5 2:45, Long long ago)]
[ 传统版 |
sForum ][登录后回复]1楼
>>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[cigar (11-5 7:47, Long long ago)]
[ 传统版 |
sForum ][登录后回复]2楼
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 exclusively to hash here instead... (just joking :^)
“Hash”直译为“哈希”,不过是一个无用的音译。
“Hash”用于名词,通常为“hash函数”或“hash算法”(“Hash函数”只不过是“hash算法”的数学表述)。hash是指一类单向函数,在计算机科学领域中多用于数据认证。hash的不可逆性保证了即使公开基于某个输入的函数值,也没有好方法可以还原并找出原来的输入值。
“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 (11-5 11:10, Long long ago)]
[ 传统版 |
sForum ][登录后回复]3楼
(引用 cigar:>>1. The parent process P creates a child process P1, then print a line. The return value of the (1st) fock() for process P1 i...)I made a mistake, i thought && and || were inthe same precedence level[cigar (11-5 14:36, Long long ago)] [ 传统版 | sForum ][登录后回复]4楼
(引用 Flying: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 ...)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.[香陵居士 (11-5 16:55, Long long ago)] [ 传统版 | sForum ][登录后回复]5楼
(引用 cigar:I made a mistake, i thought && and || were inthe same precedence level)这么早起来灌水,佩服。。[朝令夕改 (11-5 17:58, Long long ago)] [ 传统版 | sForum ][登录后回复]6楼
(引用 香陵居士:One point:Logical expression will probably be short-circuited, depends on the platform, OS, compiler as well as optimization lev...)Hmm...As long as the compiler adheres to the standard, it should perform short-circuiting...[Flying (11-5 18:09, Long long ago)] [ 传统版 | sForum ][登录后回复]7楼