forked from wenjun1055/c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.8.c
More file actions
39 lines (31 loc) · 656 Bytes
/
10.8.c
File metadata and controls
39 lines (31 loc) · 656 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>
static void sig_alrm(int);
static jmp_buf env_alrm;
int main(void)
{
int n;
char line[100];
if (signal(SIGALRM, sig_alrm) == SIG_ERR) {
printf("signal(SIGALRM) error\n");
exit(-1);
}
if (setjmp(env_alrm) != 0) {
printf("read timeout\n");
exit(-1);
}
alarm(10);
if ((n = read(STDIN_FILENO, line, 100)) < 0) {
printf("read error\n");
}
alarm(0);
write(STDOUT_FILENO, line, n);
return 0;
}
static void sig_alrm(int signo)
{
longjmp(env_alrm, 1);
}