fork进程释疑

 1 #include <unistd.h>;
 2 #include <sys/types.h>;
 3 
 4 main ()
 5 {
 6         pid_t pid;
 7         pid=fork();        // 这个地方开始分化为两个进程空间(所谓的一次调用,两个返回)
 8 
 9         if (pid < 0)
10                 printf("error in fork!");
11         else if (pid == 0)
12                 printf("i am the child process, my process id is %d\n",getpid());
13         else
14                 printf("i am the parent process, my process id is %d\n",getpid());
15 }

 

fork会拷贝父进程的三个要素进子进程

  • 程序本身;
  • 父进程进程相关联的全部数据(包括变量,内存空间,缓冲区等等);
  • 程序的执行上下文(execution context)。 

 

fork面试题: http://coolshell.cn/articles/7965.html

posted @ 2012-08-26 15:49  持幕  阅读(123)  评论(0编辑  收藏  举报