-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsignal.c
68 lines (64 loc) · 1.42 KB
/
signal.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "headers.h"
extern List cProc[100];
extern BG fore, rProc[100];
extern int no_i, no_c, shell_pid;
extern char hostname[N], *username;
void handler() // signal handler function
{
int status;
pid_t id = waitpid(-1, &status, WNOHANG);
int k = WIFEXITED(status); // get the status of child process
if (id > 0)
{
for (int i = 0; i < no_i; i++)
{
if (id == rProc[i].id)
{
cProc[no_c].id = id;
cProc[no_c].num = rProc[i].num;
cProc[no_c].status = k;
strcpy(cProc[no_c].pname, rProc[i].pname);
rProc[i].done = -1;
no_c++;
break;
}
}
}
}
void ctrl_c()
{
pid_t pid = getpid();
if (fore.id > 0)
{
kill(fore.id, SIGINT);
fore.id = -1;
printf("\n");
}
else if (pid == shell_pid)
{
printf("\n");
prompt(username, hostname);
fflush(stdout);
}
}
void ctrl_z()
{
pid_t pid = getpid();
if (fore.id > 0)
{
kill(fore.id, SIGTSTP);
rProc[no_i].id = fore.id;
rProc[no_i].num = no_i;
rProc[no_i].done = 1;
strcpy(rProc[no_i].pname, fore.pname);
no_i++;
if (no_i == 100)
no_i = 1;
}
else if (pid == shell_pid)
{
printf("\n");
prompt(username, hostname);
fflush(stdout);
}
}