关于stderr stdout linux windows 平台下的差异 以及exit与_exit

来源:互联网 发布:韩顺平js完整视频教程 编辑:程序博客网 时间:2024/05/21 09:05

  今天在看apue的时候,发现了有点忘记的函数"setbuf",man了一下是用来设置缓冲区的,设置为NULL。就会不带缓冲了。

   这个应该没什么争议好玩的就是想到了,试一试win下面的print函数发现它像是没有缓冲,执行下一个语句就直接刷新了。因为并不是基础讲解所以也不详细讲解stdout stderr做什么的了。

</pre><pre name="code" class="cpp">#include<stdio.h>int main(void){    printf("xiaochen");    sleep(10);   exit(0);} windows下是直接输出了xiaochen 然后才休眠10秒。 在linux下则完全不同. 先休眠等程序退出exit执行时才输出xiaochen。 stderr则是在linux 和windows都没带缓冲。     接下来我们看看apue上写的ISO C 对缓冲的要求是:(p117)1.当且仅当标准输入输出 指向交互式设备是,他们才是全缓冲的2.标准错误绝不会是全缓冲的3.标准错误是不带缓冲的*4若是指向终端设备的流,则是行缓冲的;否则是全缓冲的。总结一下linux下面  stderr不带缓冲  stdout带行缓冲   centos6.5windows  stderr不带缓冲 stdout给我的感觉不带   vs2010 win10环境   但是查阅msdn的文档也有一些疑惑https://msdn.microsoft.com/en-us/library/c565h7xx.aspx                     这是*ms*dn对io流的介绍上面有说道标准输入输出是带缓冲的,但我并没有感觉到。 Files opened using the stream routines are buffered by default. The stdout and stderr functions are flushed whenever they are full or, if you are writing to a character device, after each library call. If a program terminates abnormally, output buffers may not be flushed, resulting in loss of data. Use     fflush or _flushall to ensure that the buffer associated with a specified file or all open buffers are flushed to the operating system, which can cache data before writing it to disk. The commit-to-disk feature ensures that the  flushed buffer contents are not lost in the event of a system failure.  这是原文,可能是和编译器有关吧。因为这是vs2015的文档。  然后说说  exit 和_exit,这两个函数。 在linux下_exit是系统调用,而exit是一个库函数。exit会调用fflush清空缓冲流,还有把缓冲区里的东西写入磁盘内。这是和linux的回写有关系的,减少i/o次数,提升效率。标准i/o库也是因为这个原因出现的。程序退出要注意_exit可能会让你的数据丢失。 如果以上有错误的地方,请帮忙指出。在此感谢! 


1 0
原创粉丝点击