免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1389 | 回复: 5
打印 上一主题 下一主题

看apue的一个小问题! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-30 13:26 |只看该作者 |倒序浏览
关于exit()和_exit()的问题


  1. #include        <sys/types.h>
  2. #include        "ourhdr.h"

  3. int                glob = 6;                /* external variable in initialized data */

  4. int
  5. main(void)
  6. {
  7.         int                var;                /* automatic variable on the stack */
  8.         pid_t        pid;

  9.         var = 88;
  10.         printf("before vfork\n");        /* we don't flush stdio */

  11.         if ( (pid = vfork()) < 0)
  12.                 err_sys("vfork error");
  13.         else if (pid == 0) {                /* child */
  14.                 glob++;                                        /* modify parent's variables */
  15.                 var++;
  16.                 _exit(0);                                /* child terminates */
  17.                //exit(0); 修给这里没有变化!!!
  18.         }

  19.         /* parent */
  20.         printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
  21.         exit(0);
  22. }
复制代码


调用_exit()的时候结果是:
before vfork
pid = 9343, glob = 7, var = 89
调用exit()的时候结果是:
before vfork
pid = 9352, glob = 7, var = 89
这是怎么回事呢??

论坛徽章:
0
2 [报告]
发表于 2007-03-30 13:31 |只看该作者
os不保证每次的pid一样

论坛徽章:
0
3 [报告]
发表于 2007-03-30 13:42 |只看该作者

回复 2楼 Edengundam 的帖子

我指的不是pid 而是输出部分。书上的讲的是:
调用_exit()
before vfork
pid = 9343, glob = 7, var = 89
而调用exit()的时候,由于清理了I/O缓冲区
结果只是
before vfork

论坛徽章:
0
4 [报告]
发表于 2007-03-30 13:48 |只看该作者
APUE写得有早。

现在的_exit到底能不能刷新缓存,跟系统具体实现相关的。

你可以man _exit一下。

论坛徽章:
0
5 [报告]
发表于 2007-03-30 13:50 |只看该作者
原帖由 samuel1004 于 2007-3-30 13:42 发表
我指的不是pid 而是输出部分。书上的讲的是:
调用_exit()
before vfork
pid = 9343, glob = 7, var = 89
而调用exit()的时候,由于清理了I/O缓冲区
结果只是
before vfork




Note in Figure 8.3 that we call _exit instead of exit. As we described in Section 7.3, _exit does not perform any flushing of standard I/O buffers. If we call exit instead, the results are indeterminate. Depending on the implementation of the standard I/O library, we might see no difference in the output, or we might find that the output from the parent's printf has disappeared.

不用翻译了吧

论坛徽章:
0
6 [报告]
发表于 2007-03-30 14:10 |只看该作者

回复 5楼 Edengundam 的帖子

谢谢,明白了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP