-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.c
executable file
·406 lines (369 loc) · 7.14 KB
/
signal.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "threads/thread.h"
#include "threads/signal.h"
#include <stdio.h>
#include <string.h>
#include "threads/flags.h"
#include "threads/interrupt.h"
#include "threads/intr-stubs.h"
#include "threads/palloc.h"
#include "threads/switch.h"
#include "threads/synch.h"
#include "threads/vaddr.h"
#include "threads/malloc.h"
void sigchild_handler(tid_t id){
struct thread* t = get_thread(id);
ASSERT(t!=NULL);
ASSERT(is_thread(t));
int temp = t->mask & (1 << 0);
if (temp==0)
{
printf("Sigchild ignored\n");
return;
}
(t->died_so_far) = (t->died_so_far) + 1;
//printf("Thread Name : %s",t->name);
printf("Total Number of Children created so far : %d\n",t->created_so_far);
printf("Total Number of Children still alive : %d\n",(t->created_so_far)-(t->died_so_far));
}
void sigkill_handler(tid_t id){
struct thread* t = get_thread(id);
ASSERT(t!=NULL);
ASSERT(is_thread(t));
printf("Thread terminated by SIG_KILL");
thread_exit();
}
void sigcpu_handler(tid_t id){
struct thread* t = get_thread(id);
ASSERT(t!=NULL);
ASSERT(is_thread(t));
int temp = t->mask & (1 << 1);
if (temp==0)
{
printf("Sigcpu ignored\n");
return;
}
if(t->cpu_limit_over){
printf("Thread is terminating : lifetime %lld : mylife %lld\n",t->lifetime,t->mylife);
thread_exit();
}
}
void sigunblock_handler(tid_t id){
struct thread* t = get_thread(id);
ASSERT(t!=NULL);
ASSERT(is_thread(t));
int temp = t->mask & (1 << 2);
if (temp==0)
{
printf("Sigunblock ignored\n");
return;
}
if(t->status == THREAD_BLOCKED){
printf("Unblocking thread\n");
thread_unblock(t);
}
}
void siguser_handler(tid_t id){
struct thread* t = get_thread(id);
ASSERT(t!=NULL);
ASSERT(is_thread(t));
int temp = t->mask & (1 << 3);
if (temp==0)
{
printf("Siguser ignored\n");
return;
}
//tid_t tig;
struct list_elem* lelem;
for(lelem = list_begin(&t->signal_list) ; lelem != list_end(&t->signal_list) ; lelem = list_next(lelem)){
mysignal* new = list_entry(lelem,mysignal,elem);
if(new->sigtype == SIG_USER){
printf("Thread %d sent this signal to thread %d\n",new->sentid,id);
break;
}
}
}
int _signal(int signum, int sigtype){
struct thread* t = thread_current();
if(sigtype == SIG_IGN){
if(signum != SIG_KILL){
if(signum == SIG_CHLD){
t->mask = (t->mask) & (~(1<<0));
}else if(signum == SIG_CPU){
t->mask = (t->mask) & (~(1<<1));
}else if(signum == SIG_UNBLOCK){
t->mask = (t->mask) & (~(1<<2));
}else if(signum == SIG_USER){
t->mask = (t->mask) & (~(1<<3));
}
}
}else if(sigtype == SIG_DFL){
if(signum != SIG_KILL){
if(signum == SIG_CHLD){
t->mask = (t->mask) | ((1<<0));
}else if(signum == SIG_CPU){
t->mask = (t->mask) | ((1<<1));
}else if(signum == SIG_UNBLOCK){
t->mask = (t->mask) | ((1<<2));
}else if(signum == SIG_USER){
t->mask = (t->mask) | ((1<<3));
}
}
}
return 1;
}
int kill(tid_t id, int signum){
struct thread* t = get_thread(id);
// ASSERT(t!=NULL);
if (t==NULL)
{
return 0;
}
ASSERT(is_thread(t));
int val = 1;
// if(signum != SIG_KILL){
// if(signum == SIG_CHLD){
// val = (t->mask) & (1<<0);
// }else if(signum == SIG_CPU){
// val = (t->mask) & (1<<1);
// }else if(signum == SIG_UNBLOCK){
// val = (t->mask) & (1<<2);
// }else if(signum == SIG_USER){
// val = (t->mask) & (1<<3);
// }
// }
if(val == 1){
int stat = 0;
struct list_elem* lelem1;
struct thread* threadcurr = thread_current();
for(lelem1 = list_begin(&threadcurr->child_list) ; lelem1 != list_end(&threadcurr->child_list) ; lelem1 = list_next(lelem1)){
struct thread* ti = list_entry(lelem1,struct thread,childelem);
if(ti->tid == id){
stat = 1;
break;
}
}
if(signum == SIG_CPU){
t->cpu_limit_over = 1;
//printf("limit set\n");
return 0;
}
if(signum == SIG_UNBLOCK){
push_into_unblock(id);
return 0;
}
if(signum == SIG_KILL && stat == 0)
return 0;
mysignal *newsignal;
newsignal = (mysignal*)malloc(sizeof(mysignal));
newsignal->sentid = thread_current()->tid;
newsignal->receiveid = t->tid;
newsignal->sigtype = signum;
if(signum == SIG_CHLD)
list_push_back(&t->signal_list,&newsignal->elem);
else{
struct list_elem *lelem;
for(lelem = list_begin(&t->signal_list) ; lelem != list_end(&t->signal_list);){
mysignal* sig = list_entry(lelem,mysignal,elem);
if(sig->sigtype == signum){
lelem = list_remove(lelem);
break;
}else{
lelem = list_next(lelem);
}
}
list_push_back(&t->signal_list,&newsignal->elem);
}
}
return 1;
}
int sigemptyset(sigset * set)
{
if (set==NULL)
{
return 0;
}
set->sigchild = 0;
set->sigkill = 0;
set->sigcpu = 0;
set->siguser = 0;
set->sigunblock = 0;
return 1;
}
int sigfillset(sigset * set)
{
if (set==NULL)
{
return 0;
}
set->sigchild = 1;
set->sigkill = 1;
set->sigcpu = 1;
set->siguser = 1;
set->sigunblock = 1;
return 1;
}
int sigaddset(sigset * set, int signum)
{
if (set==NULL)
{
return 0;
}
if (signum == SIG_CHLD)
{
set->sigchild = 1;
return 1;
}
else if (signum == SIG_KILL)
{
set->sigkill = 1;
return 1;
}
else if (signum == SIG_UNBLOCK)
{
set->sigunblock = 1;
return 1;
}
else if (signum == SIG_CPU)
{
set->sigcpu = 1;
return 1;
}
else if (signum == SIG_USER)
{
set->siguser = 1;
return 1;
}
return 0;
}
int sigdelset(sigset * set, int signum)
{
if (set==NULL)
{
return 0;
}
if (signum == SIG_CHLD)
{
set->sigchild = 0;
return 1;
}
else if (signum == SIG_KILL)
{
set->sigkill = 0;
return 1;
}
else if (signum == SIG_UNBLOCK)
{
set->sigunblock = 0;
return 1;
}
else if (signum == SIG_CPU)
{
set->sigcpu = 0;
return 1;
}
else if (signum == SIG_USER)
{
set->siguser = 0;
return 1;
}
return 0;
}
int sigprocmask(int how, const sigset * set ,sigset * oldset)
{
if (set == NULL)
{
return 0;
}
struct thread* t = thread_current();
ASSERT(t!=NULL);
ASSERT(is_thread(t));
if (oldset != NULL)
{
oldset->sigchild = 0;
oldset->sigkill = 0;
oldset->sigcpu = 0;
oldset->siguser = 0;
oldset->sigunblock = 0;
if ((t->blockmask & (1<<0))==0)
{
oldset->sigchild = 1;
}
if ((t->blockmask & (1<<1))==0)
{
oldset->sigcpu = 1;
}
if ((t->blockmask & (1<<2))==0)
{
oldset->sigunblock = 1;
}
if ((t->blockmask & (1<<3))==0)
{
oldset->siguser = 1;
}
}
int a = (1<<4)-1;
if (how == SIG_BLOCK)
{
if (set->sigchild==1)
{
a = a & (~(1<<0));
}
if (set->sigcpu==1)
{
a = a & (~(1<<1));
}
if (set->sigunblock==1)
{
a = a & (~(1<<2));
}
if (set->siguser==1)
{
a = a & (~(1<<3));
}
t->blockmask = (t->blockmask) & a;
return 1;
}
else if (how == SIG_UNBLOCK_)
{
if (set->sigchild==0)
{
a = a & (~(1<<0));
}
if (set->sigcpu==0)
{
a = a & (~(1<<1));
}
if (set->sigunblock==0)
{
a = a & (~(1<<2));
}
if (set->siguser==0)
{
a = a & (~(1<<3));
}
t->blockmask = (t->blockmask) | a;
return 1;
}
else if (how == SIG_SETMASK)
{
if (set->sigchild==1)
{
a = a & (~(1<<0));
}
if (set->sigcpu==1)
{
a = a & (~(1<<1));
}
if (set->sigunblock==1)
{
a = a & (~(1<<2));
}
if (set->siguser==1)
{
a = a & (~(1<<3));
}
t->blockmask = a;
return 1;
}
return 0;
}