-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathass2.patch
More file actions
1256 lines (1202 loc) · 30.5 KB
/
ass2.patch
File metadata and controls
1256 lines (1202 loc) · 30.5 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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/Makefile b/Makefile
index 09d790c..21b363a 100644
--- a/Makefile
+++ b/Makefile
@@ -181,6 +181,7 @@ UPROGS=\
_usertests\
_wc\
_zombie\
+ _sanity-check\
fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)
@@ -252,7 +253,7 @@ EXTRA=\
ln.c ls.c mkdir.c rm.c stressfs.c usertests.c wc.c zombie.c\
printf.c umalloc.c\
README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list\
- .gdbinit.tmpl gdbutil\
+ .gdbinit.tmpl gdbutil sanity-check\
dist:
rm -rf dist
diff --git a/defs.h b/defs.h
index 82fb982..338df5f 100644
--- a/defs.h
+++ b/defs.h
@@ -9,6 +9,7 @@ struct spinlock;
struct sleeplock;
struct stat;
struct superblock;
+struct sigaction;
// bio.c
void binit(void);
@@ -107,7 +108,7 @@ int cpuid(void);
void exit(void);
int fork(void);
int growproc(int);
-int kill(int);
+int kill(int, int);
struct cpu* mycpu(void);
struct proc* myproc();
void pinit(void);
@@ -120,7 +121,11 @@ void userinit(void);
int wait(void);
void wakeup(void*);
void yield(void);
-
+uint sigprocmask(uint);
+int sigaction(int, const struct sigaction*, struct sigaction*);
+void myStart(void);
+void myEnd(void);
+void sigret(void);
// swtch.S
void swtch(struct context**, struct context*);
@@ -188,3 +193,4 @@ void clearpteu(pde_t *pgdir, char *uva);
// number of elements in fixed-size array
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
+
diff --git a/exec.c b/exec.c
index b40134f..aa91f99 100644
--- a/exec.c
+++ b/exec.c
@@ -92,13 +92,24 @@ exec(char *path, char **argv)
if(*s == '/')
last = s+1;
safestrcpy(curproc->name, last, sizeof(curproc->name));
-
+ // MY_CODE - 2.1.2
+ for(int k=0; i<SIGNALS_SIZE; i++)
+ {
+ if(curproc->signal_handlers[k] != (void*)SIG_IGN)
+ {
+ curproc->signal_handlers[k] = SIG_DFL;
+ }
+ }
+ // MY_CODE - 2.1.2
// Commit to the user image.
oldpgdir = curproc->pgdir;
curproc->pgdir = pgdir;
curproc->sz = sz;
curproc->tf->eip = elf.entry; // main
curproc->tf->esp = sp;
+ // MY_CODE signals handlers
+
+ // /MY_CODE signals handlers
switchuvm(curproc);
freevm(oldpgdir);
return 0;
diff --git a/kill.c b/kill.c
index 364f6af..cff9462 100644
--- a/kill.c
+++ b/kill.c
@@ -12,6 +12,6 @@ main(int argc, char **argv)
exit();
}
for(i=1; i<argc; i++)
- kill(atoi(argv[i]));
+ kill(atoi(argv[i]), 9);
exit();
}
diff --git a/param.h b/param.h
index a7e90ef..866d20a 100644
--- a/param.h
+++ b/param.h
@@ -12,3 +12,4 @@
#define NBUF (MAXOPBLOCKS*3) // size of disk block cache
#define FSSIZE 1000 // size of file system in blocks
+
diff --git a/proc.c b/proc.c
index d3c8b17..14b0a0a 100644
--- a/proc.c
+++ b/proc.c
@@ -20,6 +20,14 @@ extern void trapret(void);
static void wakeup1(void *chan);
+int
+abs(int num)
+{
+ int result = num;
+ if(num<0)
+ result = num * -1;
+ return result;
+}
void
pinit(void)
{
@@ -67,16 +75,18 @@ myproc(void) {
-
+// MY_CODE 3.1
int
allocpid(void)
{
int pid;
- acquire(&ptable.lock);
- pid = nextpid++;
- release(&ptable.lock);
+ do{
+ pid = nextpid;
+ } while(!cas(&nextpid,pid,pid+1));
return pid;
}
+// /MY_CODE 3.1
+
//PAGEBREAK: 32
// Look in the process table for an UNUSED proc.
@@ -88,20 +98,26 @@ allocproc(void)
{
struct proc *p;
char *sp;
-
- acquire(&ptable.lock);
-
+// MY_CODE 3.2
+ pushcli();
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
+ {
if(p->state == UNUSED)
- goto found;
-
- release(&ptable.lock);
+ {
+ //if(cas(&p->state, UNUSED, EMBRYO))
+ goto found;
+ cprintf("stuck\n");
+ }
+ }
+ popcli();
return 0;
-found:
- p->state = EMBRYO;
- release(&ptable.lock);
+found:
+ while(!(cas(&p->state, UNUSED, EMBRYO)));
+ //cprintf("found UNUSED proc\n");
+ popcli();
+// MY_CODE 3.2
p->pid = allocpid();
// Allocate kernel stack.
@@ -109,6 +125,7 @@ found:
p->state = UNUSED;
return 0;
}
+
sp = p->kstack + KSTACKSIZE;
// Leave room for trap frame.
@@ -124,7 +141,15 @@ found:
p->context = (struct context*)sp;
memset(p->context, 0, sizeof *p->context);
p->context->eip = (uint)forkret;
-
+ // MY_CODE - 2.1.2
+ for(int i=0; i<SIGNALS_SIZE; i++)
+ {
+ p->signal_handlers[i] = SIG_DFL;
+ p->signals_masks[i] = 0;//UNBLOCKED
+ }
+ p->pending_signals = 0;
+ p->signal_mask = 0;
+ // /MY_CODE 2.1.2
return p;
}
@@ -140,7 +165,7 @@ userinit(void)
extern char _binary_initcode_start[], _binary_initcode_size[];
p = allocproc();
-
+ //cprintf("allocated proc - userinit\n");
initproc = p;
if((p->pgdir = setupkvm()) == 0)
panic("userinit: out of memory?");
@@ -157,16 +182,14 @@ userinit(void)
safestrcpy(p->name, "initcode", sizeof(p->name));
p->cwd = namei("/");
-
+ p->stopped = 0;
// this assignment to p->state lets other cores
// run this process. the acquire forces the above
// writes to be visible, and the lock is also needed
// because the assignment might not be atomic.
- acquire(&ptable.lock);
-
- p->state = RUNNABLE;
-
- release(&ptable.lock);
+ pushcli();
+ p->state=RUNNABLE;
+ popcli();
}
// Grow current process's memory by n bytes.
@@ -212,6 +235,14 @@ fork(void)
np->state = UNUSED;
return -1;
}
+ // MY_CODE - 2.1.2
+ np->signal_mask = curproc->signal_mask;
+ for(int k=0; k<SIGNALS_SIZE; k++)
+ {
+ np->signal_handlers[k] = curproc->signal_handlers[k];
+ np->signals_masks[k] = curproc->signals_masks[k];
+ }
+ // MY_CODE - 2.1.2
np->sz = curproc->sz;
np->parent = curproc;
*np->tf = *curproc->tf;
@@ -227,13 +258,9 @@ fork(void)
safestrcpy(np->name, curproc->name, sizeof(curproc->name));
pid = np->pid;
-
- acquire(&ptable.lock);
-
+ //not have to just for fun
np->state = RUNNABLE;
- release(&ptable.lock);
-
return pid;
}
@@ -243,12 +270,14 @@ fork(void)
void
exit(void)
{
+
struct proc *curproc = myproc();
struct proc *p;
int fd;
if(curproc == initproc)
panic("init exiting");
+ //cprintf("entered exit for pid: %d \n", curproc->pid);
// Close all open files.
for(fd = 0; fd < NOFILE; fd++){
@@ -263,9 +292,11 @@ exit(void)
end_op();
curproc->cwd = 0;
- acquire(&ptable.lock);
-
+ //acquire(&ptable.lock);
+ pushcli();
// Parent might be sleeping in wait().
+ //turning parent into -ZOMBIE
+ cas(&curproc->state, RUNNING, -ZOMBIE);
wakeup1(curproc->parent);
// Pass abandoned children to init.
@@ -278,7 +309,6 @@ exit(void)
}
// Jump into the scheduler, never to return.
- curproc->state = ZOMBIE;
sched();
panic("zombie exit");
}
@@ -292,9 +322,14 @@ wait(void)
int havekids, pid;
struct proc *curproc = myproc();
- acquire(&ptable.lock);
+ //acquire(&ptable.lock);
+ pushcli();
for(;;){
// Scan through table looking for exited children.
+ // put proc on -sleeping so no one will edit his info
+ cas(&curproc->state, RUNNING, -SLEEPING);
+ //
+ curproc->chan=(void*)curproc;
havekids = 0;
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->parent != curproc)
@@ -310,20 +345,26 @@ wait(void)
p->parent = 0;
p->name[0] = 0;
p->killed = 0;
- p->state = UNUSED;
- release(&ptable.lock);
+ cas(&p->state, ZOMBIE, UNUSED);
+ cas(&curproc->state, -SLEEPING, RUNNING);
+ //p->state = UNUSED;
+ //release(&ptable.lock);
+ popcli();
return pid;
}
}
-
+
// No point waiting if we don't have any children.
if(!havekids || curproc->killed){
- release(&ptable.lock);
+ curproc->chan =0;
+ cas(&curproc->state, -SLEEPING, RUNNING);
+ //release(&ptable.lock);
+ popcli();
return -1;
}
// Wait for children to exit. (See wakeup1 call in proc_exit.)
- sleep(curproc, &ptable.lock); //DOC: wait-sleep
+ sched(); //DOC: wait-sleep
}
}
@@ -335,6 +376,37 @@ wait(void)
// - swtch to start running that process
// - eventually that process transfers control
// via swtch back to the scheduler.
+// MY_CODE 2.4
+int//TODO Check
+shouldCont(struct proc* p)
+{
+ if(!p->stopped)
+ return 1;
+ uint pending_signals = p->pending_signals & ~(p->signal_mask & ~((1 << SIGSTOP) | (1 << SIGKILL)));
+ //loop over signals
+ for(int i=0; i<SIGNALS_SIZE; i++)
+ {
+ uint currSig = 1 << i;
+ void* currHandler = p->signal_handlers[i];
+ //checking sig bit is on and not sigstop
+ if((currSig & pending_signals) && (i!=SIGSTOP))
+ {
+ //if handler is default checking only sigcont ||
+ if((currHandler == SIG_DFL && i==SIGCONT) || currHandler == (void*) SIGCONT || i==SIGKILL)
+ {
+ //cprintf("scheduler encountered SIGCONT");
+ //update pending signals
+ if(i != SIGKILL || currHandler == (void*) SIGKILL)
+ p->pending_signals = p->pending_signals & ~(currSig);
+ //set p to not be stopped
+ p->stopped = 0;
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+// /MY_CODE 2.4
void
scheduler(void)
{
@@ -347,26 +419,49 @@ scheduler(void)
sti();
// Loop over process table looking for process to run.
- acquire(&ptable.lock);
+ //acquire(&ptable.lock);
+ pushcli();
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
- if(p->state != RUNNABLE)
+ if(!cas(&p->state,RUNNABLE,-RUNNING))
+ {
+ //cprintf("pid: %d , state: %d\n", p->pid, p->state);
+ continue;
+ }
+ // MY_CODE 2.4
+ if(!shouldCont(p))
+ {
+ cas(&p->state,-RUNNING, RUNNABLE);
continue;
+ }
+ // /MY_CODE 2.4
// Switch to chosen process. It is the process's job
// to release ptable.lock and then reacquire it
// before jumping back to us.
+ //cprintf("Big BALAGAN\n");
c->proc = p;
switchuvm(p);
+ //p->state = RUNNING;
+ //cprintf("pid: %d , state: %d\n", p->pid, p->state);
p->state = RUNNING;
-
+ //cprintf("pid: %d , state: %d\n", p->pid, p->state);
swtch(&(c->scheduler), p->context);
switchkvm();
+ //cprintf("pid: %d , state: %d\n", p->pid, p->state);
+ //wake up if zombie
+ if(cas(&p->state, -ZOMBIE, ZOMBIE))
+ wakeup1(p->parent);
+
+ //otherwize change make it the absolute value
+ cas(&p->state, -RUNNABLE, RUNNABLE);
+ cas(&p->state, -SLEEPING, SLEEPING);
+ //cprintf("pid: %d , state: %d\n", p->pid, p->state);
// Process is done running for now.
// It should have changed its p->state before coming back.
c->proc = 0;
}
- release(&ptable.lock);
+ popcli();
}
}
@@ -384,8 +479,8 @@ sched(void)
int intena;
struct proc *p = myproc();
- if(!holding(&ptable.lock))
- panic("sched ptable.lock");
+ //if(!holding(&ptable.lock))
+ // panic("sched ptable.lock");
if(mycpu()->ncli != 1)
panic("sched locks");
if(p->state == RUNNING)
@@ -401,10 +496,11 @@ sched(void)
void
yield(void)
{
- acquire(&ptable.lock); //DOC: yieldlock
- myproc()->state = RUNNABLE;
+ pushcli();
+ //myproc()->state = RUNNABLE;
+ cas(&myproc()->state, RUNNING, -RUNNABLE);
sched();
- release(&ptable.lock);
+ popcli();
}
// A fork child's very first scheduling by scheduler()
@@ -414,7 +510,7 @@ forkret(void)
{
static int first = 1;
// Still holding ptable.lock from scheduler.
- release(&ptable.lock);
+ popcli();
if (first) {
// Some initialization functions must be run in the context
@@ -447,24 +543,31 @@ sleep(void *chan, struct spinlock *lk)
// guaranteed that we won't miss any wakeup
// (wakeup runs with ptable.lock locked),
// so it's okay to release lk.
- if(lk != &ptable.lock){ //DOC: sleeplock0
- acquire(&ptable.lock); //DOC: sleeplock1
- release(lk);
- }
+// if(lk != &ptable.lock){ //DOC: sleeplock0
+// acquire(&ptable.lock); //DOC: sleeplock1
+// release(lk);
+// }
// Go to sleep.
+ pushcli();
p->chan = chan;
- p->state = SLEEPING;
+ cas(&p->state, RUNNING, -SLEEPING);
+ release(lk);
+
sched();
// Tidy up.
- p->chan = 0;
-
+// p->chan = 0;
+ acquire(lk);
+ popcli();
+
+
+
// Reacquire original lock.
- if(lk != &ptable.lock){ //DOC: sleeplock2
- release(&ptable.lock);
- acquire(lk);
- }
+// if(lk != &ptable.lock){ //DOC: sleeplock2
+// release(&ptable.lock);
+// acquire(lk);
+// }
}
//PAGEBREAK!
@@ -476,39 +579,74 @@ wakeup1(void *chan)
struct proc *p;
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
- if(p->state == SLEEPING && p->chan == chan)
- p->state = RUNNABLE;
+ {
+ if(abs(p->state) == SLEEPING && p->chan == chan)
+ {
+ while((!cas(&p->state, SLEEPING, -RUNNABLE)) && p->state!=RUNNING);
+ if(p->state != RUNNING)
+ {
+ //cprintf("wake up - pid: %d, state: %d\n", p->pid, p->state);
+ p->chan = 0;
+ cas(&p->state, -RUNNABLE, RUNNABLE);
+ //cprintf("after wake up - pid: %d, state: %d\n", p->pid, p->state);
+ }
+ //if success changing to Runnable and not p->state didnt turn to Running
+ }
+ }
}
// Wake up all processes sleeping on chan.
void
wakeup(void *chan)
{
- acquire(&ptable.lock);
+ //acquire(&ptable.lock);
+ pushcli();
wakeup1(chan);
- release(&ptable.lock);
+ popcli();
+ //release(&ptable.lock);
}
// Kill the process with the given pid.
// Process won't exit until it returns
// to user space (see trap in trap.c).
int
-kill(int pid)
+kill(int pid, int signum)
{
+ //cprintf("Current Thread - begin: pid:%d , signum: %d\n", pid, signum);
+ //cprintf("Entered kill system call\n");
struct proc *p;
-
- acquire(&ptable.lock);
+ pushcli();
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->pid == pid){
- p->killed = 1;
+ // MY_CODE - 2.2.1
+ uint curr_sig = 1 << signum;
+ p->pending_signals = (p->pending_signals) | curr_sig;
+ //cprintf("Pending signals: %d\n", p->pending_signals);
// Wake process from sleep if necessary.
if(p->state == SLEEPING)
- p->state = RUNNABLE;
- release(&ptable.lock);
+ {
+ //wake up cases:
+ //if signum is sigkill
+ if(signum == SIGKILL)
+ cas(&p->state,SLEEPING,RUNNABLE);
+ //if the handler is SIGKILL and the signum is not blocked
+ else if((p->signal_handlers[signum] == (void*)SIGKILL) && (!((curr_sig) & p->signal_mask)))
+ cas(&p->state,SLEEPING,RUNNABLE);
+ //if the handler is Default and the signum is not blocked and not sigstop or sigcont
+ else if((p->signal_handlers[signum] == (void*)SIG_DFL) && (!((curr_sig) & p->signal_mask)))
+ {
+ if(signum != SIGSTOP && signum != SIGCONT)
+ cas(&p->state,SLEEPING,RUNNABLE);
+ }
+ }
+ // /MY_CODE - 2.2.1
+ //cprintf("Current Thread - end: pid:%d , state: %d\n", pid, p->state);
+ //release(&ptable.lock);
+ popcli();
return 0;
}
}
- release(&ptable.lock);
+ popcli();
return -1;
}
@@ -525,7 +663,7 @@ procdump(void)
[SLEEPING] "sleep ",
[RUNNABLE] "runble",
[RUNNING] "run ",
- [ZOMBIE] "zombie"
+ [ZOMBIE] "zombie",
};
int i;
struct proc *p;
@@ -539,7 +677,8 @@ procdump(void)
state = states[p->state];
else
state = "???";
- cprintf("%d %s %s", p->pid, state, p->name);
+ cprintf("%d %s %s\n", p->pid, state, p->name);
+ cprintf("%d %d %s\n", p->pid, p->state, p->name);
if(p->state == SLEEPING){
getcallerpcs((uint*)p->context->ebp+2, pc);
for(i=0; i<10 && pc[i] != 0; i++)
@@ -548,3 +687,120 @@ procdump(void)
cprintf("\n");
}
}
+// MY_CODE - 2.1.4
+int
+sigaction(int signum, const struct sigaction* act, struct sigaction* oldact)
+{
+ struct proc *p = myproc();
+ //checking signum is valid and signals_mals positive
+ if(signum < 0 || signum > 31 || act->sigmask < 0 || signum == SIGKILL || signum == SIGSTOP)
+ return -1;
+ if(oldact != null)
+ {
+ oldact->sigmask = p->signals_masks[signum];
+ oldact->sa_handler = p->signal_handlers[signum];
+ }
+ p->signals_masks[signum] = act->sigmask;
+ p->signal_handlers[signum] = act->sa_handler;
+ return 0;
+}
+// /MY_CODE - 2.1.4
+
+
+// MY_CODE 2.4
+void userSignalHandler(struct proc* p, int sigNum)
+{
+ uint labelSize = (uint)myEnd - (uint) myStart;
+ //save struct frame
+ //make space for trapframe
+ p->tf->esp-=sizeof(struct trapframe);
+ //copy current trapframe to stack
+ memmove((void*) p->tf->esp, p->tf, sizeof(struct trapframe));
+ //make tf_backup to point the location of the old tf in the stack
+ p->tf_backup = (void*) p->tf->esp;
+ //make space for the code
+ p->tf->esp-=labelSize;
+ //push the code
+ memmove((void*) p->tf->esp, myStart, labelSize);
+ //push sigNum and return address
+ *(int*)(p->tf->esp-4) = sigNum;
+ *(int*)(p->tf->esp-8) = p->tf->esp;
+ p->tf->esp -= 8;
+ p->tf->eip = (uint) p->signal_handlers[sigNum];
+}
+
+
+void
+signal_handler(struct proc* p, int sigNum)
+{
+ void* currHandler = p->signal_handlers[sigNum];
+ if(currHandler == (void*)SIG_IGN || currHandler == (void*)SIGCONT)
+ {
+ //cprintf("signal handler - encountered handler with SIG_IGN or SIGCONT\n");
+ return;
+ }
+ if(currHandler == (void*)SIG_DFL)
+ {
+ //cprintf("signal handler - encountered handler with SIG_DFL\n");
+ switch(sigNum)
+ {
+ case SIGCONT:
+ //cprintf("signal handler - encountered handler with SIG_DFL - SIGCONT\n");
+ break;
+ case SIGSTOP:
+ //cprintf("signal handler - encountered handler with SIG_DFL - SIGSTOP\n");
+ p->stopped = 1;
+ yield();
+ break;
+ default:
+ //cprintf("signal handler - encountered handler with SIG_DFL - default\n");
+ p->killed = 1;
+ }
+ }
+ else if(currHandler == (void*)SIGSTOP)
+ {
+ //cprintf("signal handler - encountered handler with SIGSTOP\n");
+ p->stopped = 1;
+ yield();
+ }
+ else if(currHandler == (void*)SIGKILL)
+ {
+ //cprintf("signal handler - encountered handler with SIGKILL\n");
+ p->killed = 1;
+ }
+ else
+ {
+ //cprintf("signal handler - encountered user handler\n");
+ userSignalHandler(p, sigNum);
+ }
+ return;
+
+}
+
+void
+signal_handlers(struct trapframe* tf)
+{
+ struct proc* p = myproc();
+ if(p==null || (((tf->cs) & 3) != DPL_USER))
+ return;
+ uint pending_signals = p->pending_signals & ~(p->signal_mask & ~((1 << SIGSTOP) | (1 << SIGKILL)));
+ //cprintf("PENDING SIG: %d\n", pending_signals);
+ //loop over signals -> while the process not killed & he still have signals & hes not handling signals at the moment;
+ for(int i=0; i<SIGNALS_SIZE ; i++)
+ {
+ uint currSig = 1 << i;
+ if(currSig & pending_signals)
+ {
+ //cprintf("signal need to be handled : signum = %d\n", i);
+ //Set signal as handled
+ p->pending_signals = p->pending_signals & ~(currSig);
+ //Setting back to currentmask
+ p->backup_mask = p->signal_mask;
+ //Seting mask to be signal_mask
+ p->signal_mask = p->signals_masks[i];
+ //Now handling the signal
+ signal_handler(p, i);
+ }
+ }
+}
+// /MY_CODE 2.4
\ No newline at end of file
diff --git a/proc.h b/proc.h
index 1647114..4c56340 100644
--- a/proc.h
+++ b/proc.h
@@ -46,9 +46,23 @@ struct proc {
struct context *context; // swtch() here to run process
void *chan; // If non-zero, sleeping on chan
int killed; // If non-zero, have been killed
+ int stopped; // If non-zero, have been stopped
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
+ uint pending_signals; // Process pending signals
+ uint signal_mask; // Process mask
+ uint signals_masks[32]; // Each signal with his signals mask
+ uint backup_mask; // Process backup mask
+ int busy; // Process handle signal bool
+ void* signal_handlers[32]; // Process signal handlers
+ struct trapframe* tf_backup; // Process trapframe backup
+
+};
+
+struct sigaction {
+ void(* sa_handler)(int);
+ uint sigmask;
};
// Process memory is laid out contiguously, low addresses first:
diff --git a/sanity-check.c b/sanity-check.c
new file mode 100644
index 0000000..ee4d24c
--- /dev/null
+++ b/sanity-check.c
@@ -0,0 +1,190 @@
+#include "types.h"
+#include "stat.h"
+#include "user.h"
+
+void big_calcultion_func(){
+ int y=0;
+ for(int i=0 ; i<999999; i++)
+ {
+ y+= i;
+ }
+}
+void
+UserSpaceHandler(int num)
+{
+ printf(1,"My UserSpaceHandler: %d\n", num);
+}
+
+void
+testSTOPCONTKILLSignals()
+{
+ int child = fork();
+ if (child < 0)
+ {
+ printf(1, "fork - error\n");
+ exit();
+ }
+ else if (child > 0)
+ {
+ sleep(150);
+ printf(1, "Sent SIGSTOP -> should not be killed yet\n");
+ kill(child, SIGSTOP);
+ sleep(150);
+ printf(1, "Sent SIGCONT -> should not be killed yet\n");
+ kill(child,SIGCONT);
+ sleep(150);
+ printf(1, "Sent SIGKILL ->NOW SHOULD EXIT\n");
+ kill(child,SIGKILL);
+ printf(1, "SUCCESS%d\n", wait());
+ }
+ else
+ {
+ while(1)
+ {big_calcultion_func();}
+ }
+}
+
+void
+testSigProcMaskSigAction()
+{
+ struct sigaction sig = {(void*)SIGKILL, 0};
+ struct sigaction invalid_sig1 = {(void*)SIGKILL, 0};
+ struct sigaction invalid_sig2 = {(void*)SIGSTOP, 0};
+ struct sigaction invalid_sig3 = {(void*)SIGCONT, 0};
+ struct sigaction invalid_sig4 = {(void*)SIGCONT, 0};
+ printf(1, "Trying invalid input on sigaction\n");
+ printf(1, "INVALID1 : %s\n", (sigaction(SIGKILL, &invalid_sig1, null)==-1) ? "Passed" : "Failed");
+ printf(1, "INVALID2 : %s\n", (sigaction(SIGSTOP, &invalid_sig2, null)==-1) ? "Passed" : "Failed");
+ printf(1, "INVALID3 : %s\n", (sigaction(-1, &invalid_sig3, null)==-1) ? "Passed" : "Failed");
+ printf(1, "INVALID4 : %s\n",( sigaction(32, &invalid_sig4, null)==-1) ? "Passed" : "Failed");
+
+
+ sigaction(1, &sig, null);
+ sigprocmask(2);
+ int child = fork();
+ if (child<0)
+ {
+ printf(1, "fork - error\n");
+ exit();
+ }
+ else if (child > 0)
+ {
+ sleep(50);
+ printf(1, "trying to kill chiled - should not kill him right away\n");
+ kill(child,1);
+ wait();
+ sigprocmask(0);
+ struct sigaction sig = {(void*)SIG_DFL, 0};
+ sigaction(1, &sig, null);
+ }
+ else
+ {
+ while (1)
+ {
+ sleep(1000);
+ sigprocmask(0);
+ }
+ }
+}
+
+void
+testUserSpaceHandler()
+{
+ struct sigaction sig1 = {&UserSpaceHandler, 0};
+ struct sigaction sig2 = {(void*) SIG_IGN, 0};
+ sigaction(1, &sig1, null);
+ sigaction(2, &sig2, null);
+ int child = fork();
+ if (child<0)
+ {
+ printf(1, "fork - error\n");
+ exit();
+ }
+ else if (child > 0)
+ {
+ sleep(50);
+ printf(1, "trying to send user handler signal to child - should print user handler\n");
+ kill(child,1);
+ sleep(100);
+ printf(1, "trying to kill child with SIG_IGN - should not be killed now \n");
+ kill(child, 2);
+ sleep(1000);
+ printf(1, "trying to kill child with SIG_KILL - should be killed now \n");
+ kill(child, SIGKILL);
+ wait();
+ struct sigaction sig = {(void*)SIG_DFL, 0};
+ sigaction(1, &sig, null);
+ sigaction(2, &sig, null);
+ printf(1, "PASSED\n");
+ }
+ else
+ {
+ while (1){
+ big_calcultion_func();
+ };
+ }
+}
+
+void
+part4()
+{
+int child = fork();
+ if (child < 0)
+ {
+ printf(1, "fork - error\n");
+ exit();
+ }
+ else if (child > 0)
+ {
+ sleep(150);
+ kill(child, SIGSTOP);
+ sleep(150);
+ kill(child,SIGCONT);
+ sleep(150);
+ kill(child,SIGKILL);
+ printf(1, "SUCCESS1 - %d\n", wait());
+ child = fork();
+ if (child < 0)
+ {
+ printf(1, "fork - error\n");
+ exit();
+ }
+ else if (child > 0)
+ {
+ sleep(150);
+ kill(child, SIGSTOP);
+ sleep(150);
+ kill(child,SIGCONT);
+ sleep(150);
+ kill(child,SIGKILL);
+ printf(1, "SUCCESS2 - %d\n", wait());
+ }
+ else
+ {
+ while(1)
+ {big_calcultion_func();}
+ }
+ }
+ else
+ {
+ while(1)
+ {big_calcultion_func();}
+ }
+}
+
+
+int main()
+{
+ //part 2.4
+ printf(1, "Test_1: SIGKILL, SIGCONT, SIGSTOP SIGNALS\n");
+ testSTOPCONTKILLSignals();
+ printf(1, "Test_2: sigprocmask && sigaction \n");
+ testSigProcMaskSigAction();
+ printf(1, "Test_3: User Space signals + SIG_IGN \n");
+ testUserSpaceHandler();
+ //part 4.0
+ printf(1, "TEST_4: Testing cas and sync\n");
+ part4();
+ printf(1, "PASSED");
+ exit();
+}
\ No newline at end of file
diff --git a/sanityCheck.c b/sanityCheck.c
new file mode 100644
index 0000000..e69de29
diff --git a/syscall.c b/syscall.c
index ee85261..0d2b6cd 100644
--- a/syscall.c
+++ b/syscall.c
@@ -103,6 +103,9 @@ extern int sys_unlink(void);
extern int sys_wait(void);
extern int sys_write(void);
extern int sys_uptime(void);
+extern int sys_sigprocmask(void);
+extern int sys_sigaction(void);
+extern int sys_sigret(void);
static int (*syscalls[])(void) = {
[SYS_fork] sys_fork,
@@ -126,6 +129,9 @@ static int (*syscalls[])(void) = {
[SYS_link] sys_link,