-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.patch
More file actions
249 lines (242 loc) · 6.32 KB
/
commit.patch
File metadata and controls
249 lines (242 loc) · 6.32 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
diff --git a/Makefile b/Makefile
index cac799d..f881042 100644
--- a/Makefile
+++ b/Makefile
@@ -154,7 +154,10 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
-
+ $U/_sleep\
+ $U/_pingpong\
+ $U/_find\
+
ifeq ($(LAB),syscall)
UPROGS += \
diff --git a/kernel/main.c b/kernel/main.c
index 8a3dc2e..9c61c4f 100644
--- a/kernel/main.c
+++ b/kernel/main.c
@@ -9,6 +9,7 @@ volatile static int started = 0;
// start() jumps here in supervisor mode on all CPUs.
void main() {
if (cpuid() == 0) {
+ printf("[210110627] enter main, init kernel\n");
// consoleinit();
// printfinit();
printf("\n");
diff --git a/kernel/proc.c b/kernel/proc.c
index 1607145..1333ffa 100644
--- a/kernel/proc.c
+++ b/kernel/proc.c
@@ -183,6 +183,7 @@ uchar initcode[] = {0x17, 0x05, 0x00, 0x00, 0x13, 0x05, 0x45, 0x02, 0x97, 0x05,
// Set up first user process.
void userinit(void) {
+ printf("[210110627] enter userinit\n");
struct proc *p;
p = allocproc();
@@ -197,6 +198,7 @@ void userinit(void) {
p->trapframe->epc = 0; // user program counter
p->trapframe->sp = PGSIZE; // user stack pointer
+ printf("[210110627] copy initcode to first user process\n");
safestrcpy(p->name, "initcode", sizeof(p->name));
p->cwd = namei("/");
diff --git a/kernel/start.c b/kernel/start.c
index f704fee..ba50f19 100644
--- a/kernel/start.c
+++ b/kernel/start.c
@@ -52,7 +52,8 @@ void start() {
// init uart and printf
consoleinit();
printfinit();
- }
+ printf("[210110627] in start, init driver, interrupts and change mode\n");
+ }
// switch to supervisor mode and jump to main().
asm volatile("mret");
diff --git a/user/find.c b/user/find.c
new file mode 100644
index 0000000..f1e4cfb
--- /dev/null
+++ b/user/find.c
@@ -0,0 +1,78 @@
+#include "kernel/types.h"
+#include "kernel/stat.h"
+#include "user.h"
+#include "kernel/fs.h"
+
+// 获取文件名
+char *fmtname(char *path) {
+ static char buf[DIRSIZ + 1];
+ char *p;
+
+ // Find first character after last slash.
+ for (p = path + strlen(path); p >= path && *p != '/'; p--);
+ p++;
+
+ // Return blank-padded name.
+ if (strlen(p) >= DIRSIZ) return p;
+ memmove(buf, p, strlen(p));
+ memset(buf + strlen(p), ' ', DIRSIZ - strlen(p));
+ buf[strlen(p)] = 0; // 末尾补0
+ return buf;
+}
+
+void find(char* path, char* file_name){
+ char buf[512], *p;
+ int fd;
+ struct dirent de;
+ struct stat st;
+ // 得到该路径的文件描述符
+ if ((fd = open(path, 0)) < 0) {
+ fprintf(2, "find: cannot open %s\n", path);
+ return;
+ }
+ // 得到文件状态st
+ if (fstat(fd, &st) < 0) {
+ fprintf(2, "find: cannot stat %s\n", path);
+ close(fd);
+ return;
+ }
+
+ switch (st.type) {
+ case T_FILE:
+ // 该st有可能是一个文件
+ if((strcmp(fmtname(path), file_name)) == 0)
+ printf("%s\n", path);
+ break;
+
+ case T_DIR:
+ // 该st有可能是一个文件夹
+ if (strlen(path) + 1 + DIRSIZ + 1 > sizeof buf) { // 路径长度爆了
+ printf("find: path too long\n");
+ break;
+ }
+ strcpy(buf, path);
+ p = buf + strlen(buf);
+ *p++ = '/'; // buf="path/",且p指向其后一个位置
+ while (read(fd, &de, sizeof(de)) == sizeof(de)) { // 得到目录状态de,遍历该路径下每一个文件
+ if (de.inum == 0) continue;
+ if (strcmp(de.name, ".") == 0) continue; // .跳过递归
+ if (strcmp(de.name, "..") == 0) continue; // ..跳过递归
+ memmove(p, de.name, DIRSIZ); // de.name即文件名移到路径后面,buf="path/de.name"
+ p[DIRSIZ] = 0; // 末尾补0
+ find(buf, file_name); // 递归
+ }
+ break;
+ }
+ close(fd);
+}
+
+int main(int argc, char* argv[]){
+ if (argc != 3) {
+ printf("FIND NEED RIGHT ARGUMENTS!!!\n");
+ exit(1);
+ }
+ char* path = argv[1];
+ char* file_name = argv[2];
+ find(path, file_name);
+ exit(0);
+}
\ No newline at end of file
diff --git a/user/init.c b/user/init.c
index 9ca8790..8192adc 100644
--- a/user/init.c
+++ b/user/init.c
@@ -23,6 +23,7 @@ int main(void) {
for (;;) {
printf("init: starting sh\n");
+ printf("[210110627] start sh through execve\n");
pid = fork();
if (pid < 0) {
printf("init: fork failed\n");
diff --git a/user/pingpong.c b/user/pingpong.c
new file mode 100644
index 0000000..061ebb8
--- /dev/null
+++ b/user/pingpong.c
@@ -0,0 +1,63 @@
+#include "kernel/types.h"
+#include "kernel/stat.h"
+#include "user.h"
+
+
+int main(int argc, char* argv[]){
+ int pid_child, pid_parent; // 父子进程pid
+ int p1[2], p2[2]; // 两个管道
+ char buf[4]; //传输的东西
+ long len = sizeof(buf); //传输东西的长度
+ pipe(p1); // 创建管道,父写子读
+ pipe(p2); // 创建管道,子写父读
+ int child_status = 0;
+ if((pid_child=fork())<0){
+ printf("FORK ERROR!!!\n");
+ exit(1);
+ }
+ else if(pid_child == 0){
+ /* Child */
+ pid_child = getpid();
+ close(p1[1]);
+ close(p2[0]);
+
+ if(read(p1[0], buf, len) != len){
+ printf("CHILD READ ERROR!!!\n");
+ exit(1);
+ }
+ printf("%d: received %s\n", pid_child, buf);
+ close(p1[0]);
+
+ strcpy(buf, "pong");
+ if(write(p2[1], buf, len) != len){
+ printf("CHILD READ ERROR!!!\n");
+ exit(1);
+ }
+ close(p2[1]);
+ exit(0);
+ }
+ else{
+ /* Parent */
+ pid_parent = getpid();
+ close(p1[0]);
+ close(p2[1]);
+
+ strcpy(buf, "ping");
+ if(write(p1[1], buf, len) != len){
+ printf("CHILD READ ERROR!!!\n");
+ exit(1);
+ }
+ close(p1[1]);
+
+ if(read(p2[0], buf, len) != len){
+ printf("CHILD READ ERROR!!!\n");
+ exit(1);
+ }
+ printf("%d: received %s\n", pid_parent, buf);
+ close(p2[0]);
+
+ wait(&child_status);
+ exit(0);
+ }
+ exit(0); // 进程退出
+}
\ No newline at end of file
diff --git a/user/sleep.c b/user/sleep.c
new file mode 100644
index 0000000..217cdad
--- /dev/null
+++ b/user/sleep.c
@@ -0,0 +1,13 @@
+#include "kernel/types.h"
+#include "user.h"
+
+int main(int argc, char* argv[]){
+ if (argc != 2){
+ printf("ERROR!!!Sleep needs one argument!\n"); // 检查参数数量是否正确
+ exit(-1);
+ }
+ int ticks = atoi(argv[1]); // 将字符串参数转为整数
+ sleep(ticks); // 使用系统调用sleep
+ printf("Sleep~~~~~~~~~~~~~~~for a while\n");
+ exit(0); // 进程退出
+}
\ No newline at end of file