【问题标题】:different output with exit() and _exit()?exit() 和 _exit() 的输出不同?
【发布时间】:2014-10-03 01:29:15
【问题描述】:

为什么它显示不同的输出???谁能深入解释一下。

1.

#include <stdlib.h>
 #include <stdio.h>

int main (void)
{
printf ("Using exit ... \ n");
printf ("This is the content in buffer");
exit (0);
}

输出:

使用退出 ... 这是缓冲区中的内容

2.

# Include <unistd.h>
# Include <stdio.h>

int main (void)
{
printf ("Using exit ... \ n");
printf ("This is the content in buffer");
_exit (0);
}

仅输出:

使用退出...

【问题讨论】:

标签: c unix exit


【解决方案1】:

如果我们阅读 _exit() 的文档,我们会注意到:

在没有完全清理资源的情况下导致正常程序终止。

这大概包括刷新标准输出。

【讨论】:

    【解决方案2】:

    首先你应该知道的是 STDOUT 是行缓冲的,这意味着它在获得 '\n' 后会刷新内存。第二件事是 exit() 会刷新 stdio 缓冲区,而 _exit() 不会。

    现在在您的情况下,在第一个示例中,exit() 会刷新 stdio 缓冲区,因此它会打印两个 printf 输出,而在 _exit() 中不会发生刷新,因此它不会打印两个 printf 语句。

    如果您想在第二条语句中获得正确的输出,请禁用缓冲 通过放

    int main (void)
    {
    setbuf(stdout, NULL);
    printf ("Using exit ... \ n");
    printf ("This is the content in buffer");
    _exit (0);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-20
      • 1970-01-01
      • 2011-07-22
      • 1970-01-01
      • 2011-06-01
      • 2016-07-02
      • 2013-04-28
      • 1970-01-01
      • 2021-10-01
      相关资源
      最近更新 更多