Expert C Programming

(Jeff_L) #1

Programming Solution


Asynchronous I/O


The code below causes an SVr4-based OS to send an interrupt for each character entered on
the standard input.


#include <errno.h>


#include <signal.h>


#include <stdio.h>


#include <stropts.h>


#include <sys/types.h>


#include <sys/conf.h>


int iteration=0;


char crlf []={0xd,0xa, 0};


void handler(int s)


{


int c=getchar(); / read a character /


printf("got char %c, at count %d


%s",c,iteration,crlf);


if (c=='q') {


system("stty sane");


exit(0);


}


}


main()


{


sigset(SIGPOLL, handler); / set up the handler /


system("stty raw -echo");


ioctl(0, I_SETSIG, S_RDNORM); /* ask for interrupt


driven input */


for(;;iteration++);


/ can do other stuff here /


}


Use sigset() instead of signal() and you won't have to re-register the signal handler each

Free download pdf