[comp.unix.programmer] Hey, what about SIGIO?

lush@EE.MsState.Edu (Edward Luke) (09/26/90)

In signal.h there is a signal defined as:
#define        SIGIO   23      /* input/output possible signal */


I have an application that will be doing computations, asynchronously
with this I would like to get a signal when new data arrives on an
input file descriptor, or when an output file descriptor is free to be
filled again.  Is it possible for me to do this without using fork()
to start another process to monitor the file descriptor?  Can I tell
the OS to give me a SIGIO when there is a pending condition on a
socket?  Just what is the SIGIO signal for?

Edward Luke
Mississippi State University
NSF Engineering Research Center for Complex Field Simulations

jik@athena.mit.edu (Jonathan I. Kamens) (09/27/90)

In article <LUSH.90Sep26102017@athena0.EE.MsState.Edu>, lush@EE.MsState.Edu (Edward Luke) writes:
|> I would like to get a signal when new data arrives on an
|> input file descriptor, or when an output file descriptor is free to be
|> filled again.  Is it possible for me to do this without using fork()
|> to start another process to monitor the file descriptor?  Can I tell
|> the OS to give me a SIGIO when there is a pending condition on a
|> socket?  Just what is the SIGIO signal for?

  See the man page fcntl(2).  You want to use the F_SETOWN fcntl call on the
file descriptor and set the ownership to your PID, and then use the FASYNC
fcntl to turn on asynchronous I/O; when ASYNC is enabled, and input comes in
on the file descriptor, your process will get SIGIO.

  I'm not certain that you'll get SIGIO if there is room for writing in a
pipe; I've never used it for that, and fcntl(2) only mentions data waiting to
be read when it mentions FASYNC, so I don't know whether or not it'll work.

  Oh, you'll also get SIGURG when there is an "urgent condition" on a socket;
I'm not completely sure what an "urgent condition" is, perhaps that's what
they call a premature closing of the other end of the socket or something.

  One more thing -- I believe that if the other end of the FD is closed,
you'll get a SIGIO, and then when you read, you'll get 0 bytes, thus
signalling EOF.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8495			      Home: 617-782-0710

symon@aesop.cs.unc.edu (James Symon) (09/27/90)

In article <LUSH.90Sep26102017@athena0.EE.MsState.Edu>, lush@EE.MsState.Edu (Edward Luke) writes:
> 
> In signal.h there is a signal defined as:
> #define        SIGIO   23      /* input/output possible signal */
> 
> 
> I have an application that will be doing computations, asynchronously
> with this I would like to get a signal when new data arrives on an
> input file descriptor, or when an output file descriptor is free to be
> filled again.  Is it possible for me to do this without using fork()
> to start another process to monitor the file descriptor?  Can I tell
> the OS to give me a SIGIO when there is a pending condition on a
> socket?  Just what is the SIGIO signal for?
> 


Here are some little test programs I wrote using standard input (the
keyboard) to learn how to do signal trapping. I don't think you need
to mess with fcntl().

****************************************

#include <stdio.h>
#include <signal.h>

int done = 0;

void
handler(signal)
int signal;
{

        if(signal != SIGIO)
                {
                printf("caught signal other than SIGIO . . . wrong!");
                exit(1);
                }
        else
                {
                printf("I lied. I'm taking a nap. You'll have to wait.\n");
                done = 1;
                }
}

main()
{
        extern void handler();
        int ndx;

        signal(SIGIO,handler);
        while(!done)
                {
                printf("I'll do some work here while I wait for your <CR>.\n");
                sleep(5);
                }
}


*************	Here's another:	*************


#include <stdio.h>
#include <signal.h>

void
handler(signal)
int signal;
{
}

main()
{
        extern void handler();
        int ndx, smask, oldmask;
        struct sigvec holder;

        holder.sv_handler = handler;
        holder.sv_mask = 0;
        holder.sv_flags = 0;

        sigvec(SIGIO,&holder,0);

        smask = sigmask(SIGIO);

        for(ndx=0; ndx < 4; ndx++)
                {
                oldmask = sigblock(smask);
                printf("smask = %x, oldmask = %x, wait, I'm working . . .\n",smask,oldmask);
                sleep(1);
                printf("Well? Hit return.\n");
                sigpause(oldmask);
                sigsetmask(oldmask);
                }
}

****************************************************

Jim Symon
symon@radonc.unc.edu
(919) 966-7710

rembo@unisoft.UUCP (Tony Rems) (09/28/90)

In article <LUSH.90Sep26102017@athena0.EE.MsState.Edu> lush@EE.MsState.Edu (Edward Luke) writes:
>
>In signal.h there is a signal defined as:
>#define        SIGIO   23      /* input/output possible signal */
>
>
>I have an application that will be doing computations, asynchronously
>with this I would like to get a signal when new data arrives on an
>input file descriptor, or when an output file descriptor is free to be
>filled again.  Is it possible for me to do this without using fork()
>to start another process to monitor the file descriptor?  Can I tell
>the OS to give me a SIGIO when there is a pending condition on a
>socket?  Just what is the SIGIO signal for?
>

Try the select(2) system call on BSD or SVR4 or the poll(2) system
call on any SVR3 or greater and this should do what you want.

-Tony


-----------------------------------------------------

Somedays, it doesn't pay to get out of bed.

NOTE: My opinions are barely my own much less 
my employers'.

-----------------------------------------------------

joel@isc.intel.com (Joel Clark) (09/28/90)

In article <1990Sep26.184409.10155@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes:
>In article <LUSH.90Sep26102017@athena0.EE.MsState.Edu>, lush@EE.MsState.Edu (Edward Luke) writes:
>|> I would like to get a signal when new data arrives on an
>|> input file descriptor, or when an output file descriptor is free to be
>|> filled again.  Is it possible for me to do this without using fork()
>|> Just what is the SIGIO signal for?
>
>  See the man page fcntl(2).  You want to use the F_SETOWN fcntl call on the
>.....
>  Oh, you'll also get SIGURG when there is an "urgent condition" on a socket;
						^^^^^^^^^^^^^^^^
>I'm not completely sure what an "urgent condition" is, perhaps that's what
>they call a premature closing of the other end of the socket or something.
>-- 
>Jonathan Kamens			              USnail:
>MIT Project Athena				11 Ashford Terrace
>jik@Athena.MIT.EDU				Allston, MA  02134
>Office: 617-253-8495			      Home: 617-782-0710


An "urgent condition" is when Out Of Band data arrives.  This is data
sent with the MSG_OOB flag set. i.e. "send(fd,buf,buflen,MSG_OOB)"
This is generally used to send signals such as interrupt when a DEL
key is hit.  Thus the receiving process can handle the SIGURG signal
by reading and tossing all the data in the socket upto the MSG_OOB
data if this is what it needs to do.

Joel Clark
Intel Scientific Computers
joel@isc.intel.com






line fodder

jik@athena.mit.edu (Jonathan I. Kamens) (09/28/90)

In article <16329@thorin.cs.unc.edu>, symon@aesop.cs.unc.edu (James Symon) writes:
|> Here are some little test programs I wrote using standard input (the
|> keyboard) to learn how to do signal trapping. I don't think you need
|> to mess with fcntl().

  Your program doesn't work for me unless I add the fcntl I mentioned in my
last message.  In particular, if I compile the program at the end of this
message without DO_ASYNC defined, then it doesn't detect input, but if I
compile it with DO_ASYNC defined, then it does.

  Just because it works without the fcntl on your system, doesn't mean you
should expect it to work that way on other systems.

  Note, too, that if you do the fcntl once on your tty and then forget to turn
it off, the next time you run a program that tries to use SIGIO, it'll work
even if it doesn't do the fcntl, since the FASYNC will stay in effect on the
tty line.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8495			      Home: 617-782-0710

------------------------------------------------------------------

#include <stdio.h>
#include <signal.h>
#include <fcntl.h>

#ifdef DO_ASYNC
int ttyflags;
#endif

void
handler(signal)
int signal;
{
     printf("Got some input.\n");
}

void
inthandler(signal)
int signal;
{
     if (signal)
	  printf("Got an interrupt.\n");
     
#ifdef DO_ASYNC
     if (fcntl(fileno(stdin), F_SETFL, ttyflags & ~FASYNC) < 0) {
	  perror("Couldn't set tty flags");
	  exit(1);
     }
#endif

     exit(0);
}

main()
{
        extern void handler();
        int ndx, smask, oldmask;
        struct sigvec holder;
	
#ifdef DO_ASYNC
	if ((ttyflags = fcntl(fileno(stdin), F_GETFL, 0)) < 0) {
	     perror("Couldn't get tty flags");
	     exit(1);
	}

	if (fcntl(fileno(stdin), F_SETFL, ttyflags | FASYNC) < 0) {
	     perror("Couldn't set tty flags");
	     exit(1);
	}
#endif
	     
        holder.sv_handler = (int(*)()) handler;
        holder.sv_mask = 0;
        holder.sv_flags = 0;

        sigvec(SIGIO,&holder,0);

	holder.sv_handler = (int(*)()) inthandler;
	holder.sv_mask = 0;
	holder.sv_flags = 0;

	sigvec(SIGINT, &holder, 0);
	
        smask = sigmask(SIGIO);

        for(ndx=0; ndx < 4; ndx++)
                {
                oldmask = sigblock(smask);
                printf("smask = %x, oldmask = %x, wait, I'm working . . .\n",smask,oldmask);
                sleep(1);
                printf("Well? Hit return.\n");
                sigpause(oldmask);
                sigsetmask(oldmask);
                }

	inthandler(0); /* to turn off strange modes */
	
	exit(0);
}