-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbg.c
44 lines (34 loc) · 1.02 KB
/
bg.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
#include "shell.h"
#include "utils.h"
#include "process.h"
#include "bg.h"
void handle_bg(int argc, char** argv) {
if (argc > 0 && strcmp(argv[argc - 1], "&") == 0) {
argc--;
}
if (argc != 1) {
fprintf(stderr, "Could not run background process: Invalid arguments.\n");
return;
}
int job_num = strtol(argv[0], NULL, 0);
if (job_num <= 0) {
fprintf(stderr, "Could not run background process: Invalid job number.\n");
return;
}
bg(job_num);
}
bool bg(int job_num) {
proc* p = get_data_by_id(processes, job_num);
if (p == NULL) {
fprintf(stderr, "Could not run background process: Process not found.\n");
return false;
}
pid_t child_pid = p->pid;
// Don't delete from processes list, as it is still in background
if (kill(child_pid, SIGCONT) < 0) {
perror("Could not run background process");
return false;
}
printf("[%d] %s %d running in background\n", job_num, p->pname, p->pid);
return true;
}