没有缓冲的原子线读取 [英] Atomic line-reading without buffering

查看:53
本文介绍了没有缓冲的原子线读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法以原子方式从文件(通过描述符或流)读取一行(一系列以

换行符结尾的字符),

缓冲文件的其他内容?

Is there a way to read a line (a series of characters ending in a
newline) from a file (either by descriptor or stream) atomically,
without buffering additional contents of the file?

推荐答案

japhy写道:
japhy wrote:

有没有办法从一个文件(通过描述符或流)原子地读取一行(一系列以

换行符结尾的字符),

而不缓冲额外的文件的内容?
Is there a way to read a line (a series of characters ending in a
newline) from a file (either by descriptor or stream) atomically,
without buffering additional contents of the file?



在标准C中你所能做的就是使用setvbuf函数和

指定不用_IONBF缓冲作为其中一个参数。尽管这是
,操作系统在进行I / O时仍然可以使用磁盘缓冲区。

关闭所有缓冲是系统依赖的,你必须

检查你的操作系统提供的功能。


在你使用过的上下文中,原子词不合适
$ b $它。即使禁用缓冲,也不能保证I / O是原子操作。

Within Standard C all you can do is use the setvbuf function and
specify no buffering with _IONBF as one of it''s arguments. Despite
this, the operating system may still use disk buffers when doing I/O.
To turn off all buffering is system dependant and you''ll have to
examine the facilities your OS provides to do so.

The word atomic is inappropriate in the context in which you''ve used
it. Even when buffering is disabled, I/O is not guaranteed to be an
atomic operation.


文章< 11 ********************@p10g2000cwp.googlegroups.c om>,

santosh< sa ********* @ gmail.comwrote:
In article <11********************@p10g2000cwp.googlegroups.c om>,
santosh <sa*********@gmail.comwrote:

> japhy写道:
>japhy wrote:

>有没有办法阅读一行(一系列以
换行符结尾的字符)从文件(通过描述符或流)原子地,
而不缓冲文件的其他内容?
>Is there a way to read a line (a series of characters ending in a
newline) from a file (either by descriptor or stream) atomically,
without buffering additional contents of the file?


>单词atomic在您使用它的上下文中是不合适的。即使禁用缓冲,也不能保证I / O是原子操作。
>The word atomic is inappropriate in the context in which you''ve used
it. Even when buffering is disabled, I/O is not guaranteed to be an
atomic operation.



虽然如果你向POSIX迈出了一步,但是下面写一个

系统相关的大小被认为是原子的。 POSIX需要

这个属性,因为它处理多个进程(以及

以后版本中的多个线程),这将是一个真正的

麻烦将一个写入的输出与另一个写入的

输出混合在一起。


-

如果你骗到编译器,它会报复。 - Henry Spencer

Though if you make the step over to POSIX, writes below a
system-dependant size are promised to be atomic. POSIX needs
this property because it deals with multiple processes (and
multiple threads in later editions) and it would be a real
nuisance to have the output of one write intermixed with the
output of another write.

--
If you lie to the compiler, it will get its revenge. -- Henry Spencer


Walter Roberson写道:
Walter Roberson wrote:

文章< 11 ****** **************@p10g2000cwp.googlegroups.c om>,

santosh< sa ********* @ gmail.comwrote:
In article <11********************@p10g2000cwp.googlegroups.c om>,
santosh <sa*********@gmail.comwrote:

japhy写道:
japhy wrote:

有没有办法读取一行(一系列以a结尾的字符)

换行符)从文件(通过描述符或流)原子地,

而不缓冲文件的其他内容?
Is there a way to read a line (a series of characters ending in a
newline) from a file (either by descriptor or stream) atomically,
without buffering additional contents of the file?


在您使用

的上下文中,单词atomic是不合适的。即使禁用缓冲,也不能保证I / O是原子操作。
The word atomic is inappropriate in the context in which you''ve used
it. Even when buffering is disabled, I/O is not guaranteed to be an
atomic operation.



虽然如果你跳到POSIX,下面写一个与系统相关的大小的b $ b bb承诺是原子的。 POSIX需要

这个属性,因为它处理多个进程(以及

以后版本中的多个线程),这将是一个真正的

麻烦将一个写入的输出与另一个写入的

输出混合在一起。


Though if you make the step over to POSIX, writes below a
system-dependant size are promised to be atomic. POSIX needs
this property because it deals with multiple processes (and
multiple threads in later editions) and it would be a real
nuisance to have the output of one write intermixed with the
output of another write.



是的,谢谢,我应该提到这一点,但我在考虑更多符合C标准的
关于

原子操作的保证。

Yes, thanks, I should probably have mentioned that, but I was thinking
more along the lines of the C standard''s assurances with regard to
atomic operations.


这篇关于没有缓冲的原子线读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆