linux - 匿名管道写的原子性
伊谢尔伦
伊谢尔伦 2017-04-17 15:27:08
0
1
554

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

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
Peter_Zhu

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.

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!