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 | |
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 | |
m*****e 发帖数: 4193 | |