-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfg.c
More file actions
56 lines (55 loc) · 1.31 KB
/
fg.c
File metadata and controls
56 lines (55 loc) · 1.31 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
#include "headers.h"
#include "fg.h"
void fg_implementation(int parts, char **args)
{
if (parts > 1)
{
printf("fg: too many arguments \n");
return;
}
if (parts < 1)
{
printf("fg: too few arguments \n");
return;
}
int proc_shell_id = atoi(args[0]);
if (proc_shell_id < 0)
{
printf("fg: invalid proc shell id \n");
return;
}
pid_t pid = get_pid_from_id(proc_shell_id, head);
if (pid == -1)
{
printf("fg: no process running for the given shell id \n");
return;
}
// give terminal control to the background process.
int status = 0;
pid_t shell_pid = getpgid(0);
signal(SIGTTOU, SIG_IGN);
tcsetpgrp(STDIN_FILENO, pid);
kill(pid, SIGCONT);
CUR_FG = 1;
int idx = traverse(pid, head);
change_status(idx, head, 1);
FG_RUN = 1;
waitpid(pid, &status, WUNTRACED);
tcsetpgrp(STDIN_FILENO, shell_pid);
signal(SIGTTOU, SIG_DFL);
if (WIFSTOPPED(status))
{
// ctrl+z trigger
CUR_FG = 0;
change_status(idx, head, 0);
return;
}
// everything else
idx = traverse(pid, head);
if (idx != -1)
{
delete(idx, head);
}
// printf("fg: process with pid [%d] has suspended normally / on ctrl-c trigger \n", pid);
return;
}