标签:style io ar sp for 数据 on 问题 代码
/*我们知道一个程序有代码段、数据段和堆栈段,代码段被父子进程贡献,那么数据段和堆栈段呢?我们来看下面的程序:*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
static int count = 0;
int main(int argc , char **argv)
{
 int id;
 id=fork();
 if(id<0)
 {
  printf("fork error\n");
 }
 else if(id==0)
  {
   count++;
   printf("I‘m in child process\n");
   printf("child count=%d\n",count);
  }
 else
  {
   count++;
   printf("I‘m in parent process\n");
   printf("parent count=%d\n",count);
  }
 return 0;
}
/*结果是:
I‘m in child process
child count=1
I‘m in parent process
parent count=1
很显然,如果数据段是共享的话,那么两次打印的count的值肯定有一个为2,现在都是1就说明数据段是独立的,同样其实堆栈段也是独立的!fork()函数会将父进程的数据段和代码段拷贝过来,作为子进程独立的数据段和代码段!*/
标签:style io ar sp for 数据 on 问题 代码
原文地址:http://www.cnblogs.com/leijiangtao/p/4110949.html