LinuxSir.cn,穿越时空的Linuxsir!

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

vfork子进程调用exit后的退出问题??

[复制链接]
发表于 2005-12-12 21:33:30 | 显示全部楼层 |阅读模式
代码:

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int glob = 6;

int main()
{
int var;
pid_t pid;

var = 88;

printf("before vfork\n");

if((pid = vfork()) < 0){
printf("vfork error\n"0;
}else if(pid == 0){
glob++;
var++;
_exit(0); //问题?? 换成exit(0),输出??
}


printf("pid = %d, glob = %d, var = %d\n, getpid(), glob, var);

exit(EXIT_SUCCESS);
}

就是unix环境编程的问题,书上说如果把_exit(0), 换作exit(0),将会只输出:
before vfork
原因是子进程调用了exit,它刷新关闭了所有的标准I/0流,所以父进程调用printf时,输出流已关闭,于是printf返回-1

可我运行不是,printf照样输出,返回值是32,请问各位是什么原因呢??
发表于 2005-12-13 08:55:57 | 显示全部楼层
manpage

  1. vfork()  differs  from  fork  in that the parent is suspended until the
  2.        child makes a call to execve(2) or _exit(2).  The child shares all mem-
  3.        ory  with  its parent, including the stack, until execve() is issued by
  4.        the child.  The child must not return from the current function or call
  5.        exit(), but may call _exit().
复制代码

  1. In Linux, it
  2.        has been equivalent to fork() until 2.2.0-pre6 or so. Since  2.2.0-pre9
  3.        (on  i386,  somewhat later on other architectures) it is an independent
  4.        system call. Support was added in glibc 2.0.112.
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-12-13 13:38:14 | 显示全部楼层
to haohao_t:

不是很明白你的意识, 能否解释子进程调用exit并没有关闭父进程的stdout的原因?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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