-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfg.c
120 lines (90 loc) · 2.83 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "def.h"
int zFlag = 0, cFlag = 0;
void handleC(int signal)
{
cFlag = 1;
return;
}
void handleZ(int signal)
{
zFlag = 1;
return;
}
void fg(char *token, process proc[], int **proc_size)
{
// printf("%s\n", token);
token = strtok(NULL, " \t");
if(token == NULL)
{
// printf("*****\n");
perror("Invalid number of arguments");
return;
}
char jobNumber[MAX_SIZE];
strcpy(jobNumber, token);
token = strtok(NULL, " \t");
if(token != NULL)
{
perror("Invalid number of arguments");
return;
}
int job = atoi(jobNumber);
int processesSoFar = 0;
for(int i = 0; i < MAX_SIZE; i++)
{
if(proc[i].pid != -1)
{
processesSoFar++;
if(processesSoFar == job)
{
// kill(proc[i].pid, SIGCONT); // If the process was in stopped state, then it was causing problems
int savePid = proc[i].pid;
int shellPid = getpid();
proc[i].pid = -1;
// signal(SIGINT, handleC);
// signal(SIGTSTP, handleZ);
// signal(SIGTTOU, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
tcsetpgrp(0, getpgid(savePid));
kill(savePid, SIGCONT);
int status;
waitpid(savePid, &status, WUNTRACED);
// while(waitpid(savePid, &status, WUNTRACED) != savePid)
// {
// // wait
// if(cFlag == 1)
// {
// kill(savePid, SIGINT);
// cFlag = 0;
// break;
// }
// if(zFlag == 1)
// {
// printf("%%\n");
// setpgid(savePid, savePid);
// proc[**proc_size].pid = savePid;
// strcpy(proc[**proc_size].name, proc[i].name);
// (**proc_size)++;
// kill(savePid, SIGSTOP);
// zFlag = 0;
// break;
// }
// }
// signal(SIGTTOU, SIG_IGN);
tcsetpgrp(0, shellPid);
signal(SIGTTOU, SIG_DFL);
signal(SIGTTIN, SIG_DFL);
if(WIFSTOPPED(status))
{
kill(savePid, SIGSTOP);
proc[**proc_size].pid = savePid;
strcpy(proc[**proc_size].name, proc[i].name);
(**proc_size)++;
}
return;
}
}
}
printf("Invalid job number\n");
}