One point:
登录 | 论坛导航 -> 华新鲜事 -> 求学狮城 | 本帖共有 1 楼,分 1 页, 当前显示第 1 页 : 本帖树形列表 : 刷新 : 返回上一页
<<始页  [1]  末页>>
作者:香陵居士 (等级:16 - 好恐怖呀,发帖:22662) 发表:2003-11-05 16:55:18  楼主  关注此帖
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.
最新推出专栏《倾听索罗斯》 欢迎大家前来捧场!

Yeah!
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
论坛导航 -> 华新鲜事 -> 求学狮城 | 返回上一页 | 本主题共有 1 篇文章,分 1 页, 当前显示第 1 页 | 回到顶部
<<始页  [1]  末页>>

请登录后回复:帐号   密码