Path: utzoo!mnetor!uunet!mcvax!enea!ttds!draken!sics!erikn
From: erikn@sics.se (Erik Nordmark)
Newsgroups: comp.unix.questions
Subject: Re: Need help with interprocess communications
Message-ID: <1639@sics.se>
Date: 9 Dec 87 21:21:43 GMT
References: <8117@steinmetz.steinmetz.UUCP>
Reply-To: erikn@sics.UUCP (Erik Nordmark)
Organization: Swedish Institute of Computer Science, Kista
Lines: 60
Keywords: Pipes, Ptys, Buffering, I/O

[[ I tried sending this as mail using different addresses, but failed! ]]

In article <8117@steinmetz.steinmetz.UUCP> you write:
>
>
>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.
>
>
>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 the  key 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?
>

>From looking at the BSD4.3 sources I found out the following:
When a tty is opened the associated process group is set to
that of the creator. The signals that the tty driver generate (e.g. caused
by ^C) are sent to this process group.

However, for sockets (a pipe is implemented as a pair of sockets in BSD4.3
and maybe elsewhere!) the associated process group is not set automatically.

So what you have to do is to set it before you can get ant SIGIO's! Use
   int pgrp = getpid();
   if (fcntl(fd, F_SETOWN, pgrp) == -1) {
	perror("fnctl");
	exit(1);
   }
or
       ioctl(fd, SIOCSPGRP, &pgrp)	/* note: & */

I think this should work even if pipes aren't implemented as a pair of
sockets, but I haven't tried any of it.

>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?
>

See select(2). (Just a detail: select will tell you that there is data
to read if there actually is data to read or if the other end(s) have
closed the pipe. In the latter case read() will return an EOF - this
stuff caused me some trouble before I read the *real* documentation -
the OS source code!!)

-------------------------------------------------------------------------
Erik Nordmark
Swedish Institute of Computer Science, Box 1263, S-163 13  SPANGA, Sweden
Phone: +46 8 750 79 70	Ttx: 812 61 54 SICS S	Fax: +46 8 751 72 30

uucp:	erikn@sics.UUCP or {seismo,mcvax}!enea!sics!erikn
Domain: erikn@sics.se
-------------------------------------------------------------------------