关于C和UNIX(MORE QUESTION)
登录 | 论坛导航 -> 华新鲜事 -> 求学狮城 | 本帖共有 7 楼,分 1 页, 当前显示第 1 页 : 本帖树形列表 : 刷新 : 返回上一页
<<始页  [1]  末页>>
作者:一屁致命 (等级:2 - 初出茅庐,发帖:54) 发表:2003-11-05 02:45:52  楼主  关注此帖
关于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。高手,救救我吧!!!!



好了,先问这么多,明天继续问哈。

^_^















Put your OWN COOL signature here!
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:cigar (等级:2 - 初出茅庐,发帖:296) 发表:2003-11-05 07:47:05  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
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:Flying (等级:18 - 华新水车,发帖:16849) 发表:2003-11-05 11:10:17  3楼 评分:
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的不可逆性保证了即使公开基于某个输入的函数值,也没有好方法可以还原并找出原来的输入值。

“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.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:cigar (等级:2 - 初出茅庐,发帖:296) 发表:2003-11-05 14:36:21  4楼
>>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版只看此人从这里展开收起列表
作者:香陵居士 (等级:16 - 好恐怖呀,发帖:22662) 发表:2003-11-05 16:55:18  5楼
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 exclu (more...)
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.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:朝令夕改 (等级:2 - 初出茅庐,发帖:82) 发表:2003-11-05 17:58:07  6楼
I made a mistake, i thought && and || were inthe same precedence level
这么早起来灌水,佩服。。
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:Flying (等级:18 - 华新水车,发帖:16849) 发表:2003-11-05 18:09:41  7楼
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版只看此人从这里展开收起列表
论坛导航 -> 华新鲜事 -> 求学狮城 | 返回上一页 | 本主题共有 7 篇文章,分 1 页, 当前显示第 1 页 | 回到顶部
<<始页  [1]  末页>>

请登录后回复:帐号   密码