-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfg-bg.c
More file actions
163 lines (139 loc) · 4.09 KB
/
fg-bg.c
File metadata and controls
163 lines (139 loc) · 4.09 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "headers.h"
void fg(char* command)
{
char* token = strtok(command," \t");
if(strcmp(token,"bg") != 0)
{
if(debug) printf("the cmd is not bg \n");
}
token = strtok(NULL," \t");
int args = 0;
char jid[MY_LEN];
while(token != NULL)
{
if(args == 0)
{
if(debug) perror("jobid arguement\n");
args++;
strcpy(jid,token);
}
else
{
fprintf(stderr,"Too many arguements for bg\n");
return;
}
token = strtok(NULL," \t");
}
if (args == 0)
{
fprintf(stderr, "Error: enter valid jobIndex\n");
return;
}
int jobIndex = atoi(jid);
if (jobIndex > bgpno)
{
fprintf(stderr, "No such process\n");
return;
}
// Getting process information from child pool
pid_t pid = bgpid[jobIndex - 1];
char pName[MY_LEN];
strcpy(pName, bgp[jobIndex - 1]);
// // Removing process from child pool because it is being moved to the foreground
// removeChild(pid);
bexists[jobIndex - 1] = 0;
// // Ignoring required signals from the shell
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
// // Setting foreground process flag to 1 to indicate that there is a foreground process running
int fgP = 1;
// // Giving the specified process terminal control and checking for errors
if(tcsetpgrp(STDIN_FILENO, getpgid(pid)))
{
fprintf(stderr, "Could not give terminal control to job %d\n", jobIndex);
// Control couldn't be give to the child process, we want the shell to continue
// Set signal handlers to default
signal(SIGTTOU, SIG_DFL);
signal(SIGTTIN, SIG_DFL);
// Print error message and return from the function.
return ;
}
// // Telling the job to resume and handling errors (same as above)
if(kill(pid, SIGCONT))
{
fprintf(stderr, "Unable to resume job %d\n", jobIndex);
signal(SIGTTOU, SIG_DFL);
signal(SIGTTIN, SIG_DFL);
return ;
}
// // Waiting for foreground process to be stopped or to terminate.
// // If it is stopped (Ctrl+Z), add it to the child pool
int status;
if(waitpid(pid, &status, WUNTRACED) > 0)
if(WIFSTOPPED(status))
{
strcpy(bgp[bgpno], pName);
if (debug)
printf("bgp[%d] = %s\n", bgpno, bgp[bgpno]);
bgpid[bgpno] = pid;
if (debug)
printf("bgpid[%d] = %d\n", bgpno, bgpid[bgpno]);
bexists[bgpno] = 1;
if (debug)
printf("bexists[%d] = %d\n", bgpno, bexists[bgpno]);
bgpno++;
}
// exitCode = WEXITSTATUS(status) ? -1 : 1;
// // Since the foreground process has been exitted from, set the flag back to 0
// fgP = 0;
// // Return terminal control back to the shell and perform error handling
// // If control cannot be returned, quit the program.
if(tcsetpgrp(STDIN_FILENO, getpgid(0)))
{
fprintf(stderr, "Could not return terminal controll to the shell. Exitting the shell\n");
return;
}
// // Restore default signal handlers
signal(SIGTTOU, SIG_DFL);
signal(SIGTTIN, SIG_DFL);
return;
}
void bg(char* command)
{
char* token = strtok(command," \t");
if(strcmp(token,"bg") != 0)
{
if(debug) printf("the cmd is not bg \n");
}
token = strtok(NULL," \t");
int args = 0;
char jid[MY_LEN];
while(token != NULL)
{
if(args == 0)
{
if(debug) perror("jobid arguement\n");
args++;
strcpy(jid,token);
}
else
{
fprintf(stderr,"Too many arguements for bg\n");
return;
}
token = strtok(NULL," \t");
}
if (args == 0)
{
fprintf(stderr, "Error: enter valid jobIndex\n");
return;
}
int jobIndex = atoi(jid) - 1;
if (jobIndex > bgpno)
{
fprintf(stderr, "No such process\n");
return;
}
kill(bgpid[jobIndex], SIGCONT);
return;
}