-
Notifications
You must be signed in to change notification settings - Fork 0
/
fg.c
51 lines (48 loc) · 1.12 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
#include "fg.h"
void change_foreground(int cpid)
{
foregroundProcessPid = cpid;
int status;
kill(cpid, SIGCONT);
waitpid(cpid, &status, WUNTRACED);
int pd=getpid();
if (tcsetpgrp(STDIN_FILENO, getpgid(pd)) < 0)
{
perror("cant get terminal back");
}
if (WIFEXITED(status))
{
processList = removeProcess(processList, cpid);
}
else if (WIFSTOPPED(status))
{
printf("\n");
struct ProcessInfo *x = findProcess(processList, cpid);
printf(" %d ", x->pid);
int sz=x->sz;
for (int i = 0; i < x->sz; i++)
{
printf("%s ", x->name[i]);
}
printf("Stopped\n");
}
else if (WIFSIGNALED(status))
{
processList = removeProcess(processList, cpid);
}
}
void foreground(char **tokens, int num_tokens)
{
if (num_tokens != 2)
{
fprintf(stderr, "fg: invalid format! correct format is fg <pid>\n");
return;
}
int t = (int)strtol(tokens[1], NULL, 10);
if (t <= 0)
{
fprintf(stderr, "pid does not exist \n");
return;
}
change_foreground(t);
}