-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjob_ctrl.c
More file actions
356 lines (340 loc) · 10.6 KB
/
job_ctrl.c
File metadata and controls
356 lines (340 loc) · 10.6 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "job_ctrl.h"
void job_init() // init the job struct array
{
for(int i = 0; i < JOB_NUMBER; i++)
{
job_array[i].id = 0;
job_array[i].pid = 0;
job_array[i].name = NULL;
job_array[i].state = JOB_STATE_INIT;
}
}
int check_bg_fg(char** args) // determin foreground or background
{
int i;
if(args[0] == NULL)
return 0;
for(i = 0; args[i] != NULL; i++)
{
// 到命令末尾
}
if(strcmp(args[i - 1], "&") == 0) // if the last argument of command is '&', then it's background
{
args[i - 1] = NULL;
return BACKGROUND;
}
else // versus
return FOREGROUND;
}
void job_ctrl(struct command cmd) // the three commands of job_ctrl
{
if(strcmp(cmd.args[0], "fg") == 0)
{
shell_fg(cmd);
}
else if(strcmp(cmd.args[0], "bg") == 0)
{
shell_bg(cmd);
}
else if(strcmp(cmd.args[0], "jobs") == 0)
{
shell_jobs(cmd.args);
}
}
int shell_jobs(char** args) // print jobs
{
for(int i = 0; i < JOB_NUMBER; i++)
{
if(job_array[i].state == JOB_STATE_RUN) // according to the difference to print different infos
{
printf("[%d]+ 运行中\t", job_array[i].id);
printf("pid=%d\t", job_array[i].pid);
printf("state=%d\t", job_array[i].state);
printf("%s\n", job_array[i].name);
}
if(job_array[i].state == JOB_STATE_STOP)
{
printf("[%d]+ 已终止\t", job_array[i].id);
printf("pid=%d\t", job_array[i].pid);
printf("state=%d\t", job_array[i].state);
printf("%s\n", job_array[i].name);
clear_job(job_array[i].pid);
}
if(job_array[i].state == JOB_STATE_PAUSE)
{
printf("[%d]+ 已停止\t", job_array[i].id);
printf("pid=%d\t", job_array[i].pid);
printf("state=%d\t", job_array[i].state);
printf("%s\n", job_array[i].name);
}
}
return 0;
}
int shell_kill(char** args) // kill process
{
if(args[1] == NULL)
{
printf("kill: 用法: kill %%{id} or kill {pid}\n");
}
else if(args[1][0] == '%') // kill %id
{
int id = atoi(args[1] + 1);
if(id == 0)
{
printf("myshell:参数错误\n");
}
else
{
struct jobs* temp_job = get_job_byID(id);
if(temp_job != NULL)
{
kill(temp_job->pid, SIGKILL); // send SIGKILL signal
change_state(temp_job->pid, JOB_STATE_STOP); // set to JOB_STATE_STOP
}
else
{
printf("myshell:不存在这样的id\n");
}
}
}
else // kill pid
{
pid_t pid = atoi(args[1]);
if(pid == 0)
{
printf("myshell:参数错误\n");
}
else
{
struct jobs* temp_job = get_job_byPID(pid);
if(temp_job != NULL)
{
kill(temp_job->pid, SIGKILL); // send SIGKILL signal
change_state(temp_job->pid, JOB_STATE_STOP); // set to JOB_STATE_STOP
}
else
{
kill(pid, SIGKILL); // send signal even if the process not in the jobs anyway
}
}
}
return 0;
}
int shell_fg(struct command cmd) // set the background process to foreground and run it anyway
{
int ret;
if(cmd.args[1] == NULL)
{
printf("fg: 用法: fg %%{id} or fg {id}\n");
}
else if(cmd.args[1][0] == '%') // fg %id
{
int id = atoi(cmd.args[1] + 1);
if(id == 0)
{
printf("myshell:参数错误\n");
}
else
{
struct jobs* temp_job = get_job_byID(id); // get a job by its id
if(temp_job != NULL)
{
printf("[%d] %d\n", temp_job->id, temp_job->pid);
cmd.mode = FOREGROUND; // set to FOREGROUND
change_state(temp_job->pid, JOB_STATE_RUN); // set to run
kill(temp_job->pid, SIGCONT); // send SIGCONT to continue it
waitpid(temp_job->pid, &ret, WUNTRACED); // wait the process to stop
if((WIFSTOPPED(ret)))
{
handle_stop(cmd, temp_job->pid); // if receive the SIGTSTP, handle it
}
}
else
{
printf("myshell:不存在这样的id\n");
}
}
}
else // fg pid
{
int id = atoi(cmd.args[1]);
if(id == 0)
{
printf("myshell:参数错误\n");
}
else
{
struct jobs* temp_job = get_job_byID(id); // get a job by its id
if(temp_job != NULL)
{
printf("[%d] %d\n", temp_job->id, temp_job->pid);
cmd.mode = FOREGROUND;
change_state(temp_job->pid, JOB_STATE_RUN);
kill(temp_job->pid, SIGCONT);
waitpid(temp_job->pid, &ret, WUNTRACED); // wait the process to stop
if((WIFSTOPPED(ret)))
{
handle_stop(cmd, temp_job->pid); // if receive the SIGTSTP, handle it
}
}
else
{
printf("myshell:不存在这样的id\n");
}
}
}
return 0;
}
int shell_bg(struct command cmd) // set the process run in background
{
if(cmd.args[1] == NULL)
{
printf("bg: 用法: bg %%{id} or bg {id}\n");
}
else if(cmd.args[1][0] == '%') // bg %id
{
int id = atoi(cmd.args[1] + 1);
if(id == 0)
{
printf("myshell:参数错误\n");
}
else
{
struct jobs* temp_job = get_job_byID(id); // get a job by its id
if(temp_job != NULL)
{
printf("[%d] %d\n", temp_job->id, temp_job->pid); // print job info
kill(temp_job->pid, SIGCONT); // send SIGCONT and not wait it, so the process can run in background
cmd.mode = BACKGROUND;
change_state(temp_job->pid, JOB_STATE_RUN); // change job state
}
else
{
printf("myshell:不存在这样的id\n");
}
}
}
else // bg pid
{
int id = atoi(cmd.args[1]);
if(id == 0)
{
printf("myshell:参数错误\n");
}
else
{
struct jobs* temp_job = get_job_byID(id); // get a job by its id
if(temp_job != NULL)
{
printf("[%d] %d\n", temp_job->id, temp_job->pid);
kill(temp_job->pid, SIGCONT); // send SIGCONT and not wait it, so the process can run in background
cmd.mode = BACKGROUND;
change_state(temp_job->pid, JOB_STATE_RUN); // change job state
}
else
{
printf("myshell:不存在这样的id\n");
}
}
}
return 0;
}
struct jobs* get_new_job() // when a new job needed, return a new, free job
{
int max = 0;
int i;
for(i = 0; i < JOB_NUMBER; i++)
{
if(job_array[i].state == JOB_STATE_INIT) // find a free job
break;
}
for(int j = 0; j < JOB_NUMBER; j++)
{
if(job_array[j].state != JOB_STATE_INIT) // get a proper id
{
if(job_array[j].id > max)
max = job_array[j].id;
}
}
job_array[i].id = max + 1; // and set it
return job_array + i;
}
void handle_child(int sig) // handle
{
int status; //
pid_t pid;
if((pid = waitpid(-1, &status, WNOHANG)) > 0) // wait no hang
{ // so the children process can run in background
if(WIFEXITED(status)) // if exit
{
change_state(pid, JOB_STATE_PAUSE); // set to pause
}
}
}
void handle_stop(struct command cmd, pid_t pid) // when a job is stop, set and print some info
{
struct jobs* temp_job = get_job_byPID(pid); // get job by pid
if(temp_job == NULL)
{
temp_job = get_new_job();
temp_job->pid = pid;
temp_job->name = malloc(NAME_SIZE);
memset(temp_job->name, 0, NAME_SIZE);
for(int i = 0; cmd.args[i] != NULL; i++) // set name by its commands
{
strcat(temp_job->name, cmd.args[i]);
strcat(temp_job->name, " ");
}
}
cmd.mode = BACKGROUND;
change_state(pid, JOB_STATE_PAUSE); // change state to stop
printf("\n");
printf("[%d]+ 已停止\t", temp_job->id);
printf("%s\n", temp_job->name);
}
void change_state(pid_t pid, int state) // change the job state
{
for(int i = 0; i < JOB_NUMBER; i++)
{
if(job_array[i].pid == pid)
{
job_array[i].state = state;
break;
}
}
}
void clear_job(pid_t pid) // free the space taken by the job
{
struct jobs* temp_job = get_job_byPID(pid);
temp_job->id = 0;
temp_job->pid = 0;
free(temp_job->name);
temp_job->name = NULL;
temp_job->state = JOB_STATE_INIT;
}
void clear_job_all() // clear job_array
{
for(int i = 0; i < JOB_NUMBER; i++)
{
free(job_array[i].name);
}
free(job_array);
}
struct jobs* get_job_byID(int id) // get jobs by id
{
for(int i = 0; i < JOB_NUMBER; i++)
{
if(job_array[i].id == id)
return job_array + i;
}
return NULL;
}
struct jobs* get_job_byPID(pid_t pid) // get jobs by pid
{
for(int i = 0; i < JOB_NUMBER; i++)
{
if(job_array[i].pid == pid)
return job_array + i;
}
return NULL;
}