Skip to content

Commit 8b88616

Browse files
committed
study
1 parent ab45a17 commit 8b88616

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

10.22.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <signal.h>
4+
#include <unistd.h>
5+
6+
7+
#define BUFFSIZE 1024
8+
9+
static void sig_tstp(int);
10+
11+
int main(void)
12+
{
13+
int n;
14+
char buf[BUFFSIZE];
15+
16+
if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
17+
signal(SIGTSTP, sig_tstp);
18+
}
19+
20+
while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) {
21+
if (write(STDOUT_FILENO, buf, n) != n) {
22+
printf("write error\n");
23+
exit(-1);
24+
}
25+
}
26+
27+
if (n < 0) {
28+
printf("read error\n");
29+
exit(-1);
30+
}
31+
32+
return 0;
33+
}
34+
35+
static void sig_tstp(int signo)
36+
{
37+
sigset_t mask;
38+
39+
sigemptyset(&mask);
40+
sigaddset(&mask, SIGTSTP);
41+
sigprocmask(SIG_UNBLOCK, &mask, NULL);
42+
43+
signal(SIGTSTP, SIG_DFL);
44+
45+
kill(getpid(), SIGTSTP);
46+
47+
signal(SIGTSTP, sig_tstp);
48+
}

0 commit comments

Comments
 (0)