StudyingUnixInterface/10/signal/read.c
2021-01-11 18:00:36 +08:00

36 lines
591 B
C

#include <signal.h>
#include <unistd.h>
#include <setjmp.h>
#include <stdio.h>
static void sig_alrm(int);
static jmp_buf env_alrm;
int main(void) {
int n;
char line[256];
if(signal(SIGALRM, sig_alrm) == SIG_ERR) {
printf("signal(SIGALRM) error\n");
return -1;
}
if(setjmp(env_alrm) != 0) {
printf("read timeout\n");
return -1;
}
alarm(5);
if((n = read(STDIN_FILENO, line, 256) < 0)) {
printf("read error\n");
return -1;
}
alarm(0);
write(STDOUT_FILENO, line, n);
return 0;
}
static void sig_alrm(int signo) {
longjmp(env_alrm, 1);
}