POSIX: Pipe syscall in FreeBSD vs Linux

风格不统一 提交于 2019-12-23 08:17:10

问题


In Linux (2.6.35-22-generic), man pipe states that

pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication."

In FreeBSD (6.3-RELEASE-p5), man pipe states that

The pipe() system call creates a pipe, which is an object allowing bidirectional data flow, and allocates a pair of file descriptors."

One is unidirectional, the other is bidirectional. I hope this isn't a silly question, but which method is the standard way of doing this? Are they both POSIX compliant?

To make my intentions clear, I lost some points on an exam for believing pipe() was one way and am looking for some ammo to get any points back ;p


回答1:


I started this as a comment on Greg's answer at first, but it occurs to me that it more closely answers your specific question:

pipe()s documentation in the POSIX standard explicitly states that the behavior in question is "unspecified" -- that is, pipe() is not required to be bidirectional, though it's not forbidden. Linux's is unidirectional, FreeBSD's is bidirectional. Both are compliant, one just implements additional behavior that is not required (but doesn't break apps built to work on compliant systems).

Data can be written to the file descriptor fildes[1] and read from the file descriptor fildes[0]. A read on the file descriptor fildes[0] shall access data written to the file descriptor fildes[1] on a first-in-first-out basis. It is unspecified whether fildes[0] is also open for writing and whether fildes[1] is also open for reading.

I wouldn't count on getting the points back (though you should). Professors have a tendency to ignore the real world in favor of whatever they've decided is correct.




回答2:


The FreeBSD man page for pipe is pretty clear on this point:

The bidirectional nature of this implementation of pipes is not portable to older systems, so it is recommended to use the convention for using the endpoints in the traditional manner when using a pipe in one direction.



来源:https://stackoverflow.com/questions/5385534/posix-pipe-syscall-in-freebsd-vs-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!