Skip to content

Commit 798f38b

Browse files
Finish Lab 2
1 parent a46a9f6 commit 798f38b

File tree

19 files changed

+178
-58
lines changed

19 files changed

+178
-58
lines changed

ASST0.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/bin/bash
22

3+
4+
cd /root/cs350-os161/os161-1.99
5+
./configure --ostree=/root/cs350-os161/root --toolprefix=cs350-
6+
7+
38
cd /root/cs350-os161/os161-1.99/kern/conf
49

510
./config ASST0
@@ -22,5 +27,5 @@ bmake install
2227
# Running the kernel
2328

2429
cd /root/cs350-os161/root
25-
exec bash
26-
# sys161 kernel-ASST0 #execute/run the kernel
30+
#exec bash
31+
sys161 kernel-ASST0 #execute/run the kernel

os161-1.99/kern/arch/mips/syscall/syscall.c

+7-11
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ syscall(struct trapframe *tf)
109109
(userptr_t)tf->tf_a1);
110110
break;
111111

112-
// Lab 2
113-
case SYS___exitwithcode: // Lab 2: Part B
114-
sys__exit((int)tf->tf_a0);
115-
/* sys__exit does not return, execution should not get here */
116-
panic("unexpected return from sys__exit");
117-
break;
112+
// Lab 2: Part B
113+
case SYS__exit:
114+
sys__exit((int)tf->tf_a0);
115+
/* sys__exit does not return, execution should not get here */
116+
panic("unexpected return from sys__exit");
117+
break;
118118

119119
case SYS_printint: // Lab 2: Part C
120120
err = sys_printint((int)tf->tf_a0);
@@ -130,11 +130,7 @@ syscall(struct trapframe *tf)
130130
(int)tf->tf_a2,
131131
(int *)(&retval));
132132
break;
133-
case SYS__exit:
134-
sys__exit((int)tf->tf_a0);
135-
/* sys__exit does not return, execution should not get here */
136-
panic("unexpected return from sys__exit");
137-
break;
133+
138134
case SYS_getpid:
139135
err = sys_getpid((pid_t *)&retval);
140136
break;

os161-1.99/kern/include/kern/syscall.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#define SYS_fork 0
5151
#define SYS_vfork 1
5252
#define SYS_execv 2
53+
// Lab 2: Part B
5354
#define SYS__exit 3
5455
#define SYS_waitpid 4
5556
#define SYS_getpid 5
@@ -198,9 +199,10 @@
198199
//#define SYS___sysctl 120
199200

200201
// Lab 2
201-
#define SYS___exitwithcode 121 // Lab 2: Part B
202-
#define SYS_printint 122 // Lab 2: Part C
203-
#define SYS_reversestring 123 // Lab 2: Part D
202+
// Lab 2: part C
203+
#define SYS_printint 122
204+
// Lab 2: Part D
205+
#define SYS_reversestring 123
204206

205207
/*CALLEND*/
206208

os161-1.99/kern/include/syscall.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ int sys_reboot(int code);
5959
int sys___time(userptr_t user_seconds, userptr_t user_nanoseconds);
6060

6161

62-
// Lab 2
63-
void sys___exit(int exitcode); // Lab 2: Part B
64-
int sys_printint(int c); // Lab 2: Part C
65-
int sys_reversestring(const char *str, int len); // Lab 2: Part D
62+
/*************** Lab 2 ***************/
63+
// Lab 2: Part C
64+
int sys_printint(int c);
65+
// Lab 2: Part D
66+
int sys_reversestring(const char *str, int len);
6667

6768

6869
#ifdef UW
6970
int sys_write(int fdesc,userptr_t ubuf,unsigned int nbytes,int *retval);
71+
// Lab 2: Part B
7072
void sys__exit(int exitcode);
7173
int sys_getpid(pid_t *retval);
7274
int sys_waitpid(pid_t pid, userptr_t status, int options, pid_t *retval);

os161-1.99/kern/include/thread.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ int thread_fork(const char *name, struct proc *proc,
148148
* Cause the current thread to exit.
149149
* Interrupts need not be disabled.
150150
*/
151-
void thread_exit(void);
151+
void thread_exit(int exitcode);
152152

153153
/*
154154
* Cause the current thread to yield to the next runnable thread, but

os161-1.99/kern/startup/menu.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ cmd_quit(int nargs, char **args)
285285

286286
vfs_sync();
287287
sys_reboot(RB_POWEROFF);
288-
thread_exit();
288+
thread_exit(0); // for now, I'm passing this value to just print something
289289
return 0;
290290
}
291291

os161-1.99/kern/syscall/proc_syscalls.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
/* this implementation of sys__exit does not do anything with the exit code */
1414
/* this needs to be fixed to get exit() and waitpid() working properly */
1515

16+
// Lab 2: Part B
1617
void sys__exit(int exitcode) {
1718

1819
struct addrspace *as;
@@ -43,7 +44,10 @@ void sys__exit(int exitcode) {
4344
will wake up the kernel menu thread */
4445
proc_destroy(p);
4546

46-
thread_exit();
47+
48+
// Lab 2: Part B
49+
thread_exit(exitcode);
50+
4751
/* thread_exit() does not return, so we should never get here */
4852
panic("return from thread_exit in sys_exit\n");
4953
}

os161-1.99/kern/syscall/simple_syscalls.c

+12-19
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,33 @@
66
#include <kern/unistd.h>
77
#include <current.h>
88

9-
// Lab 2: Part B
10-
void sys___exit(int exitcode) {
11-
kprintf("exit: (%d)\n", exitcode);
12-
13-
kprintf("Thread %s exited with code %d\n", curthread->t_name, exitcode);
14-
15-
thread_exit();
16-
17-
panic("sys__exit: Thread did not exit properly\n");
18-
}
199

2010
// Lab 2: Part C
2111
int sys_printint(int c) {
22-
kprintf("Given Number: %d\n", c);
12+
kprintf("Number: %d\n", c);
2313

2414
return (c % 2 == 0) ? 0 : 1;
2515
}
2616

27-
// Implement a simplified system call named int reversestring(const char *str, int len)
28-
29-
// This system call should accept a string and length of the string as input and print the reverse of the string
30-
// using the internal kprintf( ) function. The return value should be 1 if the length of the string is multiple of
31-
// 5 or 0 otherwise.
17+
/*
18+
Implement a simplified system call named int reversestring(const char *str, int len)
19+
This system call should accept a string and length of the string as input and print the reverse of the string
20+
using the internal kprintf( ) function. The return value should be 1 if the length of the string is multiple of
21+
5 or 0 otherwise.
22+
*/
3223

3324
// Lab 2: Part D
3425
int sys_reversestring(const char *str, int len) {
3526
kprintf("Given String: %s\n", str);
36-
3727
for (int i = len - 1; i >= 0; i--) {
3828
kprintf("%c", str[i]);
3929
}
40-
4130
kprintf("\n");
31+
32+
int isStringLenMultipleOf5 = 1;
33+
(len % 5 != 0) ? isStringLenMultipleOf5 = 0 : 1;
34+
kprintf("Return value: %d\n", isStringLenMultipleOf5);
4235

43-
return (len % 5 == 0) ? 1 : 0;
36+
return isStringLenMultipleOf5;
4437
}
4538

os161-1.99/kern/test/synchtest.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ semtestthread(void *junk, unsigned long num)
123123
kprintf("\n");
124124
V(donesem);
125125
#ifdef UW
126-
thread_exit();
126+
thread_exit(0);
127127
#endif
128128
}
129129

@@ -176,7 +176,7 @@ fail(unsigned long num, const char *msg)
176176
lock_release(testlock);
177177

178178
V(donesem);
179-
thread_exit();
179+
thread_exit(0);
180180
}
181181

182182
static
@@ -220,7 +220,7 @@ locktestthread(void *junk, unsigned long num)
220220
}
221221
V(donesem);
222222
#ifdef UW
223-
thread_exit();
223+
thread_exit(0);
224224
#endif
225225
}
226226

@@ -288,7 +288,7 @@ cvtestthread(void *junk, unsigned long num)
288288
kprintf("That's too fast... you must be "
289289
"busy-looping\n");
290290
V(donesem);
291-
thread_exit();
291+
thread_exit(0);
292292
}
293293

294294
}
@@ -306,7 +306,7 @@ cvtestthread(void *junk, unsigned long num)
306306
}
307307
V(donesem);
308308
#ifdef UW
309-
thread_exit();
309+
thread_exit(0);
310310
#endif
311311
}
312312

os161-1.99/kern/test/uw-tests.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ add_thread(void *junk, unsigned long num)
8484

8585
}
8686
V(donesem);
87-
thread_exit();
87+
thread_exit(0);
8888
}
8989

9090
/* This thread substract values from a global variable. */
@@ -117,7 +117,7 @@ sub_thread(void *junk, unsigned long num)
117117

118118
}
119119
V(donesem);
120-
thread_exit();
120+
thread_exit(0);
121121
}
122122

123123
int
@@ -251,7 +251,7 @@ vmstats_thread(void *junk, unsigned long num)
251251
}
252252

253253
V(donesem);
254-
thread_exit();
254+
thread_exit(0);
255255
}
256256

257257
int

os161-1.99/kern/thread/thread.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ cpu_hatch(unsigned software_number)
403403
kprintf("cpu%u: %s\n", software_number, cpu_identify());
404404

405405
V(cpu_startup_sem);
406-
thread_exit();
406+
thread_exit(0);
407407
}
408408

409409
/*
@@ -765,7 +765,7 @@ thread_startup(void (*entrypoint)(void *data1, unsigned long data2),
765765
entrypoint(data1, data2);
766766

767767
/* Done. */
768-
thread_exit();
768+
thread_exit(0);
769769
}
770770

771771
/*
@@ -778,8 +778,10 @@ thread_startup(void (*entrypoint)(void *data1, unsigned long data2),
778778
* Does not return.
779779
*/
780780
void
781-
thread_exit(void)
781+
thread_exit(int exitcode)
782782
{
783+
784+
783785
struct thread *cur;
784786

785787
cur = curthread;
@@ -796,6 +798,12 @@ thread_exit(void)
796798
proc_remthread(cur);
797799
#endif // UW
798800

801+
// Lab 2: Part B - Doing a spin lock so the kprintf output is not jumbled or garbled
802+
spinlock_acquire(&curcpu->c_runqueue_lock);
803+
kprintf("Exiting thread with code \n");
804+
kprintf("%d\n", exitcode);
805+
spinlock_release(&curcpu->c_runqueue_lock);
806+
799807
/* Make sure we *are* detached (move this only if you're sure!) */
800808
KASSERT(cur->t_proc == NULL);
801809

os161-1.99/user/include/unistd.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@
114114
#define __DEAD
115115
#endif
116116

117-
// Lab 2
118-
int printint(int c); // Lab 2: Part C
119-
void _exit(int exitCode); // Lab 2: Part B
120-
int reversestring(const char *str, int len); // Lab 2: Part D
117+
// Lab 2: Part C
118+
int printint(int c);
119+
// Lab 2: Part D
120+
int reversestring(const char *str, int len);
121121

122122
/* Required. */
123123
__DEAD void _exit(int code);

os161-1.99/user/testbin/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ TOP=../..
88
SUBDIRS=add argtest badcall bigfile conman crash ctest dirconc dirseek \
99
dirtest f_test farm faulter filetest forkbomb forktest guzzle \
1010
hash hog huge kitchen malloctest matmult palin parallelvm psort \
11-
randcall rmdirtest rmtest sink sort sty tail testA tictac triplehuge \
11+
randcall rmdirtest rmtest sink sort sty tail testExit testPrint testReverseString tictac triplehuge \
1212
triplemat triplesort zero
1313

1414
# But not:
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Makefile for userthreads
2+
3+
TOP=../../..
4+
.include "$(TOP)/mk/os161.config.mk"
5+
6+
PROG=testExit
7+
SRCS=testExit.c
8+
BINDIR=/testbin
9+
10+
.include "$(TOP)/mk/os161.prog.mk"
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <unistd.h>
2+
#include <stdio.h>
3+
4+
5+
int exitcode = 42;
6+
7+
8+
9+
int
10+
main()
11+
{
12+
printf("Killing the thread\n");
13+
printf("calling exit\n");
14+
_exit(exitcode); // Lab 2: Part B
15+
16+
printf("This should not be printed since the thread is killed\n");
17+
return 0;
18+
};
19+
20+
21+
22+
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Makefile for userthreads
2+
3+
TOP=../../..
4+
.include "$(TOP)/mk/os161.config.mk"
5+
6+
PROG=testPrint
7+
SRCS=testPrint.c
8+
BINDIR=/testbin
9+
10+
.include "$(TOP)/mk/os161.prog.mk"
11+

0 commit comments

Comments
 (0)