forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathkill.c
76 lines (73 loc) · 1.67 KB
/
kill.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
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
#include "jobsconst.h"
int
main(int argc, char **argv)
{
int fd = open(JOBS_FILENAME, O_RDONLY);
int i;
if (argc < 2) {
printf(2, "usage: kill pid...\n");
exit();
}
for (i = 1; i < argc; i++) {
//printf(2,"%s\n", argv[i])
if (argv[i][0] == '%') {//kill backstage process
//int found = 0;
char newargv[10];//remove %
int len = strlen(argv[i]);
for (int j = 0; j < len; j++) {
newargv[j] = argv[i][j + 1];
}
newargv[len-1] = '\0';
int id_to_found = atoi(newargv);
//printf(2,"%s\n", newargv);
//int fd;//file discriptor
if (fd < 0) {//file not exist
printf(2, "no backstage processes\n");
continue;
}
char pid_backstage[10] = "";
char buf[100];
for(int k = 0; k < id_to_found; k++) {//get id from file
strcpy(buf, "");
int n = jobs_readline(fd, buf, sizeof(buf));
//printf(2,"%d\n", n);
if (n < 0) {
printf(2,"read error\n");
//found = -1; //no warnings again
break;
}
else if (n == 0) {//read over
printf(2, "process not found\n");
break;
}
}
if(id_to_found == 0) {
printf(2,"process id numbered from 1\n");
continue;
}
strcpy(pid_backstage, "");
int j = 0;
while (buf[j] == ' ') {
j++;
}
while (buf[j] != ' ' && buf[j] != '\0') {
char bufarray[2];
bufarray[0] = buf[j];
bufarray[1] = '\0';
strcpy(pid_backstage + strlen(pid_backstage), bufarray);
j++;
}
pid_backstage[strlen(pid_backstage)] = '\0';
//printf(2,"%s\n", pid_backstage);
kill(atoi(pid_backstage));//kill the backstage process
}
else {
kill(atoi(argv[i]));
}
}
exit();
}