匿名管道写的原子性
命名管道FIFO中提供了写操作的原子化,但存在多个写者(生产者)同时往一个管道中写的时候只要每一次写操作的数据长度小于PIPE_BUF就能保证这些数据要么全部写入要么阻塞等待(或出错返回),这样就保证了两次写的数据不会交错。
想请教一下匿名管道并没有提供这样的功能,那么当有多个写者同时往一个管道中写的时候是否会出现数据交错的问题?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
named pipe
和anonymouse pipe
的原子性保证是由read
和write
来保证的。write():
Write requests of {PIPE_BUF} bytes or less shall not be interleaved with data from other processes doing writes on the same pipe. Writes of greater than {PIPE_BUF} bytes may have data interleaved, on arbitrary boundaries, with writes by other processes, whether or not the O_NONBLOCK flag of the file status flags is set.
read():
Upon successful completion, where nbyte is greater than 0, read() shall mark for update the last data access timestamp of the file, and shall return the number of bytes read. This number shall never be greater than nbyte. The value returned may be less than nbyte if the number of bytes left in the file is less than nbyte, if the read() request was interrupted by a signal, or if the file is a pipe or FIFO or special file and has fewer than nbyte bytes immediately available for reading. For example, a read() from a file associated with a terminal may return one typed line of data.
所以在长度限定下都是原子的。