LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 764|回复: 5

请大家帮忙分析一下一下的代码

[复制链接]
发表于 2003-10-27 12:31:31 | 显示全部楼层 |阅读模式
#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函数有关?
发表于 2003-10-27 16:58:21 | 显示全部楼层
看看fork函数的功能吧
发表于 2003-10-27 17:45:05 | 显示全部楼层
这是最基本的多进程建立,先看看操作系统理论。
发表于 2003-10-30 22:17:54 | 显示全部楼层
父子进程同时执行,你看到的上面的输出是子进程的输出,而你所说的ELSE后面是由于父进程执行,而产生的输出。
 楼主| 发表于 2003-10-31 01:19:19 | 显示全部楼层
谢谢你老兄
今天我认真的看了一下fork函数终于明白了
现在我正在学习linux网络编程
发表于 2003-11-1 10:29:59 | 显示全部楼层
我也是呀,有时间多交流交流。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表