-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmit.patch
More file actions
1626 lines (1560 loc) · 39.4 KB
/
Copy pathsubmit.patch
File metadata and controls
1626 lines (1560 loc) · 39.4 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/kern/arch/mips/syscall/syscall.c b/kern/arch/mips/syscall/syscall.c
index 0f773bd..e946dd3 100644
--- a/kern/arch/mips/syscall/syscall.c
+++ b/kern/arch/mips/syscall/syscall.c
@@ -35,6 +35,10 @@
#include <thread.h>
#include <current.h>
#include <syscall.h>
+#include <addrspace.h>
+#include <proc.h>
+#include <copyinout.h>
+#include <file.h>
/*
@@ -81,6 +85,11 @@ syscall(struct trapframe *tf)
int callno;
int32_t retval;
int err;
+ off_t pos=0;
+ off_t retval64=0;
+ //off_t temp=0, temp1=0;
+ int retval_low=0;
+ int whence;
KASSERT(curthread != NULL);
KASSERT(curthread->t_curspl == 0);
@@ -98,18 +107,64 @@ syscall(struct trapframe *tf)
*/
retval = 0;
-
+ // int64_t retval1;
switch (callno) {
case SYS_reboot:
- err = sys_reboot(tf->tf_a0);
+ err = sys_reboot(tf->tf_a0);
break;
case SYS___time:
- err = sys___time((userptr_t)tf->tf_a0,
- (userptr_t)tf->tf_a1);
+ err = sys___time((userptr_t)tf->tf_a0,
+ (userptr_t)tf->tf_a1);
+ break;
+ case SYS_open:
+ err = sys_open((userptr_t) tf->tf_a0, tf->tf_a1, tf->tf_a2,&retval);
+ break;
+
+ case SYS_read:
+ err = sys_read(tf->tf_a0, (userptr_t)tf->tf_a1, tf->tf_a2,&retval);
break;
- /* Add stuff here */
+ case SYS_write:
+ err = sys_write(tf->tf_a0, (userptr_t)tf->tf_a1, tf->tf_a2,&retval);
+ break;
+
+ case SYS_close:
+ err=sys_close(tf->tf_a0, &retval);
+ break;
+
+ case SYS_fork:
+ err = 0;
+ err = sys_fork(tf,&retval);
+ break;
+ case SYS_getpid:
+ err = 0;
+ err = justpid(&retval);
+ break;
+ case SYS_waitpid:
+ err =0;
+ err = waitpid((pid_t)tf->tf_a0,(int *)tf->tf_a1,tf->tf_a2,&retval);
+ break;
+ case SYS__exit:
+ err =0;
+ sys_exit(tf->tf_a0);
+ break;
+ case SYS_lseek:
+ pos = (off_t) ((off_t)tf->tf_a2 << 32 | (off_t)tf->tf_a3);
+ //kprintf("\na2_high: %d\na3_low: %d\npos: %llu\n",tf->tf_a2,tf->tf_a3,pos);
+ err = copyin((const_userptr_t)tf->tf_sp+16,&whence,sizeof(int));
+ if(!err)
+ {
+ err = sys_lseek(tf->tf_a0,pos,whence,&retval64);
+ if(!err)
+ {
+ retval = retval64 >> 32;
+ retval_low = (int) retval64;
+ }
+ }
+ break;
+
+ /* Add stuff here */
default:
kprintf("Unknown syscall %d\n", callno);
@@ -130,6 +185,7 @@ syscall(struct trapframe *tf)
else {
/* Success. */
tf->tf_v0 = retval;
+ tf->tf_v1 = retval_low;
tf->tf_a3 = 0; /* signal no error */
}
@@ -155,7 +211,33 @@ syscall(struct trapframe *tf)
* Thus, you can trash it and do things another way if you prefer.
*/
void
-enter_forked_process(struct trapframe *tf)
+enter_forked_process(void *data1,unsigned long data2)
{
- (void)tf;
+
+ struct trapframe* child_tf;
+ struct addrspace *child_addr;
+
+ data1 = (struct trapframe *)data1;
+ child_tf = data1;
+
+ child_tf->tf_a3 = 0;
+ child_tf->tf_v0 = 0;
+
+ child_tf->tf_epc = child_tf->tf_epc + 4;
+
+
+
+
+ child_addr = (struct addrspace *)data2;
+
+ as_copy(child_addr,&curthread->t_addrspace);
+ as_activate(curthread->t_addrspace);
+
+ mips_usermode(child_tf);
+
+ (void)data1;
+ (void)data2;
+
}
+
+
diff --git a/kern/conf/conf.kern b/kern/conf/conf.kern
index d527f61..13a8e96 100644
--- a/kern/conf/conf.kern
+++ b/kern/conf/conf.kern
@@ -367,6 +367,8 @@ file vfs/devnull.c
file syscall/loadelf.c
file syscall/runprogram.c
file syscall/time_syscalls.c
+file syscall/file.c
+file syscall/proc.c
#
# Startup and initialization
diff --git a/kern/include/file.h b/kern/include/file.h
new file mode 100644
index 0000000..7895618
--- /dev/null
+++ b/kern/include/file.h
@@ -0,0 +1,31 @@
+#ifndef _FILE_H_
+#define _FILE_H_
+
+#include <lib.h>
+#include <device.h>
+#include <uio.h>
+#include <fs.h>
+#include <vnode.h>
+#include <thread.h>
+
+#define MAX_FILENAME_LEN 1024
+
+struct file_desc{
+ char name[MAX_FILENAME_LEN];
+ struct vnode* vn;
+ int flags;
+ off_t offset;
+ int ref_count;
+ struct lock* lock;
+};
+
+
+//int fd_table_init(struct thread *thr);
+int fd_table_init(struct thread *thr);
+int sys_open(userptr_t filename, int flags, int mode, int *retval);
+int sys_close(int fd, int *retval);
+int sys_read(int fd, userptr_t buf, size_t buflen, int *retval);
+int sys_write(int fd, userptr_t buf, size_t nbytes, int *retval);
+int sys_lseek(int fd, off_t pos, int whence, off_t *retval);
+
+#endif
diff --git a/kern/include/proc.h b/kern/include/proc.h
new file mode 100644
index 0000000..e8cf0e6
--- /dev/null
+++ b/kern/include/proc.h
@@ -0,0 +1,32 @@
+#ifndef _PROC_H_
+#define _PROC_H_
+
+struct proc_table{
+
+ pid_t pid;
+ pid_t ppid;
+ struct thread *proc_thread;
+ bool status;
+ int exit_code;
+ struct lock *proc_lock;
+ struct cv *proc_cv;
+ };
+
+
+// function listings
+
+// sys_fork
+// sys_execv
+// sys_waitpid
+
+pid_t makepid(struct thread *pid_thread);
+int justpid(int *retval);
+int sys_fork(struct trapframe *tf,int *retval);
+int waitpid(pid_t pid, int *status, int options,int *retval);
+void sys_exit(int exitcode);
+void mywait(struct proc_table *pro);
+
+
+
+#endif /* proc_h_ */
+
diff --git a/kern/include/synch.h b/kern/include/synch.h
index ac3714b..8b0cdf9 100644
--- a/kern/include/synch.h
+++ b/kern/include/synch.h
@@ -74,10 +74,16 @@ void V(struct semaphore *);
*/
struct lock {
char *lk_name;
+ struct wchan *lk_wchan;
+ struct spinlock lk_lock;
+ volatile int lk_held;
+ struct thread *lk_owner;
// add what you need here
// (don't forget to mark things volatile as needed)
};
+#define MAXCNT 1000 //Maximum number of simultaneous readers.
+
struct lock *lock_create(const char *name);
void lock_acquire(struct lock *);
@@ -115,6 +121,7 @@ struct cv {
char *cv_name;
// add what you need here
// (don't forget to mark things volatile as needed)
+ struct wchan *cv_wchan;
};
struct cv *cv_create(const char *name);
@@ -143,6 +150,8 @@ void cv_broadcast(struct cv *cv, struct lock *lock);
struct rwlock {
char *rwlock_name;
+ struct semaphore *rw_sem;
+ struct lock *rw_lock;
};
struct rwlock * rwlock_create(const char *);
diff --git a/kern/include/syscall.h b/kern/include/syscall.h
index befd3d8..62bf37f 100644
--- a/kern/include/syscall.h
+++ b/kern/include/syscall.h
@@ -44,7 +44,7 @@ void syscall(struct trapframe *tf);
*/
/* Helper for fork(). You write this. */
-void enter_forked_process(struct trapframe *tf);
+void enter_forked_process(void *data1,unsigned long data2);
/* Enter user mode. Does not return. */
void enter_new_process(int argc, userptr_t argv, vaddr_t stackptr,
diff --git a/kern/include/thread.h b/kern/include/thread.h
index 86706ca..a888c17 100644
--- a/kern/include/thread.h
+++ b/kern/include/thread.h
@@ -38,6 +38,8 @@
#include <spinlock.h>
#include <threadlist.h>
+#include <limits.h>
+#include <file.h>
struct addrspace;
struct cpu;
@@ -112,6 +114,13 @@ struct thread {
struct vnode *t_cwd; /* current working directory */
/* add more here as needed */
+
+ /* File descriptor table */
+ struct file_desc* fd_table[OPEN_MAX];
+
+ pid_t pid;
+ pid_t ppid;
+
};
/* Call once during system startup to allocate data structures. */
diff --git a/kern/startup/main.c b/kern/startup/main.c
index be4c4b8..cf302eb 100644
--- a/kern/startup/main.c
+++ b/kern/startup/main.c
@@ -100,7 +100,7 @@ boot(void)
kprintf("%s", harvard_copyright);
kprintf("\n");
- kprintf("Put-your-group-name-here's system version %s (%s #%d)\n",
+ kprintf("msaiyed@buffalo.edu avishekd@buffalo.edu's system version %s (%s #%d)\n",
GROUP_VERSION, buildconfig, buildversion);
kprintf("\n");
diff --git a/kern/startup/menu.c b/kern/startup/menu.c
index 6c71551..a431f8d 100644
--- a/kern/startup/menu.c
+++ b/kern/startup/menu.c
@@ -40,6 +40,9 @@
#include <sfs.h>
#include <syscall.h>
#include <test.h>
+#include <synch.h>
+#include <current.h>
+#include <proc.h>
#include "opt-synchprobs.h"
#include "opt-sfs.h"
#include "opt-net.h"
@@ -53,6 +56,9 @@
#define MAXMENUARGS 16
// XXX this should not be in this file
+
+extern struct proc_table *proc[20];
+
void
getinterval(time_t s1, uint32_t ns1, time_t s2, uint32_t ns2,
time_t *rs, uint32_t *rns)
@@ -127,7 +133,8 @@ int
common_prog(int nargs, char **args)
{
int result;
-
+ // pid_t pid;
+ // struct proc_table *singlep;
#if OPT_SYNCHPROBS
kprintf("Warning: this probably won't work with a "
"synchronization-problems kernel.\n");
@@ -141,6 +148,20 @@ common_prog(int nargs, char **args)
kprintf("thread_fork failed: %s\n", strerror(result));
return result;
}
+
+ proc[1] = (struct proc_table *)kmalloc(sizeof(struct proc_table));
+
+ curthread->pid = 1;
+ proc[1]->pid = 1;
+ proc[1]->exit_code = 0;
+ proc[1]->status = 0;
+ proc[1]->proc_lock = lock_create("Process_lock");
+ proc[1]->proc_cv = cv_create("Process_cv");
+ proc[1]->proc_thread = curthread;
+
+ lock_acquire(proc[1]->proc_lock);
+ mywait(proc[1]);
+ lock_release(proc[1]->proc_lock);
return 0;
}
diff --git a/kern/synchprobs/problems.c b/kern/synchprobs/problems.c
index 81d2f0e..7e38240 100644
--- a/kern/synchprobs/problems.c
+++ b/kern/synchprobs/problems.c
@@ -47,15 +47,43 @@
// functions will allow you to do local initialization. They are called at
// the top of the corresponding driver code.
+static int male_cnt=0, female_cnt=0, matchmaker_cnt=0;
+static struct lock *lock;
+static struct cv *male_cv, *female_cv, *matchmaker_cv;
+
+//Variables for Buffalo Intersections
+static struct lock *lock_quad[4], *lock_getlock;
+
void whalemating_init() {
- return;
+
+ lock=lock_create("lock");
+ if(lock==NULL) {
+ panic("Can't create lock in whale mating.");
+ }
+
+ male_cv=cv_create("male_cv");
+ if(male_cv==NULL) {
+ panic("can't create male cv");
+ }
+ female_cv=cv_create("female_cv");
+ if(female_cv==NULL) {
+ panic("can't create female cv");
+ }
+ matchmaker_cv=cv_create("matchmaker_cv");
+ if(matchmaker_cv==NULL) {
+ panic("can't create matchmaker cv");
+ }
+
}
// 20 Feb 2012 : GWA : Adding at the suggestion of Nikhil Londhe. We don't
// care if your problems leak memory, but if you do, use this to clean up.
void whalemating_cleanup() {
- return;
+ lock_destroy(lock);
+ cv_destroy(male_cv);
+ cv_destroy(female_cv);
+ cv_destroy(matchmaker_cv);
}
void
@@ -65,7 +93,22 @@ male(void *p, unsigned long which)
(void)which;
male_start();
+
// Implement this function
+ lock_acquire(lock);
+ male_cnt++;
+ if(female_cnt>0 && matchmaker_cnt>0)
+ {
+ cv_signal(female_cv,lock);
+ cv_signal(matchmaker_cv,lock);
+ }
+ else
+ {
+ cv_wait(male_cv,lock);
+ }
+ male_cnt--;
+ lock_release(lock);
+
male_end();
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
@@ -81,7 +124,22 @@ female(void *p, unsigned long which)
(void)which;
female_start();
+
// Implement this function
+ lock_acquire(lock);
+ female_cnt++;
+ if(male_cnt>0 && matchmaker_cnt>0)
+ {
+ cv_signal(male_cv,lock);
+ cv_signal(matchmaker_cv,lock);
+ }
+ else
+ {
+ cv_wait(female_cv,lock);
+ }
+ female_cnt--;
+ lock_release(lock);
+
female_end();
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
@@ -97,7 +155,22 @@ matchmaker(void *p, unsigned long which)
(void)which;
matchmaker_start();
+
// Implement this function
+ lock_acquire(lock);
+ matchmaker_cnt++;
+ if(male_cnt>0 && female_cnt>0)
+ {
+ cv_signal(male_cv,lock);
+ cv_signal(female_cv,lock);
+ }
+ else
+ {
+ cv_wait(matchmaker_cv,lock);
+ }
+ matchmaker_cnt--;
+ lock_release(lock);
+
matchmaker_end();
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
@@ -138,22 +211,57 @@ matchmaker(void *p, unsigned long which)
// the top of the corresponding driver code.
void stoplight_init() {
- return;
+ lock_quad[0]=lock_create("lock1");
+ if(lock_quad[0]==NULL) {
+ panic("Can't create lock 1 in buffalo intersection.");
+ }
+ lock_quad[1]=lock_create("lock2");
+ if(lock_quad[1]==NULL) {
+ panic("Can't create lock 2 in buffalo intersection.");
+ }
+ lock_quad[2]=lock_create("lock3");
+ if(lock_quad[2]==NULL) {
+ panic("Can't create lock 3 in buffalo intersection.");
+ }
+ lock_quad[3]=lock_create("lock4");
+ if(lock_quad[3]==NULL) {
+ panic("Can't create lock 4 in buffalo intersection.");
+ }
+ lock_getlock=lock_create("lockGetLock");
+ if(lock_getlock==NULL) {
+ panic("Can't create lock getLock in buffalo intersection.");
+ }
+ //return;
}
// 20 Feb 2012 : GWA : Adding at the suggestion of Nikhil Londhe. We don't
// care if your problems leak memory, but if you do, use this to clean up.
void stoplight_cleanup() {
- return;
+ lock_destroy(lock_quad[0]);
+ lock_destroy(lock_quad[1]);
+ lock_destroy(lock_quad[2]);
+ lock_destroy(lock_quad[3]);
+ lock_destroy(lock_getlock);
+ //return;
}
void
gostraight(void *p, unsigned long direction)
{
struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
- (void)direction;
+ //(void)direction;
+ lock_acquire(lock_getlock);
+ lock_acquire(lock_quad[direction]);
+ lock_acquire(lock_quad[(direction+3)%4]);
+ lock_release(lock_getlock);
+ inQuadrant(direction); //in first quadrant (from 2 quadrants)
+ inQuadrant((direction+3)%4); //in second quadrant (from 2 quadrants)
+ leaveIntersection(); //leave intersection
+ lock_release(lock_quad[direction]);
+ lock_release(lock_quad[(direction+3)%4]);
+
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
// stoplight driver can return to the menu cleanly.
V(stoplightMenuSemaphore);
@@ -164,7 +272,20 @@ void
turnleft(void *p, unsigned long direction)
{
struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
- (void)direction;
+ //(void)direction;
+
+ lock_acquire(lock_getlock);
+ lock_acquire(lock_quad[direction]);
+ lock_acquire(lock_quad[(direction+3)%4]);
+ lock_acquire(lock_quad[(direction+2)%4]);
+ lock_release(lock_getlock);
+ inQuadrant(direction); //in first quadrant (from 3 quadrants)
+ inQuadrant((direction+3)%4); //in second quadrant (from 3 quadrants)
+ inQuadrant((direction+2)%4); //in third quadrant (from 3 quadrants)
+ leaveIntersection(); //leave intersection
+ lock_release(lock_quad[direction]);
+ lock_release(lock_quad[(direction+3)%4]);
+ lock_release(lock_quad[(direction+2)%4]);
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
// stoplight driver can return to the menu cleanly.
@@ -176,7 +297,14 @@ void
turnright(void *p, unsigned long direction)
{
struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
- (void)direction;
+ //(void)direction;
+
+ lock_acquire(lock_getlock);
+ lock_acquire(lock_quad[direction]);
+ lock_release(lock_getlock);
+ inQuadrant(direction);
+ leaveIntersection();
+ lock_release(lock_quad[direction]);
// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
// stoplight driver can return to the menu cleanly.
diff --git a/kern/syscall/.proc.c.swo b/kern/syscall/.proc.c.swo
new file mode 100644
index 0000000..9b5de8f
Binary files /dev/null and b/kern/syscall/.proc.c.swo differ
diff --git a/kern/syscall/file.c b/kern/syscall/file.c
new file mode 100644
index 0000000..e011bba
--- /dev/null
+++ b/kern/syscall/file.c
@@ -0,0 +1,400 @@
+/*
+ File system calls
+*/
+
+#include <types.h>
+#include <kern/errno.h>
+#include <kern/fcntl.h>
+#include <lib.h>
+#include <array.h>
+#include <spl.h>
+#include <thread.h>
+#include <current.h>
+#include <vfs.h>
+#include <synch.h>
+#include <mainbus.h>
+#include <vnode.h>
+#include <file.h>
+#include <copyinout.h>
+#include <kern/stat.h>
+#include <kern/seek.h>
+
+
+int fd_table_init(struct thread *thr)
+{
+ int result;
+ char fname[]="con:";
+ char fname1[]="con:";
+ char fname2[]="con:";
+ struct vnode *v0;
+ struct vnode *v1;
+ struct vnode *v2;
+
+ if(thr->fd_table[0] == NULL)
+ {
+ thr->fd_table[0] = (struct file_desc *)kmalloc(sizeof(struct file_desc));
+ result = vfs_open(fname, O_RDONLY, 0664, &v0);
+ if(result)
+ {
+ kprintf("file table initialization failed\n");
+ return EINVAL;
+ }
+ strcpy(thr->fd_table[0]->name,fname);
+ thr->fd_table[0]->vn = v0;
+ thr->fd_table[0]->flags = O_RDONLY;
+ thr->fd_table[0]->offset = 0;
+ thr->fd_table[0]->lock = lock_create(fname);
+ thr->fd_table[0]->ref_count=1;
+ }
+
+ if(thr->fd_table[1] == NULL)
+ {
+ thr->fd_table[1] = (struct file_desc *)kmalloc(sizeof(struct file_desc));
+ result = vfs_open(fname1, O_WRONLY, 0664, &v1);
+ if(result)
+ {
+ kprintf("file table initialization failed\n");
+ return EINVAL;
+ }
+ strcpy(thr->fd_table[1]->name,fname);
+ thr->fd_table[1]->vn = v1;
+ thr->fd_table[1]->flags = O_WRONLY;
+ thr->fd_table[1]->offset = 0;
+ thr->fd_table[1]->lock = lock_create(fname);
+ thr->fd_table[1]->ref_count=1;
+ }
+
+ if(thr->fd_table[2] == NULL)
+ {
+ thr->fd_table[2] = (struct file_desc *)kmalloc(sizeof(struct file_desc));
+ result = vfs_open(fname2, O_WRONLY, 0664, &v2);
+ if(result)
+ {
+ kprintf("file table initialization failed2\n");
+ return EINVAL;
+ }
+ strcpy(thr->fd_table[2]->name,fname);
+ thr->fd_table[2]->vn = v2;
+ thr->fd_table[2]->flags = O_WRONLY;
+ thr->fd_table[2]->offset = 0;
+ thr->fd_table[2]->lock = lock_create(fname);
+ thr->fd_table[2]->ref_count=1;
+ }
+
+ return 0;
+
+}
+
+int sys_open(userptr_t filename, int flags, int mode, int *retval)
+{
+ int result, i;
+ struct vnode *v;
+ size_t siz;
+ char *fname = (char *)kmalloc(sizeof(char)*PATH_MAX);
+
+ if(!(flags&O_RDONLY || flags&O_WRONLY || flags&O_RDWR))
+ return EINVAL;
+
+ if(filename==NULL)
+ return EFAULT;
+
+ result=copyinstr(filename, fname, PATH_MAX, &siz);
+ if(result)
+ return result;
+
+ //Check for modes remaining.
+
+ for(i=3;i<OPEN_MAX;i++)
+ {
+ if(curthread->fd_table[i]==NULL)
+ break;
+ }
+ if(i==OPEN_MAX)
+ return ENFILE;
+
+ curthread->fd_table[i] = (struct file_desc *)kmalloc(sizeof(struct file_desc));
+
+ if(curthread->fd_table[i] == NULL)
+ {
+ return ENFILE;
+ }
+
+ result = vfs_open(fname, flags, mode, &v);
+ //kprintf("vfs_open result: %d %d",result,flags);
+ if(result)
+ return result;
+
+ strcpy(curthread->fd_table[i]->name,fname);
+ curthread->fd_table[i]->vn = v;
+ curthread->fd_table[i]->flags = flags;
+ curthread->fd_table[i]->offset = 0;
+ curthread->fd_table[i]->lock = lock_create(fname);
+ curthread->fd_table[i]->ref_count=1;
+
+ kfree(fname);
+
+ *retval=i;
+
+ return 0;
+
+}
+
+int sys_close(int fd, int *retval)
+{
+ if(fd<3 || fd>OPEN_MAX)
+ {
+ //*retval=EBADF;
+ return EBADF;
+ }
+ if(curthread->fd_table[fd]==NULL)
+ {
+ //*retval=EBADF;
+ return EBADF;
+ }
+
+ lock_acquire(curthread->fd_table[fd]->lock);
+ vfs_close(curthread->fd_table[fd]->vn);
+ if(curthread->fd_table[fd]->ref_count==1)
+ {
+ lock_release(curthread->fd_table[fd]->lock);
+ lock_destroy(curthread->fd_table[fd]->lock);
+ kfree(curthread->fd_table[fd]);
+ curthread->fd_table[fd]=NULL;
+ }
+ else
+ {
+ KASSERT(curthread->fd_table[fd]->ref_count > 1);
+ curthread->fd_table[fd]->ref_count--;
+ lock_release(curthread->fd_table[fd]->lock);
+ }
+ *retval=0;
+ return 0;
+}
+
+int sys_read(int fd, userptr_t buf, size_t buflen, int *retval)
+{
+ int result,mode;
+ struct iovec iov;
+ struct uio u_io;
+ size_t lenread;
+
+ if(fd<1 || fd>OPEN_MAX)
+ {
+ //*retval=EBADF;
+ return EBADF;
+ }
+ if(curthread->fd_table[fd]==NULL)
+ {
+ //*retval=EBADF;
+ return EBADF;
+ }
+
+ lock_acquire(curthread->fd_table[fd]->lock);
+
+ //void *kbuff=kmalloc(sizeof(buflen));
+
+ mode=curthread->fd_table[fd]->flags & O_ACCMODE;
+ if(mode == O_WRONLY)
+ {
+ lock_release(curthread->fd_table[fd]->lock);
+ return EBADF;
+ }
+
+ //uio_kinit(struct iovec *iov, struct uio *u, void *kbuf, size_t len, off_t pos, enum uio_rw rw)
+ uio_kinit(&iov, &u_io, buf, buflen, curthread->fd_table[fd]->offset, UIO_READ);
+ result = VOP_READ(curthread->fd_table[fd]->vn, &u_io);
+ if (result) {
+ lock_release(curthread->fd_table[fd]->lock);
+ return result;
+ }
+
+ //copyout(kbuff,buf,buflen);
+ //char *c=(char*)kbuff;
+ //kprintf("%c",*c);
+
+ curthread->fd_table[fd]->offset = u_io.uio_offset;
+ lenread = buflen - u_io.uio_resid;
+
+ lock_release(curthread->fd_table[fd]->lock);
+
+ *retval=lenread;
+ return 0;
+}
+
+int sys_write(int fd, userptr_t buf, size_t nbytes, int *retval)
+{
+ int result;
+ struct iovec iov;
+ struct uio u_io;
+ //size_t siz;
+ size_t lenread;
+
+ if(fd<1 || fd>OPEN_MAX)
+ {
+ //*retval=EBADF;
+ return EBADF;
+ }
+ if(curthread->fd_table[fd]==NULL)
+ {
+ //*retval=EBADF;
+ return EBADF;
+ }
+
+ lock_acquire(curthread->fd_table[fd]->lock);
+
+ //Not opened in RW or write mode.
+ if(!(curthread->fd_table[fd]->flags & O_WRONLY || curthread->fd_table[fd]->flags & O_RDWR))
+ {
+ lock_release(curthread->fd_table[fd]->lock);
+ return EBADF;
+ }
+
+ /*char *kbuff = (char *)kmalloc(nbytes);
+ copyinstr((userptr_t)buf, kbuff, strlen(kbuff), &siz);*/
+ //char *c=kbuff;
+ //kprintf("%c",*c);
+
+ uio_kinit(&iov, &u_io, buf, nbytes, curthread->fd_table[fd]->offset, UIO_WRITE);
+ result = VOP_WRITE(curthread->fd_table[fd]->vn, &u_io);
+ if (result)
+ {
+ lock_release(curthread->fd_table[fd]->lock);
+ return result;
+ }
+
+ curthread->fd_table[fd]->offset = u_io.uio_offset;
+ lenread = nbytes - u_io.uio_resid;
+
+ lock_release(curthread->fd_table[fd]->lock);
+ //kprintf("\n\nhii\n\n");
+ //result=lenread;
+ *retval=lenread;
+ return 0;
+}
+
+int sys_lseek(int fd, off_t pos, int whence, off_t *retval)
+{
+ int result;
+ off_t end;
+ struct stat statptr;
+
+ //kprintf("target: %llu",pos);
+
+ if(fd<1 || fd>OPEN_MAX)
+ {
+ //*retval=EBADF;
+ return EBADF;
+ }
+ if(curthread->fd_table[fd]==NULL)
+ {
+ //*retval=EBADF;
+ return EBADF;
+ }
+
+ lock_acquire(curthread->fd_table[fd]->lock);
+
+ if(pos<0)
+ {
+ lock_release(curthread->fd_table[fd]->lock);
+ return EINVAL;
+ }
+
+ if(!(whence==SEEK_SET || whence==SEEK_CUR || whence==SEEK_END))
+ {
+ lock_release(curthread->fd_table[fd]->lock);
+ return EINVAL;
+ }
+
+ if(whence==SEEK_CUR)
+ {
+ pos = curthread->fd_table[fd]->offset + pos;
+ }
+ else if (whence==SEEK_END)
+ {
+ VOP_STAT(curthread->fd_table[fd]->vn, &statptr);
+ end = statptr.st_size;
+ pos = pos + end;
+ }
+ result = VOP_TRYSEEK(curthread->fd_table[fd]->vn, pos);
+ if (result) {
+ lock_release(curthread->fd_table[fd]->lock);
+ return result;
+ }
+
+ curthread->fd_table[fd]->offset = pos;
+
+ lock_release(curthread->fd_table[fd]->lock);
+
+ //kprintf("final: %llu\n",pos);
+ *retval=pos;
+ return 0;
+}
+
+/*
+int sys__getcwd(char *buf,size_t buflen,int *retval)
+{
+// int vfs_getcwd(struct uio *buf);
+
+ // Hence need to initialize a uio structure
+
+ struct iovec iov;
+ struct uio ku;
+ size_t size;
+
+ char *filename = (char *)kmalloc(buflen);
+
+ // need to call kinit and then store result of getcwd into an int variable
+ // need to call kinit
+ uio_kinit(&iov,&ku,&filename,buflen-1,0,UIO_READ);
+ int result = vfs_getcwd(&ku);
+ if(result)
+ {
+ // kprintf("vfs_getcwd failed (%s) \n",stderror(result));
+ return result;
+ }
+
+ // references taken from menu.c
+
+ // null terminate
+ filename[buflen-1-ku.uio_resid] = 0;
+
+ //need to copy the kernel buffer to the user buffer else there are compile problems
+ copyoutstr(filename,(userptr_t)buf,buflen,&size);
+ *retval = buflen-1-ku.uio_resid;
+ result = buflen-1-ku.uio_resid;
+ kfree(filename);
+ return result;
+
+}
+// check and change
+int sys_chdir(const char *pathname,int *retval)
+{
+ // need to use vfs_chdir
+ // before that need to setup
+ int result;
+ size_t size;
+
+ // no other process should acces the file while it is having its path changed
+ int a = splhigh();
+ // need to make a copy of the pathname and copy it to a new kernel buffer
+ // using copyinstr
+ char *path = (char *)kmalloc(sizeof(char)*PATH_MAX);
+ copyinstr((const_userptr_t)pathname,path,PATH_MAX,&size);
+ // the pathname now is in the kernel buffer
+
+ result = vfs_chdir(path);
+ if(result)
+ {
+ splx(a);
+ return result;
+ }
+
+ // success condition for pathname : it has been changed
+
+ *retval = 0;
+ kfree(path);
+ splx(a);
+ return 0;