-
Notifications
You must be signed in to change notification settings - Fork 307
/
apue.9.1.c
42 lines (36 loc) · 873 Bytes
/
apue.9.1.c
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
40
41
42
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
static void sig_hup(int signo)
{
printf("SIGHUP received, pid = %d\n", getpid());
}
static void pr_ids(char *name)
{
printf("%s: pid = %d, ppid = %d, pgrd = %d, tpgrd = %d\n", name, getpid(), getppid(), getpgrp(), tcgetpgrp(STDIN_FILENO));
fflush(stdout);
}
int main(void)
{
char c;
pid_t pid;
pr_ids("parent");
if ((pid = fork()) < 0) {
printf("fork error\n");
exit(-1);
} else if (pid > 0) {
sleep(5);
exit(0);
} else {
pr_ids("child");
signal(SIGHUP, sig_hup);
kill(getpid(), SIGTSTP);
pr_ids("child");
if (read(STDIN_FILENO, &c, 1) != 1 ) {
printf("read error from controlling TTY, errno = %d\n", errno);
}
exit(0);
}
}