[comp.unix.questions] sockets and signals

skp@stl.stc.co.uk (Steve Perryman ) (08/10/90)

Does anyone know of a way to set up a signal handler such that if a flood
of data comes in to a socket, the SIGIO/SIGPOLL (maybe even SIGURG) signal
can invoke a handler fast enough such that a variable can be incremented to
represent the correct number of data items at the socket.

The signal handler will be :

void catch()
{
   signal(SIGIO,catch) ;
   count++ ;
   ...
   /* Process the signal */
   ...
}

I've used fcntl to allow the socket to interrupt :

fcntl(sock,F_SETOWN,getpid()) ;
fcntl(sock,F_SETFL,F_ASYNC) ;
...
...

This works but it can't log the correct number of items received if they come
in as bursts of data (5+ items per burst). Can this timing problem be resolved
using SIGIO , or is another way required ???




skp

lm@snafu.Sun.COM (Larry McVoy) (08/11/90)

In article <3304@stl.stc.co.uk> "Steve Perryman " <skp@stl.stc.co.uk> writes:
>
>Does anyone know of a way to set up a signal handler such that if a flood
>of data comes in to a socket, the SIGIO/SIGPOLL (maybe even SIGURG) signal
>can invoke a handler fast enough such that a variable can be incremented to
>represent the correct number of data items at the socket.
>
>This works but it can't log the correct number of items received if they come
>in as bursts of data (5+ items per burst). Can this timing problem be resolved
>using SIGIO , or is another way required ???

This can't be done with Unix signals.  Unix signals don't stack, i.e., if
you hit yout interrupt character several times before the kernel delivers 
the signal, the application will only see one signal.

About the best you can do for you application is to use sigio as a hint that
there is data waiting and schedule a timeout every second or so to collect
what you might have missed.  It's also a good idea to code your processing
like so:


	for ( ;; ) {
		sigpause(0);
		while (more_data()) {
			process_data();
		}
	}

rather than

	for ( ;; ) {
		sigpause(0);
		if (more_data()) {
			process_data();
		}
	}
---
Larry McVoy, Sun Microsystems     (415) 336-7627       ...!sun!lm or lm@sun.com

chris@mimsy.umd.edu (Chris Torek) (08/13/90)

>In article <3304@stl.stc.co.uk> Steve Perryman <skp@stl.stc.co.uk> asks
>>[for] a way to set up a signal handler such that [...] a variable can be
>>incremented to represent the correct number of data items at the socket.

In article <140492@sun.Eng.Sun.COM>, lm@snafu.Sun.COM (Larry McVoy) writes:
>This can't be done with Unix signals.  Unix signals don't stack ....

This is true (even with BSD or POSIX reliable signals); however:

>About the best you can do for you application is to use sigio as a hint that
>there is data waiting and schedule a timeout every second or so to collect
>what you might have missed.

This is not necessary.  With reliable signals and non-blocking I/O (or
select()) it is possible to code reliable interrupt handlers, just as
it is possible to code relible interrupt handlers on `bare hardware'
(except with certain pathological hardware systems).  It is tricky,
however.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris
	(New campus phone system, active sometime soon: +1 301 405 2750)