由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - fork(): why both if and else are executed?
相关主题
再问个fork的题 (转载)a C++ interview question..........
Linux GNU C, readlink问题一个关于Insight debugger的问题
大家看看这道C语言题是怎么回事?[合集] Intel 9编译器在vc 6.0的环境里编译openmp的问题
问个问题,关于gdb的请教如何修正这个C程序的bug。
问问跟Linux Kernel Programming 有关的问题C 和 C++ 的区别
one C programming question - Thanks.简单的c code问题
VS C++项目的Reference是干什么的?请教一道题 (转载)
GO似乎是目前最难反编译的代码了关于 c malloc的一个问题
相关话题的讨论汇总
话题: process话题: pid话题: fork话题: getpid话题: else
进入Programming版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
#include
#include
#include
main ()
{
pid_t pid;
pid=fork();
if (pid < 0)
printf("error in fork!\n");
else if (pid == 0)
printf("i am the child process, my process id is %d\n",getpid());
else
printf("i am the parent process, my process id is %d\n",getpid());
}
output:
i am the child process, my process id is 9832
i am the parent process, my process id is 9832
r*********r
发帖数: 3195
2
oh, boy.
after fork(), you have two processes !
one executes the "if" branch, and the other executes the "else" branch.
c**********e
发帖数: 2007
3
Then why the two processes have the same id?
Could you detail your answer?

【在 r*********r 的大作中提到】
: oh, boy.
: after fork(), you have two processes !
: one executes the "if" branch, and the other executes the "else" branch.

r*********r
发帖数: 3195
4
yes, normally the two getpid() should return different results.
what environment are you running the program?
c**********e
发帖数: 2007
5
CC on unix.

【在 r*********r 的大作中提到】
: yes, normally the two getpid() should return different results.
: what environment are you running the program?

r*********r
发帖数: 3195
6
faulty system. time to get a new one.
t****t
发帖数: 6806
7
说错了.
t****t
发帖数: 6806
8
agree. i think the compiler tried to optimize getpid() call into common
subexpression and called it only once and saved the result.

【在 r*********r 的大作中提到】
: faulty system. time to get a new one.
c**********e
发帖数: 2007
9
Good idea. But the university is not willing to update their system now.
f*******y
发帖数: 988
10
这个很难想象,真是这样的话这个compiler得多烂呀

【在 t****t 的大作中提到】
: agree. i think the compiler tried to optimize getpid() call into common
: subexpression and called it only once and saved the result.

t****t
发帖数: 6806
11
not related。

will
m*****e
发帖数: 4193
12
What system is this?
1 (共1页)
进入Programming版参与讨论
相关主题
关于 c malloc的一个问题问问跟Linux Kernel Programming 有关的问题
关于在C中定义常量one C programming question - Thanks.
求助 怎么编辑 多个 .c files(比如a.c, b.c) 和一个.h file(ab (转载)VS C++项目的Reference是干什么的?
问一个简单的问题:fork(), execvp(), 怎么传递函数GO似乎是目前最难反编译的代码了
再问个fork的题 (转载)a C++ interview question..........
Linux GNU C, readlink问题一个关于Insight debugger的问题
大家看看这道C语言题是怎么回事?[合集] Intel 9编译器在vc 6.0的环境里编译openmp的问题
问个问题,关于gdb的请教如何修正这个C程序的bug。
相关话题的讨论汇总
话题: process话题: pid话题: fork话题: getpid话题: else