Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!steinmetz!mercutio!lanzo From: lanzo@mercutio.steinmetz (Mark Lanzo) Newsgroups: comp.unix.questions Subject: Need help with interprocess communications Message-ID: <8117@steinmetz.steinmetz.UUCP> Date: Mon, 7-Dec-87 10:35:13 EST Article-I.D.: steinmet.8117 Posted: Mon Dec 7 10:35:13 1987 Date-Received: Sun, 13-Dec-87 01:32:05 EST Sender: root@steinmetz.steinmetz.UUCP Reply-To: lanzo@.UUCP (Mark Lanzo) Organization: General Electric CRD, Schenectady, NY Lines: 95 Keywords: Pipes, Ptys, Buffering, I/O [This was originally posted to comp.unix, where it seems to have fallen into a black hole, so I'll try my luck here...] I'm working on an application where I have a process fork off a 2nd process and maintain communications with the subprocess via a pipe (or more accurately, via 2 pipes--one for parent-to-child writes, the other for child-to-parent). The problem that I am running into is that I need to be able to detect when the child process writes to the pipe; and I need the I/O to be as unbuffered as possible. I need the ability to detect writes asynchronously as well as synchronously. I have tried using "fcntl(fd,F_SETFL,FASYNC)" as well as setting up an interrupt handler to handle SIGIO signals (via "sigvec(2)"), and this works fine when I'm reading from the terminal, but does not seem to work at all when I try it from a pipe. My intent was for an interrupt routine to be triggered whenever the child process wrote into the pipe; but I never get a SIGIO signal when data is written into the pipe. I can manually examine the pipe to see if any data is waiting to be read (by the master process) using "ioctl(FIONREAD)", but even this does not work reliably since I don't detect that anything was written until the child process flushes its end of the pipe. I need to know when it writes something to the pipe, not when the buffer is full (4K bytes?). One major constraint is that I don't necessarily have access to the source code of the child process--the code I'm working on (the master process) has to work with any existing program out there. For reference, my basic setup looks like: call "pipe" to open a pipe which is for writing from parent-to-child call "pipe" to open a pipe for writing from child-to-parent use fcntl to set FASYNC mode on all descriptors. fork process: child ------- disconnect unused ends of pipes: close parent-to-child write end close child-to-parent read end use close/dup etc to make pipe ends be stdin & stdout. set unbuffered I/O mode on stdin, stdout, stderr exec subprocess... parent ---------- disconnect unused ends of pipes: close child-to-parent write end close parent-to-child read end set up signal handler for SIGIO interrupts. sit in loop waiting for signals (sigpause(2))-- SIGIO handler does ioctl(FIONREAD) to check pipe, then reads data. Well, the SIGIO handler works fine to detect input from places like stdin, but never sees anything coming down the pipe. When it gets invoked (generally by me banging on thekey causing an interrupt from stdin), it does find that there is data available in the pipe (as well as stdin) and has no problem reading it. Does anyone out there know how I can fix this problem? Also: Is there a way that I can determine WHICH file descriptor caused a SIGIO interrupt to be invoked, or by which I can set up a different interrupt handler for each descriptor? Oh yeah, almost forgot: I'm using Ultrix 1.2 (going to 2.0 soon), but I'm interesting in solutions which are as portable as possible between various flavors of Unix. Thanks in advance, Mark Lanzo mercutio!lanzo P.s. I'm also interested in solutions using the pseudoterminal mechanism or sockets. I tried using pseudoterminals, but had no more luck than I have had with pipes (in fact, I got almost the same results). Pseudoterminals would seem to suffer from the problem that I must have a 'device' (special file) set up ahead of time, ie., I need to create the files in /dev/. Since my application may eventually be running MANY subprocesses simultaneously, I don't want to have to tie up all the pseudoterminals, especially since on systems around here the ptys exist primarily for the sake of remote logins (as best as I can figure it). Sockets unfortunately I don't know enough about to do anything useful, and what docs I could find didn't help much.