Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSIX: Pipe syscall in FreeBSD vs Linux

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

like image 989
Joseph Avatar asked Mar 22 '11 00:03

Joseph


2 Answers

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.

like image 34
Greg Hewgill Avatar answered Nov 06 '22 09:11

Greg Hewgill


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.

like image 111
Nicholas Knight Avatar answered Nov 06 '22 09:11

Nicholas Knight