Hey, what about SIGIO?

Jonathan I. Kamens jik at athena.mit.edu
Fri Sep 28 13:50:27 AEST 1990


In article <16329 at thorin.cs.unc.edu>, symon at 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 at 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);
}



More information about the Comp.unix.programmer mailing list