匿名管道写的原子性

发布于 2022-09-03 08:34:01 字数 176 浏览 17 评论 0

命名管道FIFO中提供了写操作的原子化,但存在多个写者(生产者)同时往一个管道中写的时候只要每一次写操作的数据长度小于PIPE_BUF就能保证这些数据要么全部写入要么阻塞等待(或出错返回),这样就保证了两次写的数据不会交错。
想请教一下匿名管道并没有提供这样的功能,那么当有多个写者同时往一个管道中写的时候是否会出现数据交错的问题?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有 2022-09-10 08:34:01

named pipeanonymouse pipe 的原子性保证是由 readwrite 来保证的。

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.

所以在长度限定下都是原子的。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文