Hi-
I’m trying to use a named pipe (FIFO), but I’ve run into an interesting problem. My program hangs when I try to fopen() the pipe.
(Actually, if I first run the program as root, ctrl-C, then run it again as another user, fopen() will return NULL the second time around. This is the only case where fopen() doesn’t hang.)
This code came from a test program that I wrote months ago. I’m really sure it used to work…
TIA,
- Pete
======= Sample Code ==================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#define FIFO “/usr/tmp/net.in”
void main()
{
FILE *f;
printf( “main: starting…\n” );
if ( mkfifo( FIFO, S_IRUSR | S_IWUSR ) == -1 )
{
printf( “main: mkfifo(%s) failed, errno=%1d, %s.\n”,
FIFO, errno, strerror(errno) );
}
printf( “before fopen(%s)\n”, FIFO );
f= fopen( FIFO, “r” );
printf( “after fopen, f=%1d.\n”, (int) f );
}
======= End Sample Code ============
Pete DiMarco <peted@ifspurity.nospam.com> wrote:
Hi-
I’m trying to use a named pipe (FIFO), but I’ve run into an interesting problem. My program hangs when I try to fopen() the pipe.
(Actually, if I first run the program as root, ctrl-C, then run it again as another user, fopen() will return NULL the second time around. This is the only case where fopen() doesn’t hang.)
This code came from a test program that I wrote months ago. I’m really sure it used to work… >
In *NIX, FIFO’s work a little different from regular files. If you open() a FIFO
for READ, the open will block until another process opens the FIFO for WRITE (and
vise versa). If you wish, you can open() the FIFO with the O_NONBLOCK flag set to
cause the open() for READ to return immediately. Something to watch out for, is that
if you open() with O_NONBLOCK for write, you’ll return and error until someone already
has the FIFO open for reading.
-Adam
Previously, Operating System for Tech Supp wrote in qdn.public.qnx4:
Pete DiMarco <> peted@ifspurity.nospam.com> > wrote:
Hi-
I’m trying to use a named pipe (FIFO), but I’ve run into an interesting problem. My program hangs when I try to fopen() the pipe.
(Actually, if I first run the program as root, ctrl-C, then run it again as another user, fopen() will return NULL the second time around. This is the only case where fopen() doesn’t hang.)
This code came from a test program that I wrote months ago. I’m really sure it used to work… >
In *NIX, FIFO’s work a little different from regular files. If you open() a FIFO
for READ, the open will block until another process opens the FIFO for WRITE (and
vise versa). If you wish, you can open() the FIFO with the O_NONBLOCK flag set to
cause the open() for READ to return immediately. Something to watch out for, is that
if you open() with O_NONBLOCK for write, you’ll return and error until someone already
has the FIFO open for reading.
-Adam
Oops!
Thanks for the info!