-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_jobs.c
148 lines (126 loc) · 3.46 KB
/
shell_jobs.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "shell_jobs.h"
struct p processes[10000];
int running_proc_num;
pid_t current_running_proc;
int jobs(char** args, char* root) {
int la = 0;
while (la < running_proc_num)
{
// printf("%d\n", la);
// fprintf(stdout, "%s %d\n", processes[la].pname, processes[la].pid);
// fprintf(stdout, "\[%d\] ")
if (processes[la].status == 1) {
fprintf(stdout, "[%d] Stopped %s [%d]\n", la + 1, processes[la].pname, processes[la].pid);
}
if (processes[la].status == 0) {
fprintf(stdout, "[%d] Running %s [%d]\n", la + 1, processes[la].pname, processes[la].pid);
}
if (processes[la].status == 2) {
fprintf(stdout, "[%d] Moved %s [%d] To Foreground\n", la + 1, processes[la].pname, processes[la].pid);
}
la++;
}
return 1;
}
int overkill(char** args, char* root) {
int la = 0;
while(la < running_proc_num) {
if (processes[la].status == 0) {
int stat = kill(processes[la].pid, SIGINT);
if (stat < 0) {
perror("Error in Killing process");
return -1;
}
else {
fprintf(stdout, "Killed - %s %d\n", processes[la].pname, processes[la].pid);
}
}
la++;
}
return 1;
}
int shell_fg(char** args, char* root) {
int count = 0, i = 0;
while (args[i] != NULL) {
i++;
count++;
}
if (count >= 3) {
fprintf(stderr, "fg: Too many Arguments\n");
return -1;
}
else if (count <= 1) {
fprintf(stderr, "fg: Too few Arguments\n");
return -1;
}
else {
int num = atoi(args[1]);
if (num > running_proc_num) {
perror("Process doesn't exist");
return -1;
}
else {
// printf("%d %d %s\n", num, running_proc_num, processes[num - 1].pname);
kill(processes[num - 1].pid, SIGCONT);
current_running_proc = processes[num - 1].pid;
waitpid(-1, NULL, WUNTRACED);
processes[num - 1].status = 2;
}
}
}
int shell_kjob(char** args, char* root) {
int num_args = 0, i = 0;
while (args[i] != NULL) {
i++;
num_args++;
}
if (num_args >= 4) {
fprintf(stderr, "kjob: Too many Arguments\n");
return -1;
}
else if (num_args <= 2) {
fprintf(stderr, "kjob: Too few arguments\n");
return -1;
}
else {
int jno = atoi(args[1]);
int sig = atoi(args[2]);
if (jno > running_proc_num) {
perror("Process doesn't exist");
return -1;
}
else {
kill(processes[jno - 1].pid, sig);
}
}
return 1;
}
int shell_bg(char** args, char* root) {
int count = 0, i = 0;
while (args[i] != NULL) {
i++;
count++;
}
if (count >= 3) {
fprintf(stderr, "bg: Too many Arguments\n");
return -1;
}
else if (count <= 1) {
fprintf(stderr, "bg: Too few Arguments\n");
return -1;
}
else {
int jno = atoi(args[1]);
if (jno > running_proc_num) {
perror("Process Doesn't Exist");
return -1;
}
else {
kill(processes[jno - 1].pid, SIGTTIN);
kill(processes[jno - 1].pid, SIGCONT);
processes[jno - 1].status = 0;
processes[jno - 1].print_status = 0;
}
}
return 1;
}