#include #include #include /* por write*/ #include /* for exit */ /*#include "ourhdr.h"*/ static void sig_alrm(int); #define MAXLINE 200 void err_sys(char * msg) { printf("%s \n", msg); exit(-1); } int main(void) { int n; char line[MAXLINE]; if (signal(SIGALRM, sig_alrm) == SIG_ERR) err_sys("signal(SIGALRM) error"); alarm(2); /* 1. There is a race condition here. Think of having this process blocked for more than 10 seconds. 2. If the system call (read) is restarted automatically, the time out does nothing! */ if ( (n = read(STDIN_FILENO, line, MAXLINE)) < 0) err_sys("read error"); alarm(0); write(STDOUT_FILENO, line, n); exit(0); } static void sig_alrm(int signo) { printf("en handler\n"); return; /* nothing to do, just return to interrupt the read */ }