-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfg.c
More file actions
52 lines (45 loc) · 1.26 KB
/
fg.c
File metadata and controls
52 lines (45 loc) · 1.26 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
#include "headers.h"
void fg(int argc, char **argv)
{
if (argc > 2)
{
printf("fg: too many arguments\n");
return;
}
else if (argc != 2)
{
printf("fg: less arguments\n");
return;
}
int job = atoi(argv[1]);
NodePtr temp = Get_Node_job(job);
if (temp == NULL)
{
printf("Error: job not found\n");
return;
}
pid_t pid = temp->pid;
if (pid <= 0)
{
perror("Invalid command");
return;
}
signal(SIGTTIN, SIG_IGN); //ignoring the default behaviour
signal(SIGTTOU, SIG_IGN); //else it would stop the process
tcsetpgrp(STDIN_FILENO, pid); //changing the forground process group to child else terminal process don't work
if (kill(pid, SIGCONT) < 0)//sigcont signal resumes the process where it stopped
{
perror("Can't send signal");
}
int status;
waitpid(pid, &status, WUNTRACED);//WUNTRACED used
if (WIFSTOPPED(status))//cntrl-z
{
kill(pid, SIGSTOP);//send pause signal
}
else
Delete_Node(pid);
tcsetpgrp(STDIN_FILENO, getpgid(getpid())); //changing the forground process group to parent
signal(SIGTTIN, SIG_DFL);//do the default action
signal(SIGTTOU, SIG_DFL);//do the default action
}