-
Notifications
You must be signed in to change notification settings - Fork 0
/
fg.c
58 lines (43 loc) · 1.47 KB
/
fg.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
#include "headers.h"
void fg(char** arg_list, int arg_num){
if (arg_num != 1){
printf("Invalid number of arguments!\n");
return;
}
int job_number = atoi(arg_list[0]);
if (job_number < 1 || job_number > running_background_processes->size){
printf("Invalid job number!\n");
return;
}
int cur_num = 1;
DLLNode cur_node = running_background_processes->start_node->next_node;
while(cur_node != NULL && cur_num < job_number){
cur_num++;
cur_node = cur_node->next_node;
}
pid_t job_pid = cur_node->element->process_pid;
// ignoring signals
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
// setting group pid
setpgid(job_pid, getpgid(0));
// making foreground process
tcsetpgrp(STDIN_FILENO, job_pid);
if (kill(job_pid, SIGCONT) < 0){
perror("Could not send signal");
delete_from_dll(running_background_processes, job_pid);
// setting process specified by terminal again foreground
tcsetpgrp(STDIN_FILENO, shell_pid);
// returning to default signals
signal(SIGTTIN, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
return;
}
waitpid(job_pid, NULL, WUNTRACED);
delete_from_dll(running_background_processes, job_pid);
// setting process specified by terminal again foreground
tcsetpgrp(STDIN_FILENO, shell_pid);
// returning to default signals
signal(SIGTTIN, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
}