[comp.unix.questions] named pipes and O_RDWR

net@opal.cs.tu-berlin.de (Oliver Laumann) (05/17/91)

Is it allowed to open(2) a named pipe (FIFO) with O_RDWR?

The X/Open Portability Guide explicitly says that in this case the
result of the call to open() is undefined.  On the other hand, P1003.1
and the manual pages on several UNIX versions do not explicitly forbid
this.

I have noticed that under SunOS, when a process opens several FIFOs
with O_RDWR and then writes a single byte into each of these in turn,
the 12th call to write() blocks.  It blocks even if I set the respective
file descriptors to non-blocking.

This doesn't happen under Ultrix 4.0.  I currently don't have access to
other systems that support named pipes, so I can't check it there.

Here is a small test program that demonstrates the `blocking write
syndrome'.  Before you run it, you have to mknod(8) 14 FIFOs named
0, 1, 2, etc. in the current directory.

#include <fcntl.h>

#define N 15

main () {
    int i, f[N];
    char buf[10];

    for (i = 0; i < N; i++) {
	sprintf (buf, "%d", i);
	if ((f[i] = open (buf, O_RDWR, 0)) == -1) {
	    perror (buf); return;
	}
    }
    for (i = 0; i < N; i++)
	printf ("%d: %d\n", i, write (f[i], buf, 1));
}

Regards,
--
Oliver Laumann    net@tub.cs.tu-berlin.de  net@tub.UUCP  ol@gnu.ai.mit.edu

urban@cbnewsl.att.com (john.urban) (05/17/91)

In article <3374@kraftbus.cs.tu-berlin.de> net@opal.cs.tu-berlin.de (Oliver Laumann) writes:
>Is it allowed to open(2) a named pipe (FIFO) with O_RDWR?
>
>The X/Open Portability Guide explicitly says that in this case the
>result of the call to open() is undefined.  On the other hand, P1003.1
>and the manual pages on several UNIX versions do not explicitly forbid
>this.
>
>I have noticed that under SunOS, when a process opens several FIFOs
>with O_RDWR and then writes a single byte into each of these in turn,
>the 12th call to write() blocks.  It blocks even if I set the respective
>file descriptors to non-blocking.
>

This works fine on AT&T UNIX System V/x86 Release 4.0 Version 2.1.

Sincerely,

John Ben Urban

P.S. If you the line: mknod (buf, 010666); and change the open(2) sightly,
there is no need to run the mknods by hand first.

OLD CODE >
>	sprintf (buf, "%d", i);
>	if ((f[i] = open (buf, O_RDWR, 0)) == -1) {

New CODE <
<	sprintf (buf, "%d", i);
<	mknod (buf, 010666);
<	if ((f[i] = open (buf, O_RDWR, 0644)) == -1) {