When a pipe updated, any signal can be caught?

When a pipe updated, any signal can be caught?

Post by Michael Wojc » Sat, 22 Aug 1998 04:00:00

> I need to monitor a pipe in such a way that if any change occurs,
> fire a action against it(read the new bytes just appended). A signal
> handler could be ideal if such signal was defined(pipe changed signal).

(In the future, you might want to mention details like what version
of AIX you're using, what language you're working in, and so on.)

Probably the preferred approach these days would be to use POSIX
threads and have a thread always doing a blocking read on the pipe.

It's not clear (without trying it, which is left as an exercise for
the reader) whether the FIOASYNC ioctl / FASYNC descriptor flag
(they're the same thing; FIOASYNC sets or clears FASYNC) will work
for pipes.  (I'm assuming this actually *is* a pipe, and not, say, a
Unix-domain socket.)  ioctl(2) says FIOASYNC functionality is only
implemented for ttys, ptys, and sockets; however, some Unixes
implement pipes using sockets, and I can't remember offhand if AIX is
one.  (Actually, looking at Stevens, _Advanced Programming in the
Unix Environment_, it appears that FIOASYNC should work with both
SVR4, where pipes are stream devices, and BSD4.2+, where they're
Unix-domain sockets.  So it probably works in AIX.)

Assuming FASYNC *is* honored for pipes in AIX, set up a signal
handler for SIGIO, use the FIOSETOWN ioctl to set your process as the
destination for SIGIO on the descriptor, then use the FIOASYNC ioctl
to enable async I/O.  When someone writes to the pipe, your SIGIO
handler will be invoked:


   int PipeD;                     /* pipe descriptor */
   int MyPid = getpid(),
       On = 1;
   struct sigaction CatchSIGIO;
   void ReadFromPipe(int);


   CatchSIGIO.sa_handler = ReadFromPipe;
   CatchSIGIO.sa_flags = 0;

   sigaction(SIGIO, &CatchSIGIO, NULL);

   ioctl(PipeD, FIOSETOWN, &MyPid);
   ioctl(PipeD, FIOASYNC, &On);

or something like that (with appropriate error checking, etc.).

AAI Development, Micro Focus Inc.
Department of English, Miami University

The lark is exclusively a Soviet bird.  The lark does not like the
other countries, and lets its harmonious song be heard only over the
fields made fertile by the collective labor of the citizens of the
happy land of the Soviets.
-- D. Bleiman