中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > C/C++
一个关于进程的问题
作者:未知 时间:2005-09-13 19:18 出处:ChinaUnix.net 责编:chinaitpower
              摘要:一个关于进程的问题

#include <unistd.h>
#include <sys/types.h>
int main(void)
{
int var= 1;
pid_t pid;
printf("before vfork\n";
if((pid=vfork())<0)
{
perror("fork";
exit(1);
}
else if (pid==0)
{
printf("child\n";
var++;
exit(0);
}

printf("var = %d \n",var);
exit(0);
}
运行结果是
before vfork
child
var = 2 
为什么是这样的,子进程处调用了exit(0),不是就返回到shell了吗?
应该不会输出var=2。


 jacamar 回复于:2002-11-05 23:32:26
fork后就是两个进程了,就像没有关系一样

 zhutr 回复于:2002-11-05 23:34:30
只是子进程退出,
父进程并没有退出呀!
当然要打出来了!

 beggar 回复于:2002-11-05 23:49:05
vfork和fork不同
vfork在调用exec或exit之前在父进程空间中运行且保证子进程先运行,如果子进程不调用exec或exit父进程永远得不到运行.你用vfork的子进程更改了父进程空间中的的var值,所以你会看到var值为2而不是你想要的1.用fork就能看到你想要的1

 JohnBull 回复于:2002-11-06 12:12:15
同意.

(From  XPG4  / SUSv2 / POSIX draft.)  The vfork() function has the same effect as fork(), except that  the  behaviour is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store  the  return value from vfork(), or returns from the
function in which vfork() was called, or calls  any  other function before successfully calling _exit() or one of the exec family of functions.


 aiya 回复于:2002-11-06 15:13:34
有道理!

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有