Stdout

For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. 

Note that fflush() only flushes the user-space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, for example, with sync(2) or fsync(2).

Normally all files are block buffered. When the first I/O operation occurs on a file, malloc(3) is called, and a buffer is obtained.

Golang  os/file/ 写文件是直接调用系统库函数write(),把内容写入到文件。当然,不一定是到disk,需要调用fsync()。

write()调用的成本是很高的,需要数千个CPU周期。

C语言里stdout是FILE结构,是对内核文件描述符的封装(增加了用户空间的buffer,line-buffer/block-buffer是指的用户空间的buffer)。但是Golang里的stdout虽然也是标准的File类型,但是没有buffer。

之所以在用户空间增加buffer是为了减少write()的调用。

glibc则提供了许多系统调用的封装。这使我们在编写程序的时候,并不需要直接和内核进行交互,而是借用glibc这层封装,更加安全、稳定地使用。

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值