-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignals.c
100 lines (91 loc) · 1.94 KB
/
signals.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "headers.h"
#include "common.h"
void ctrlz()
{
if (currfgid != -1)
{
bgs[bgi].id = currfgid;
strcpy(bgs[bgi].comm, currfgcom);
bgi++;
setpgid(currfgid, currfgid);
kill(currfgid, SIGTSTP);
printf("Process with PID %d stopped.\n", currfgid);
currfgid = -1;
}
else
zflag = 1;
}
void ctrlc()
{
// printf("%d\n", currfgid);
if (currfgid != -1)
{
// printf("%d\n", currfgid);
kill(currfgid, SIGINT);
currfgid = -1;
}
// printf("ok\n");
}
void ctrld()
{
if (zflag == 1)
{
printf("\n");
// exit(0);
zflag = 0;
return;
}
// kill all background processes
int i, j;
for (i = 0; i < bgi; i++)
kill(bgs[i].id, SIGKILL);
bgi = 0;
printf("\nbye :(");
exit(0);
}
void signals(char *subcom)
{
char *token = strtok(subcom, " \t");
token = strtok(NULL, " \t");
if (token == NULL)
{
printf(ERROR_COLOR "Too less arguments\n" DEFAULT_COLOR);
return;
}
int pid = atoi(token);
token = strtok(NULL, " \t");
if (token == NULL)
{
printf(ERROR_COLOR "Too less arguments\n" DEFAULT_COLOR);
return;
}
int signum = atoi(token) % 32;
token = strtok(NULL, " \t");
if (token != NULL)
{
printf(ERROR_COLOR "Too many arguments\n" DEFAULT_COLOR);
return;
}
if (kill(pid, signum) < 0)
{
printf(ERROR_COLOR "Error in sending signal\n" DEFAULT_COLOR);
return;
}
// // remove from bgs
// int i;
// for (i = 0; i < bgi; i++)
// {
// if (bgs[i].id == pid)
// {
// int j;
// for (j = i; j < bgi - 1; j++)
// {
// bgs[j].id = bgs[j + 1].id;
// strcpy(bgs[j].comm, bgs[j + 1].comm);
// }
// bgi--;
// break;
// }
// }
return;
}