|
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
main(int argc,char** argv)
{
int value=0;
int childpid;
fprintf(stderr,"The process before creating pid=%d\n",getpid());
if((childpid=fork())==0){
fprintf(stderr,"the child process pid=%d\n",getpid());
fprintf(stderr,"the initial value=%d\n",value);
value=1;
fprintf(stderr,"the value is %d after being changed by the child process\n",value);
}else if(childpid>0){
fprintf(stderr,"the parent process is %d\n",getpid());
fprintf(stderr,"the value in parent process is %d\n",value);
}else
fprintf(stderr,"the fork system call fail");
}
程序的输出结果为
[xiaosuo@Center net]$ ./fock.out
The process before creating pid=2361
the child process pid=2362
the initial value=0
the value is 1 after being changed by the child process
the parent process is 2361
the value in parent process is 0
按照c语言来分析
下面else的代码应该不执行
可是它执行了
请问这是为什么?
是不是和编译或者是fork函数有关? |
|