-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal_jobs.c
290 lines (253 loc) · 4.98 KB
/
signal_jobs.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*Rajat Arora 2013A7PS104P*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
/*struct for background processes*/
struct background
{
pid_t id;
int bit;
char *name;
struct background *next;
};
typedef struct background background;
/*utility function to print background jobs*/
void print_jobs(struct background *head)
{
head = head->next;
while(head!=NULL)
{
if(head->bit == 1)
printf("%d running %s \n",head->id,head->name);
else
printf("%d stopped %s \n",head->id,head->name);
head = head->next;
}
}
/*function to stop a job according to pid*/
void stop_job(struct background *head, pid_t pid)
{
int output;
output = kill(pid,SIGTSTP);
if(output == -1)
{
printf("no process with given pid\n");
}
head = head->next;
while(head!=NULL)
{
if(head->id == pid)
{
head->bit = 0;
break;
}
head = head->next;
}
}
/*function to start a stopped job*/
void start_job(struct background *head, pid_t pid)
{
kill(pid,SIGCONT);
head = head->next;
while(head!=NULL)
{
if(head->id == pid)
{
head->bit = 1;
break;
}
head = head->next;
}
}
/*function to delete terminated functions*/
delete_process(struct background *head, pid_t pid,struct background **node)
{
struct background* prev = (struct background*)malloc(sizeof(struct background));
prev = head;
head = head->next;
while(head!=NULL)
{
if(head->id == pid)
{
prev->next=head->next;
if(head->next == NULL)
{
*node = prev;
}
head->next= NULL;
free(head);
break;
}
prev = head;
head = head->next;
}
}
int main(void)
{
int status;
char *input;
char *job;
char *init;
int background[1000];
int count = 0;
pid_t w,pid,fore;
struct background *head;
struct background *node;
head = (struct background*)malloc(sizeof(struct background));
node = head;
/*block signals*/
signal(SIGQUIT,SIG_IGN);
signal(SIGINT,SIG_IGN);
signal(SIGTSTP,SIG_IGN);
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
while(1)
{
/*initialisiing foreground process group*/
tcsetpgrp(0,getpid());
int flag = 0;
input = (char*)malloc(100*sizeof(char));
printf("command<<");
fgets(input,100,stdin);
init = input;
/*if user presses newline continue*/
if(strcmp(input,"\n") == 0)
{
continue;
}
if(input[strlen(input)-1] == '\n')
input[strlen(input)-1] = '\0';
/*printf("%s",input);*/
if(input[strlen(input)-1] == '&'){
input[strlen(input)-1] = '\0';
/*flag indicates if process is background*/
flag = 1;
}
if (strcmp(input,"jobs") == 0)
{
int stat;
pid_t changed;
while(1)
{
/*print_jobs(head);*/
/*check for terminated background jobs*/
changed = waitpid(-1,&stat,WNOHANG);
if(changed == 0||changed == -1)
break;
/*printf("%d\n",changed);*/
delete_process(head,changed,&node);
}
print_jobs(head);
continue;
}
if(strcmp(input,"quit") == 0)
{
break;
}
char *args[20];
int k;
for(k = 0;k<20;k++)
{
args[k] = NULL;
}
char *token;
char s[] =" ";
token = strtok(input,s);
int i = 0,j;
/*tokenising the command*/
if(token == NULL)
{
job = input;
}
else
{
job = token;
if(strcmp(job,"stop") == 0)
{
token = strtok(NULL,s);
/*printf("%s",token);*/
pid_t proc = atoi(token);
stop_job(head,proc);
continue;
}
if(strcmp(job,"start") == 0)
{
token = strtok(NULL,s);
pid_t proc = atoi(token);
start_job(head,proc);
continue;
}
args[i] = token;
token = strtok(NULL,s);
while(token!=NULL)
{
i++;
args[i] = token;
token = strtok(NULL,s);
}
}
{ pid = fork();
if(!flag)
fore = pid;
if(pid == 0)
{
if(flag)
{
signal(SIGCHLD,SIG_IGN);
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
setpgid(0,0);
}
else{
signal(SIGTSTP,SIG_DFL);
signal(SIGTTOU, SIG_IGN);
setpgid(getpid(),getpid());
tcsetpgrp(0,getpgid(getpid()));
}
if(i ==0){
execlp(input,input,(char *)0);
perror ("error in execlp\n");
}
else{
/*printf("%s %s\n",job,args[1]);*/
execvp(job,args);
perror ("error in execvp\n");
}
}
else
{
if(flag == 1)
{
/*store process initiated in a linked list*/
int stat;
node->next = (struct background*)malloc(sizeof(struct background));
node = node->next;
node->bit = 1;
node->id = pid;
node->name = (char*)malloc(100*sizeof(char));
node->name = input;
/*while(waitpid(-1,&stat,WNOHANG|WUNTRACED));*/
}
else
{
/*if process is foreground, wait*/
if(pid == fore){
do
{
w = waitpid(-1,&status,WUNTRACED);
if(WIFSTOPPED(status))
{
kill(pid,SIGSTOP);
}
}while(!WIFEXITED(status)&&!WIFSIGNALED(status)&&!WIFSTOPPED(status));
/*setting shell back to foreground*/
tcsetpgrp(0,getpgid(0));}
}
}
}
}
}