Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2508695
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2011-05-05 18:13:21

    vfork并不完全复制父进程的数据段,而是和父进程共享数据段。调用vfork对于父子进程的执行次序有所限制,调用vfork时,父进程将被挂起,子进程运行至调用exec函数族或调用exit时解除这种状态。编写代码如下:
  1. #include <sys/types.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>

  4. int main(int argc, char *argv[])
  5. {
  6.   pid_t pid;
  7.   if((pid=vfork()) < 0){
  8.     printf("fork error!\n");
  9.     exit(1);
  10.   }
  11.   else if(pid == 0){
  12.     printf("child process is printing.\n");
  13.   }
  14.   else{
  15.     printf("parent process is printing.\n");
  16.   }
  17.   exit(0);
  18. }
执行结果如下:
peng@ubuntu:~/src/test/c/linuxc$ gcc 6.2.c
peng@ubuntu:~/src/test/c/linuxc$ ./a.out 
child process is printing.
parent process is printing.
peng@ubuntu:~/src/test/c/linuxc$ ./a.out 
child process is printing.
parent process is printing.

阅读(2906) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

kinfinger2011-05-07 00:22:32

pass by