-
Notifications
You must be signed in to change notification settings - Fork 2
/
200952448_302762190.patch
3119 lines (2912 loc) · 64.1 KB
/
200952448_302762190.patch
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 509bf9a..ca02ff6 100644
--- a/Makefile
+++ b/Makefile
@@ -179,6 +179,7 @@ UPROGS=\
_usertests\
_wc\
_zombie\
+ _sanity\
fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)
@@ -248,7 +249,7 @@ qemu-nox-gdb: fs.img xv6.img .gdbinit
EXTRA=\
mkfs.c ulib.c user.h cat.c echo.c forktest.c grep.c kill.c\
ln.c ls.c mkdir.c rm.c stressfs.c usertests.c wc.c zombie.c\
- printf.c umalloc.c\
+ printf.c umalloc.c sanity.c\
README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list\
.gdbinit.tmpl gdbutil\
diff --git a/README b/README
index 3427894..7fffbc1 100644
--- a/README
+++ b/README
@@ -1,3 +1,6 @@
+ASSIGNMENT INSTRUCTIONS:
+https://www.cs.bgu.ac.il/~os172/wiki.files/Assignment1.pdf
+
xv6 is a re-implementation of Dennis Ritchie's and Ken Thompson's Unix
Version 6 (v6). xv6 loosely follows the structure and style of v6,
but is implemented for a modern x86-based multiprocessor using ANSI C.
diff --git a/cat.c b/cat.c
index 5ddc820..ed1987d 100644
--- a/cat.c
+++ b/cat.c
@@ -12,12 +12,12 @@ cat(int fd)
while((n = read(fd, buf, sizeof(buf))) > 0) {
if (write(1, buf, n) != n) {
printf(1, "cat: write error\n");
- exit();
+ exit(0);
}
}
if(n < 0){
printf(1, "cat: read error\n");
- exit();
+ exit(0);
}
}
@@ -28,16 +28,16 @@ main(int argc, char *argv[])
if(argc <= 1){
cat(0);
- exit();
+ exit(0);
}
for(i = 1; i < argc; i++){
if((fd = open(argv[i], 0)) < 0){
printf(1, "cat: cannot open %s\n", argv[i]);
- exit();
+ exit(0);
}
cat(fd);
close(fd);
}
- exit();
+ exit(0);
}
diff --git a/defs.h b/defs.h
index 300c75c..c283ece 100644
--- a/defs.h
+++ b/defs.h
@@ -3,6 +3,7 @@ struct context;
struct file;
struct inode;
struct pipe;
+struct perf;
struct proc;
struct rtcdate;
struct spinlock;
@@ -103,7 +104,7 @@ int pipewrite(struct pipe*, char*, int);
//PAGEBREAK: 16
// proc.c
-void exit(void);
+void exit(int);
int fork(void);
int growproc(int);
int kill(int);
@@ -113,9 +114,12 @@ void scheduler(void) __attribute__((noreturn));
void sched(void);
void sleep(void*, struct spinlock*);
void userinit(void);
-int wait(void);
+int wait(int*);
+void priority(int);
+void policy(int);
void wakeup(void*);
void yield(void);
+int wait_stat(int*, struct perf*);
// swtch.S
void swtch(struct context**, struct context*);
diff --git a/echo.c b/echo.c
index 806dee0..eed68a0 100644
--- a/echo.c
+++ b/echo.c
@@ -9,5 +9,5 @@ main(int argc, char *argv[])
for(i = 1; i < argc; i++)
printf(1, "%s%s", argv[i], i+1 < argc ? " " : "\n");
- exit();
+ exit(0);
}
diff --git a/exec.c b/exec.c
index 55b66dd..2ba30a0 100644
--- a/exec.c
+++ b/exec.c
@@ -7,14 +7,17 @@
#include "x86.h"
#include "elf.h"
-void
-pseudo_main(int (*entry)(int, char**), int argc, char **argv)
-{
+void pseudo_main(int (*entry)(int, char**), int argc, char **argv) {
+
+ (*entry)(argc,argv);
+
+ __asm__ ("push %eax \n\t"
+ "push $0 \n\t"
+ "movl $2, %eax \n\t"
+ "int $64");
}
-int
-exec(char *path, char **argv)
-{
+int exec(char *path, char **argv) {
char *s, *last;
int i, off;
uint argc, sz, sp, ustack[3+MAXARG+1];
@@ -25,7 +28,6 @@ exec(char *path, char **argv)
pde_t *pgdir, *oldpgdir;
begin_op();
-
if((ip = namei(path)) == 0){
end_op();
return -1;
@@ -79,24 +81,32 @@ exec(char *path, char **argv)
sp = sz;
// Push argument strings, prepare rest of stack in ustack.
+
+//////////added code////////// args: entry,argc,argv
+
for(argc = 0; argv[argc]; argc++) {
if(argc >= MAXARG)
goto bad;
sp = (sp - (strlen(argv[argc]) + 1)) & ~3;
if(copyout(pgdir, sp, argv[argc], strlen(argv[argc]) + 1) < 0)
goto bad;
- ustack[3+argc] = sp;
+ ustack[4+argc] = sp;
}
- ustack[3+argc] = 0;
+ ustack[4+argc] = 0;
+
ustack[0] = 0xffffffff; // fake return PC
- ustack[1] = argc;
- ustack[2] = sp - (argc+1)*4; // argv pointer
+ ustack[1] = elf.entry;
+ ustack[2] = argc;
+ ustack[3] = sp - (argc+1)*4; //argv of original main
- sp -= (3+argc+1) * 4;
- if(copyout(pgdir, sp, ustack, (3+argc+1)*4) < 0)
+ sp -= (4+argc+1) * 4;
+ if(copyout(pgdir, sp, ustack, (4+argc+1)*4) < 0)
goto bad;
+
+//////////added code//////////
+
// Save program name for debugging.
for(last=s=path; *s; s++)
if(*s == '/')
@@ -107,8 +117,9 @@ exec(char *path, char **argv)
oldpgdir = proc->pgdir;
proc->pgdir = pgdir;
proc->sz = sz;
- proc->tf->eip = elf.entry; // main
+ proc->tf->eip = pointer_pseudo_main; // main
proc->tf->esp = sp;
+
switchuvm(proc);
freevm(oldpgdir);
return 0;
diff --git a/forktest.c b/forktest.c
index 73f2fe8..101bccf 100644
--- a/forktest.c
+++ b/forktest.c
@@ -17,6 +17,7 @@ void
forktest(void)
{
int n, pid;
+ int status = 0;
printf(1, "fork test\n");
@@ -25,24 +26,24 @@ forktest(void)
if(pid < 0)
break;
if(pid == 0)
- exit();
+ exit(0);
}
if(n == N){
printf(1, "fork claimed to work N times!\n", N);
- exit();
+ exit(0);
}
for(; n > 0; n--){
- if(wait() < 0){
+ if(wait(&status) < 0){
printf(1, "wait stopped early\n");
- exit();
+ exit(0);
}
}
- if(wait() != -1){
+ if(wait(&status) != -1){
printf(1, "wait got too many\n");
- exit();
+ exit(0);
}
printf(1, "fork test OK\n");
@@ -52,5 +53,5 @@ int
main(void)
{
forktest();
- exit();
+ exit(0);
}
diff --git a/grep.c b/grep.c
index adc4835..1aa1081 100644
--- a/grep.c
+++ b/grep.c
@@ -43,24 +43,24 @@ main(int argc, char *argv[])
if(argc <= 1){
printf(2, "usage: grep pattern [file ...]\n");
- exit();
+ exit(0);
}
pattern = argv[1];
if(argc <= 2){
grep(pattern, 0);
- exit();
+ exit(0);
}
for(i = 2; i < argc; i++){
if((fd = open(argv[i], 0)) < 0){
printf(1, "grep: cannot open %s\n", argv[i]);
- exit();
+ exit(0);
}
grep(pattern, fd);
close(fd);
}
- exit();
+ exit(0);
}
// Regexp matcher from Kernighan & Pike,
diff --git a/init.c b/init.c
index 10ca30b..49338fa 100644
--- a/init.c
+++ b/init.c
@@ -11,6 +11,7 @@ int
main(void)
{
int pid, wpid;
+ int status = 0;
if(open("console", O_RDWR) < 0){
mknod("console", 1, 1);
@@ -24,14 +25,14 @@ main(void)
pid = fork();
if(pid < 0){
printf(1, "init: fork failed\n");
- exit();
+ exit(0);
}
if(pid == 0){
exec("/bin/sh", argv);
printf(1, "init: exec sh failed\n");
- exit();
+ exit(0);
}
- while((wpid=wait()) >= 0 && wpid != pid)
+ while((wpid=wait(&status)) >= 0 && wpid != pid)
printf(1, "zombie!\n");
}
}
diff --git a/kill.c b/kill.c
index 364f6af..5a2e450 100644
--- a/kill.c
+++ b/kill.c
@@ -9,9 +9,9 @@ main(int argc, char **argv)
if(argc < 2){
printf(2, "usage: kill pid...\n");
- exit();
+ exit(0);
}
for(i=1; i<argc; i++)
kill(atoi(argv[i]));
- exit();
+ exit(0);
}
diff --git a/ln.c b/ln.c
index cf8a64e..14acdc8 100644
--- a/ln.c
+++ b/ln.c
@@ -7,9 +7,9 @@ main(int argc, char *argv[])
{
if(argc != 3){
printf(2, "Usage: ln old new\n");
- exit();
+ exit(0);
}
if(link(argv[1], argv[2]) < 0)
printf(2, "link %s %s: failed\n", argv[1], argv[2]);
- exit();
+ exit(0);
}
diff --git a/ls.c b/ls.c
index 2862913..27742c2 100644
--- a/ls.c
+++ b/ls.c
@@ -77,9 +77,9 @@ main(int argc, char *argv[])
if(argc < 2){
ls(".");
- exit();
+ exit(0);
}
for(i=1; i<argc; i++)
ls(argv[i]);
- exit();
+ exit(0);
}
diff --git a/mkdir.c b/mkdir.c
index 6e4c954..d101838 100644
--- a/mkdir.c
+++ b/mkdir.c
@@ -9,7 +9,7 @@ main(int argc, char *argv[])
if(argc < 2){
printf(2, "Usage: mkdir files...\n");
- exit();
+ exit(0);
}
for(i = 1; i < argc; i++){
@@ -19,5 +19,5 @@ main(int argc, char *argv[])
}
}
- exit();
+ exit(0);
}
diff --git a/proc.c b/proc.c
index 7d03ad7..f16ee0a 100644
--- a/proc.c
+++ b/proc.c
@@ -19,21 +19,26 @@ extern void forkret(void);
extern void trapret(void);
static void wakeup1(void *chan);
+static unsigned long int next = 1;
-void
-pinit(void)
-{
+
+enum policy { UNIFORM, PRIORITY, DYNAMIC };
+enum policy pol = UNIFORM;
+
+void pinit(void) {
initlock(&ptable.lock, "ptable");
}
+int systemUptime(void) {
+ return ticks;
+}
+
//PAGEBREAK: 32
// Look in the process table for an UNUSED proc.
// If found, change state to EMBRYO and initialize
// state required to run in the kernel.
// Otherwise return 0.
-static struct proc*
-allocproc(void)
-{
+static struct proc* allocproc(void) {
struct proc *p;
char *sp;
@@ -49,9 +54,21 @@ allocproc(void)
found:
p->state = EMBRYO;
p->pid = nextpid++;
-
+ switch(pol) {
+ case UNIFORM:
+ p->ntickets = 1;
+ break;
+ case PRIORITY:
+ p->ntickets = 10;
+ break;
+ case DYNAMIC:
+ p->ntickets = 20;
+ break;
+ }
release(&ptable.lock);
+ p->ctime = systemUptime();
+
// Allocate kernel stack.
if((p->kstack = kalloc()) == 0){
p->state = UNUSED;
@@ -72,15 +89,12 @@ found:
p->context = (struct context*)sp;
memset(p->context, 0, sizeof *p->context);
p->context->eip = (uint)forkret;
-
return p;
}
//PAGEBREAK: 32
// Set up first user process.
-void
-userinit(void)
-{
+void userinit(void) {
struct proc *p;
extern char _binary_initcode_start[], _binary_initcode_size[];
@@ -110,15 +124,12 @@ userinit(void)
acquire(&ptable.lock);
p->state = RUNNABLE;
-
release(&ptable.lock);
}
// Grow current process's memory by n bytes.
// Return 0 on success, -1 on failure.
-int
-growproc(int n)
-{
+int growproc(int n) {
uint sz;
sz = proc->sz;
@@ -137,9 +148,7 @@ growproc(int n)
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
-int
-fork(void)
-{
+int fork(void) {
int i, pid;
struct proc *np;
@@ -183,12 +192,12 @@ fork(void)
// Exit the current process. Does not return.
// An exited process remains in the zombie state
// until its parent calls wait() to find out it exited.
-void
-exit(void)
-{
+void exit(int status) {
struct proc *p;
int fd;
+ proc->status = status;
+
if(proc == initproc)
panic("init exiting");
@@ -219,17 +228,78 @@ exit(void)
}
}
+ proc->ttime = systemUptime();
// Jump into the scheduler, never to return.
+
proc->state = ZOMBIE;
+
sched();
panic("zombie exit");
}
+
+// Wait for a child process to exit and return its pid.
+// Return -1 if this process has no children.
+int wait_stat(int * status, struct perf * perfPtr) {
+ struct proc *p;
+ int havekids, pid;
+
+ acquire(&ptable.lock);
+ for(;;){
+ // Scan through table looking for exited children.
+ havekids = 0;
+ for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
+ if(p->parent != proc)
+ continue;
+ havekids = 1;
+ if(p->state == ZOMBIE){
+ if (status != 0) {
+ *status = p-> status;
+ }
+ perfPtr->ctime = p->ctime;
+ perfPtr->ttime = p->ttime;
+ perfPtr->stime = p->stime;
+ perfPtr->retime = p->retime;
+ perfPtr->rutime = p->rutime;
+
+ // Found one.
+ pid = p->pid;
+ kfree(p->kstack);
+ p->kstack = 0;
+ freevm(p->pgdir);
+ p->pid = 0;
+ p->parent = 0;
+ p->name[0] = 0;
+ p->killed = 0;
+ p->ntickets = 0;
+ p->ctime = 0;
+ p->ttime = 0;
+ p->stime = 0;
+ p->retime = 0;
+ p->rutime = 0;
+
+ p->state = UNUSED;
+ release(&ptable.lock);
+ return pid;
+ }
+ }
+
+ // No point waiting if we don't have any children.
+ if(!havekids || proc->killed){
+ release(&ptable.lock);
+ return -1;
+ }
+
+ // Wait for children to exit. (See wakeup1 call in proc_exit.)
+ sleep(proc, &ptable.lock); //DOC: wait-sleep
+ }
+}
+
+
+
// Wait for a child process to exit and return its pid.
// Return -1 if this process has no children.
-int
-wait(void)
-{
+int wait(int * status) {
struct proc *p;
int havekids, pid;
@@ -242,6 +312,9 @@ wait(void)
continue;
havekids = 1;
if(p->state == ZOMBIE){
+ if (status != 0) {
+ *status = p-> status;
+ }
// Found one.
pid = p->pid;
kfree(p->kstack);
@@ -251,6 +324,12 @@ wait(void)
p->parent = 0;
p->name[0] = 0;
p->killed = 0;
+ p->ntickets = 0;
+ p->ctime = 0;
+ p->ttime = 0;
+ p->stime = 0;
+ p->retime = 0;
+ p->rutime = 0;
p->state = UNUSED;
release(&ptable.lock);
return pid;
@@ -268,6 +347,96 @@ wait(void)
}
}
+void distributeTickets(int ticketCount) {
+ acquire(&ptable.lock);
+ struct proc *p;
+ for(p = ptable.proc; p < &ptable.proc[NPROC]; p++) {
+ if(p->state != UNUSED)
+ p->ntickets = ticketCount;
+ }
+ release(&ptable.lock);
+}
+
+
+void priority(int priority) {
+ if (pol == PRIORITY)
+ proc->ntickets = priority;
+}
+
+
+void policy(int policy) {
+ pol = policy;
+ switch(pol) {
+ case UNIFORM:
+ distributeTickets(1);
+ break;
+ case PRIORITY:
+ distributeTickets(10);
+ break;
+ case DYNAMIC:
+ distributeTickets(20);
+ break;
+ }
+}
+
+int getTicketSum(void) {
+ int sum = 0;
+ acquire(&ptable.lock);
+ struct proc *p;
+ for(p = ptable.proc; p < &ptable.proc[NPROC]; p++) {
+ if(p->state != UNUSED)
+ sum += p->ntickets;
+ }
+ release(&ptable.lock);
+ return sum;
+}
+
+static int first = 0;
+int getRandomTicket(void) {
+
+ if (first == 0 && ticks != 0){
+ first = 1;
+ next = ticks;
+ }
+
+ int ticketSum = getTicketSum();
+ if (ticketSum == 0)
+ return 0;
+ next = next * 1103515245 + 12341;
+ return (unsigned int)(next/65536) % ticketSum;
+}
+
+struct proc* getSelectedProc(int ticketNum) {
+ acquire(&ptable.lock);
+ struct proc *p;
+ for(p = ptable.proc; p < &ptable.proc[NPROC]; p++) {
+ if(p->state == UNUSED)
+ continue;
+ ticketNum -= p->ntickets;
+ if (ticketNum < 0) {
+ release(&ptable.lock);
+ return p;
+ }
+ }
+ release(&ptable.lock);
+ return 0;
+}
+
+
+void redistributeTickets() {
+ int state = proc->state;
+ int ticketCount = proc->ntickets;
+ if (state == RUNNABLE && ticketCount > 1)
+ proc->ntickets--;
+ if (state == SLEEPING) {
+ if (ticketCount > 90)
+ proc->ntickets = 100;
+ else
+ proc->ntickets += 10;
+ }
+}
+
+
//PAGEBREAK: 42
// Per-CPU process scheduler.
// Each CPU calls scheduler() after setting itself up.
@@ -276,9 +445,44 @@ wait(void)
// - swtch to start running that process
// - eventually that process transfers control
// via swtch back to the scheduler.
-void
-scheduler(void)
-{
+//IMPLEMENTED SKEDULAR
+void scheduler(void) {
+ struct proc *p;
+ int ticketNum;
+
+ for(;;) {
+ sti(); // Enable interrupts on this processor.
+ ticketNum = getRandomTicket();
+ p = getSelectedProc(ticketNum);
+ acquire(&ptable.lock);
+ if(p != 0 && p->state == RUNNABLE) {
+
+ // Switch to chosen process. It is the process's job
+ // to release ptable.lock and then reacquire it
+ // before jumping back to us.
+ proc = p;
+ switchuvm(p);
+ p->state = RUNNING;
+ swtch(&cpu->scheduler, p->context);
+ switchkvm();
+
+ if (pol == DYNAMIC)
+ redistributeTickets();
+ // Process is done running for now.
+ // It should have changed its p->state before coming back.
+
+
+ proc = 0;
+ }
+ release(&ptable.lock);
+
+ }
+}
+
+
+
+//ORIGINAL SKEDULAR
+void orig_scheduler(void) {
struct proc *p;
for(;;){
@@ -309,6 +513,8 @@ scheduler(void)
}
}
+
+
// Enter scheduler. Must hold only ptable.lock
// and have changed proc->state. Saves and restores
// intena because intena is a property of this
@@ -316,9 +522,7 @@ scheduler(void)
// be proc->intena and proc->ncli, but that would
// break in the few places where a lock is held but
// there's no process.
-void
-sched(void)
-{
+void sched(void) {
int intena;
if(!holding(&ptable.lock))
@@ -335,9 +539,7 @@ sched(void)
}
// Give up the CPU for one scheduling round.
-void
-yield(void)
-{
+void yield(void) {
acquire(&ptable.lock); //DOC: yieldlock
proc->state = RUNNABLE;
sched();
@@ -346,9 +548,7 @@ yield(void)
// A fork child's very first scheduling by scheduler()
// will swtch here. "Return" to user space.
-void
-forkret(void)
-{
+void forkret(void) {
static int first = 1;
// Still holding ptable.lock from scheduler.
release(&ptable.lock);
@@ -367,9 +567,7 @@ forkret(void)
// Atomically release lock and sleep on chan.
// Reacquires lock when awakened.
-void
-sleep(void *chan, struct spinlock *lk)
-{
+void sleep(void *chan, struct spinlock *lk) {
if(proc == 0)
panic("sleep");
@@ -390,6 +588,7 @@ sleep(void *chan, struct spinlock *lk)
// Go to sleep.
proc->chan = chan;
proc->state = SLEEPING;
+
sched();
// Tidy up.
@@ -405,20 +604,17 @@ sleep(void *chan, struct spinlock *lk)
//PAGEBREAK!
// Wake up all processes sleeping on chan.
// The ptable lock must be held.
-static void
-wakeup1(void *chan)
-{
+static void wakeup1(void *chan) {
struct proc *p;
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
- if(p->state == SLEEPING && p->chan == chan)
+ if(p->state == SLEEPING && p->chan == chan) {
p->state = RUNNABLE;
+ }
}
// Wake up all processes sleeping on chan.
-void
-wakeup(void *chan)
-{
+void wakeup(void *chan) {
acquire(&ptable.lock);
wakeup1(chan);
release(&ptable.lock);
@@ -427,9 +623,7 @@ wakeup(void *chan)
// 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)
-{
+int kill(int pid) {
struct proc *p;
acquire(&ptable.lock);
@@ -437,8 +631,9 @@ kill(int pid)
if(p->pid == pid){
p->killed = 1;
// Wake process from sleep if necessary.
- if(p->state == SLEEPING)
+ if(p->state == SLEEPING) {
p->state = RUNNABLE;
+ }
release(&ptable.lock);
return 0;
}
@@ -447,13 +642,26 @@ kill(int pid)
return -1;
}
+
+void incCounters(void) {
+ struct proc *p;
+ acquire(&ptable.lock);
+ for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
+ if (p->state == SLEEPING)
+ p->stime++;
+ if (p->state == RUNNABLE)
+ p->retime++;
+ if (p->state == RUNNING)
+ p->rutime++;
+ }
+ release(&ptable.lock);
+}
+
//PAGEBREAK: 36
// Print a process listing to console. For debugging.
// Runs when user types ^P on console.
// No lock to avoid wedging a stuck machine further.
-void
-procdump(void)
-{
+void procdump(void) {
static char *states[] = {
[UNUSED] "unused",
[EMBRYO] "embryo",
diff --git a/proc.h b/proc.h
index 7352805..5a28513 100644
--- a/proc.h
+++ b/proc.h
@@ -46,6 +46,15 @@ struct context {
uint eip;
};
+
+struct perf {
+ int ctime;
+ int ttime;
+ int stime;
+ int retime;
+ int rutime;
+};
+
enum procstate { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
// Per-process state
@@ -63,6 +72,13 @@ struct proc {
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
+ int status; // Exit status
+ int ntickets; // Holds the amount of tickets allocated to the process
+ int ctime; // Procedure creation tick
+ int ttime; // Procedure termination tick
+ int stime; // Procedure total SLEEPING tick count
+ int retime; // Procedure total READY (RUNNABLE) tick count
+ int rutime; // Procedure total RUNNING tick count
};
// Process memory is laid out contiguously, low addresses first:
diff --git a/rm.c b/rm.c
index 4fd33c8..83c5f36 100644
--- a/rm.c
+++ b/rm.c
@@ -9,7 +9,7 @@ main(int argc, char *argv[])
if(argc < 2){
printf(2, "Usage: rm files...\n");
- exit();
+ exit(0);
}
for(i = 1; i < argc; i++){
@@ -19,5 +19,5 @@ main(int argc, char *argv[])
}
}
- exit();
+ exit(0);
}
diff --git a/sanity.c b/sanity.c
new file mode 100644
index 0000000..bd8379e
--- /dev/null
+++ b/sanity.c
@@ -0,0 +1,84 @@
+#include "types.h"
+#include "stat.h"
+#include "user.h"
+#include "fs.h"
+
+
+struct perf {
+ int ctime;
+ int ttime;
+ int stime;
+ int retime;
+ int rutime;
+};
+
+void printStat(struct perf * myPerf, int status){
+ printf(1, "%d\tstart\t\t%d\n",status, myPerf->ctime);
+ printf(1, "%d\tend\t\t%d\n",status, myPerf->ttime);
+ printf(1, "%d\tsleep\t\t%d\n",status, myPerf->stime);
+ printf(1, "%d\trunnable\t%d\n",status, myPerf->retime);
+ printf(1, "%d\trunning\t\t%d\n",status, myPerf->rutime);
+ printf(1, "%d\tturnaround\t%d\n",status, myPerf->ttime-myPerf->ctime);
+ printf(1, "%d\tSpent %d\% as runnable\n",status, (myPerf->retime*100/(myPerf->ttime-myPerf->ctime)));
+ printf(1, "%d\tSpent %d\% in sleep\n",status, (myPerf->stime*100/(myPerf->ttime-myPerf->ctime)));
+ printf(1, "\n");
+}
+
+
+void fib(int n) {
+ if (n <= 1)
+ return;
+ fib(n-1);
+ fib(n-2);
+}
+
+
+void workIO(){
+ int i;
+ for (i=0; i<1750 ;i++)
+ sleep(1);
+}
+
+void workCPU(){
+ fib(38);
+}
+
+
+void insanity(int deadpool, int testNum, void evenFunc(), void oddFunc()) {