-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbgfg.c
More file actions
84 lines (72 loc) · 2.21 KB
/
bgfg.c
File metadata and controls
84 lines (72 loc) · 2.21 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
#include "headers.h"
void bg(char *commands[], ll n){
// printf("bg\n");
if(n>2){
printf("\033[0;31mError: Too many arguments\033[0m\n");
return;
}
else if(n<2){
printf("\033[0;31mError: Too less arguments\033[0m\n");
return;
}
else{
int job_num;
job_num = atoi(commands[1]);
// printf("%d %d \n", job_num, njobs);
if(job_num > njobs || job_num <= 0){
printf("\033[0;31mError: No process found\033[0m\n");
return;
}
else{
kill(pids[job_num], SIGTTIN);
kill(pids[job_num], SIGCONT);
}
}
}
void fg(char *commands[], ll n){
// printf("fg\n");
if(n>2){
printf("\033[0;31mError: Too many arguments\033[0m\n");
return;
}
else if(n<2){
printf("\033[0;31mError: Too less arguments\033[0m\n");
return;
}
else{
int job_num;
int status;
job_num = atoi(commands[1]);
// printf("%d %d \n", job_num, njobs);
if(job_num > njobs || job_num <= 0){
printf("\033[0;31mError: No process found\033[0m\n");
return;
}
else{
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
tcsetpgrp(STDIN_FILENO, pids[job_num]);
curr_pid = pids[job_num];
strcpy(curr_name, names[job_num]);
// printf("fg: %d, %s\n", curr_pid, curr_name);
kill(pids[job_num], SIGCONT);
// Delete from jobs array
int pid = pids[job_num];
for (int i = 1; i <= njobs; i++)
{
if (pids[i] == pid)
{
for (int j = i; j < njobs; j++){
pids[j] = pids[j + 1];
strcpy(names[j], names[j+1]);
}
}
njobs -= 1;
}
waitpid(-1, NULL, WUNTRACED);
tcsetpgrp(STDIN_FILENO,getpgrp());
signal(SIGTTIN,SIG_DFL);
signal(SIGTTOU,SIG_DFL);
}
}
}