-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignalhandler.c
More file actions
128 lines (127 loc) · 3.29 KB
/
signalhandler.c
File metadata and controls
128 lines (127 loc) · 3.29 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "headers.h"
#include "main.h"
#include "takeinput.h"
#include "prompt.h"
#include "systemcommand.h"
#include "io.h"
#include "history.h"
#include "pinfo.h"
#include "signalhandler.h"
void signalc(int sig_no)
{
int process_pid = ongoing_process;
if (process_pid > 0)
{
//fprintf(stderr, "%d process_pid\n", process_pid);
if (kill(process_pid, sig_no) < 0)
{
perror("signalc : ");
}
else
{
printf("\n");
ongoing_process = 0;
fflush(stdout);
}
}
else
{
printf("\n");
char cwd[1024] = "";
getcwd(cwd, 1024);
prompt(cwd);
fflush(stdout);
}
}
void signalz(int sig_no)
{
int process_pid = ongoing_process;
if (process_pid > 0)
{
// printf("%d process_pid", process_pid);
if (kill(process_pid, sig_no) < 0)
{
perror("signalz : ");
}
else
{
int k = process_pid;
int count = 0;
char str1[256], str2[256];
for (int i = 0; i < 256; i++)
{
str1[i] = '\0';
str2[i] = '\0';
}
while (k > 0)
{
str1[count] = k % 10 + '0';
k = k / 10;
count++;
}
int index = 0;
while (index < count)
{
str2[index] = str1[count - 1 - index];
index++;
}
char cmd[1024] = "";
strcat(cmd, "/proc/");
strcat(cmd, str2);
strcat(cmd, "/stat");
int fd1 = open(cmd, O_RDONLY);
if (fd1 == -1)
{
printf("Process is ended\n");
}
else
{
char a[1024] = "";
char divs[128][128];
for (int ind = 0; ind < 128; ind++)
{
for (int indi = 0; indi < 128; indi++)
{
divs[ind][indi] = '\0';
}
}
int charread = read(fd1, a, 1024);
index = 0;
for (int ind = 0; ind < charread; ind++)
{
// printf("ind = %d\n", ind);
int k = 0;
while (a[ind] != ' ')
{
divs[index][k] = a[ind];
ind++;
k++;
// printf("ind = %d\n", ind);
if (ind >= charread)
{
break;
}
}
if (k != 0)
{
index++;
}
}
backgroundpid[backgroundprocess] = process_pid;
strcpy(background_process[backgroundprocess], divs[1]);
backgroundprocess++;
close(fd1);
printf("\n%d stopped\n", process_pid);
ongoing_process = 0;
}
}
}
else
{
printf("\n");
char cwd[1024] = "";
getcwd(cwd, 1024);
prompt(cwd);
fflush(stdout);
}
}