-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.patch
More file actions
598 lines (592 loc) · 14.4 KB
/
Copy pathcommit.patch
File metadata and controls
598 lines (592 loc) · 14.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
diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
new file mode 100644
index 0000000..4039bef
--- /dev/null
+++ b/.vscode/c_cpp_properties.json
@@ -0,0 +1,16 @@
+{
+ "configurations": [
+ {
+ "name": "Linux",
+ "includePath": [
+ "${workspaceFolder}/**"
+ ],
+ "defines": [],
+ "compilerPath": "/usr/bin/gcc",
+ "cStandard": "c17",
+ "cppStandard": "gnu++17",
+ "intelliSenseMode": "linux-gcc-x64"
+ }
+ ],
+ "version": 4
+}
\ No newline at end of file
diff --git a/Makefile b/Makefile
index cac799d..a15d043 100644
--- a/Makefile
+++ b/Makefile
@@ -154,8 +154,10 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
-
-
+ $U/_sleep\
+ $U/_pingpong\
+ $U/_find\
+
ifeq ($(LAB),syscall)
UPROGS += \
$U/_trace\
diff --git a/commit.patch b/commit.patch
new file mode 100644
index 0000000..4f59ca2
--- /dev/null
+++ b/commit.patch
@@ -0,0 +1,296 @@
+diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
+new file mode 100644
+index 0000000..4039bef
+--- /dev/null
++++ b/.vscode/c_cpp_properties.json
+@@ -0,0 +1,16 @@
++{
++ "configurations": [
++ {
++ "name": "Linux",
++ "includePath": [
++ "${workspaceFolder}/**"
++ ],
++ "defines": [],
++ "compilerPath": "/usr/bin/gcc",
++ "cStandard": "c17",
++ "cppStandard": "gnu++17",
++ "intelliSenseMode": "linux-gcc-x64"
++ }
++ ],
++ "version": 4
++}
+\ No newline at end of file
+diff --git a/Makefile b/Makefile
+index cac799d..a15d043 100644
+--- a/Makefile
++++ b/Makefile
+@@ -154,8 +154,10 @@ UPROGS=\
+ $U/_grind\
+ $U/_wc\
+ $U/_zombie\
+-
+-
++ $U/_sleep\
++ $U/_pingpong\
++ $U/_find\
++
+ ifeq ($(LAB),syscall)
+ UPROGS += \
+ $U/_trace\
+diff --git a/kernel/main.c b/kernel/main.c
+index 8a3dc2e..a7f62dd 100644
+--- a/kernel/main.c
++++ b/kernel/main.c
+@@ -10,7 +10,7 @@ volatile static int started = 0;
+ void main() {
+ if (cpuid() == 0) {
+ // consoleinit();
+- // printfinit();
++ printf("[210810201] enter main, init kernel\n");
+ printf("\n");
+ printf("xv6 kernel is booting\n");
+ printf("\n");
+diff --git a/kernel/proc.c b/kernel/proc.c
+index 1607145..bae4f89 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("[210810201] entry userinit\n");
+ struct proc *p;
+
+ p = allocproc();
+@@ -199,7 +200,7 @@ void userinit(void) {
+
+ safestrcpy(p->name, "initcode", sizeof(p->name));
+ p->cwd = namei("/");
+-
++ printf("[210810201] copy initcode to first user process\n");
+ p->state = RUNNABLE;
+
+ release(&p->lock);
+diff --git a/kernel/start.c b/kernel/start.c
+index f704fee..4225a28 100644
+--- a/kernel/start.c
++++ b/kernel/start.c
+@@ -18,6 +18,7 @@ extern void timervec();
+
+ // entry.S jumps here in machine mode on stack0.
+ void start() {
++
+ // set M Previous Privilege mode to Supervisor, for mret.
+ unsigned long x = r_mstatus();
+ x &= ~MSTATUS_MPP_MASK;
+@@ -52,8 +53,9 @@ void start() {
+ // init uart and printf
+ consoleinit();
+ printfinit();
++ printf("[210810201] 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..9c90381
+--- /dev/null
++++ b/user/find.c
+@@ -0,0 +1,91 @@
++#include "kernel/types.h"
++#include "kernel/stat.h"
++#include "user/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));
++ return buf;
++}
++
++void find(char *path,char *filename)
++{
++ char buf[512],*p;
++ int fd;
++ struct dirent de;
++ struct stat st; //status
++ if ((fd = open(path,0))<0)
++ {
++ fprintf(2, "find: cannot open %s\n", path);
++ return;
++ }
++
++ if (fstat(fd,&st)<0)
++ {
++ fprintf(2, "find: cannot stat %s\n", path);
++ close(fd);
++ return;
++ }
++ switch(st.type)
++ {
++ case T_FILE:
++ break;
++
++
++ case T_DIR:
++ if (strlen(path) + 1 + DIRSIZ + 1 > sizeof (buf))
++ {
++ printf("find: path too long\n");
++ break;
++ }
++ strcpy(buf,path);
++ p = buf + strlen(buf);
++ *p++ = '/';
++ while (read(fd, &de, sizeof(de)) == sizeof(de)) {
++ if (de.inum == 0) continue;
++ memmove(p, de.name, DIRSIZ);
++ p[DIRSIZ] = 0;
++ if (stat(buf, &st) < 0) {
++ printf("find: cannot stat %s\n", buf);
++ continue;
++ }
++ if (strcmp(p,".") && strcmp(p,"..")) //exclude . and ..
++ {
++ if(st.type == T_FILE && !strcmp(p,filename))
++ {
++ printf("%s/%s\n",path,filename);
++ }
++ if(st.type == T_DIR)
++ {
++ find(buf,filename);
++ }
++ }
++
++
++ }
++ }
++}
++
++
++
++int main(int argc, char *argv[])
++{
++ if (argc != 3) {
++ fprintf(2, "Usage: find <path> <pattern>\n");
++ exit(1);
++ }
++ find(argv[1], argv[2]);
++ exit(0);
++
++}
+\ No newline at end of file
+diff --git a/user/init.c b/user/init.c
+index 9ca8790..108f27a 100644
+--- a/user/init.c
++++ b/user/init.c
+@@ -23,6 +23,7 @@ int main(void) {
+
+ for (;;) {
+ printf("init: starting sh\n");
++ printf("[210810201] 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..78494fa
+--- /dev/null
++++ b/user/pingpong.c
+@@ -0,0 +1,59 @@
++#include "kernel/types.h"
++#include "user.h"
++#include "kernel/stat.h"
++int main(int argc,char* argv[])
++{
++ int pid;
++
++ int pipe1[2];
++ int pipe2[2];
++
++
++ pipe(pipe1);
++ pipe(pipe2);
++ pid = fork();
++
++
++ if (pid == 0) //子进程
++ {
++
++ int childpid;
++ close (pipe2[1]);
++ close (pipe1[0]);
++ char childchar[10];
++
++ read(pipe2[0],childchar,1);
++ childpid = getpid();
++ printf("%d: received ping\n",childpid);
++ //关闭读端
++ close(pipe2[0]);
++
++ write(pipe1[1],"a",2);
++ close(pipe1[1]);
++
++ }
++
++ else if (pid>0)//父进程
++ {
++
++ int fatherpid;
++ close (pipe2[0]);
++ close (pipe1[1]);
++ char fatherchar[10];
++
++ write(pipe2[1],"a",2);
++ close(pipe2[1]);
++
++ fatherpid = getpid();
++
++ //关闭读端
++ read(pipe1[0],fatherchar,1);
++ printf("%d: received pong\n",fatherpid);
++ close (pipe1[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..e1888e3
+--- /dev/null
++++ b/user/sleep.c
+@@ -0,0 +1,16 @@
++#include "kernel/types.h"
++#include "user.h"
++//eg:在执行命令“cp file_a file_b”时,程序cp的main函数接收到的参数为:argc=3,argv=["cp", "file_a", "file_b",null]
++//不难理解执行“sleep x”时,argc = 2,argv=["sleep","x",null]
++int main(int argc,char *argv[])
++{
++ if (argc!=2){
++ printf("Sleep needs one argument!\n");
++ exit(-1);
++ }
++ int ticks = atoi(argv[1]);
++ sleep(ticks);
++ printf("(nothing happens for a little while)\n");
++ exit(0);
++
++}
+\ No newline at end of file
diff --git a/kernel/main.c b/kernel/main.c
index 8a3dc2e..a7f62dd 100644
--- a/kernel/main.c
+++ b/kernel/main.c
@@ -10,7 +10,7 @@ volatile static int started = 0;
void main() {
if (cpuid() == 0) {
// consoleinit();
- // printfinit();
+ printf("[210810201] enter main, init kernel\n");
printf("\n");
printf("xv6 kernel is booting\n");
printf("\n");
diff --git a/kernel/proc.c b/kernel/proc.c
index 1607145..bae4f89 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("[210810201] entry userinit\n");
struct proc *p;
p = allocproc();
@@ -199,7 +200,7 @@ void userinit(void) {
safestrcpy(p->name, "initcode", sizeof(p->name));
p->cwd = namei("/");
-
+ printf("[210810201] copy initcode to first user process\n");
p->state = RUNNABLE;
release(&p->lock);
diff --git a/kernel/start.c b/kernel/start.c
index f704fee..4225a28 100644
--- a/kernel/start.c
+++ b/kernel/start.c
@@ -18,6 +18,7 @@ extern void timervec();
// entry.S jumps here in machine mode on stack0.
void start() {
+
// set M Previous Privilege mode to Supervisor, for mret.
unsigned long x = r_mstatus();
x &= ~MSTATUS_MPP_MASK;
@@ -52,8 +53,9 @@ void start() {
// init uart and printf
consoleinit();
printfinit();
+ printf("[210810201] 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..9c90381
--- /dev/null
+++ b/user/find.c
@@ -0,0 +1,91 @@
+#include "kernel/types.h"
+#include "kernel/stat.h"
+#include "user/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));
+ return buf;
+}
+
+void find(char *path,char *filename)
+{
+ char buf[512],*p;
+ int fd;
+ struct dirent de;
+ struct stat st; //status
+ if ((fd = open(path,0))<0)
+ {
+ fprintf(2, "find: cannot open %s\n", path);
+ return;
+ }
+
+ if (fstat(fd,&st)<0)
+ {
+ fprintf(2, "find: cannot stat %s\n", path);
+ close(fd);
+ return;
+ }
+ switch(st.type)
+ {
+ case T_FILE:
+ break;
+
+
+ case T_DIR:
+ if (strlen(path) + 1 + DIRSIZ + 1 > sizeof (buf))
+ {
+ printf("find: path too long\n");
+ break;
+ }
+ strcpy(buf,path);
+ p = buf + strlen(buf);
+ *p++ = '/';
+ while (read(fd, &de, sizeof(de)) == sizeof(de)) {
+ if (de.inum == 0) continue;
+ memmove(p, de.name, DIRSIZ);
+ p[DIRSIZ] = 0;
+ if (stat(buf, &st) < 0) {
+ printf("find: cannot stat %s\n", buf);
+ continue;
+ }
+ if (strcmp(p,".") && strcmp(p,"..")) //exclude . and ..
+ {
+ if(st.type == T_FILE && !strcmp(p,filename))
+ {
+ printf("%s/%s\n",path,filename);
+ }
+ if(st.type == T_DIR)
+ {
+ find(buf,filename);
+ }
+ }
+
+
+ }
+ }
+}
+
+
+
+int main(int argc, char *argv[])
+{
+ if (argc != 3) {
+ fprintf(2, "Usage: find <path> <pattern>\n");
+ exit(1);
+ }
+ find(argv[1], argv[2]);
+ exit(0);
+
+}
\ No newline at end of file
diff --git a/user/init.c b/user/init.c
index 9ca8790..108f27a 100644
--- a/user/init.c
+++ b/user/init.c
@@ -23,6 +23,7 @@ int main(void) {
for (;;) {
printf("init: starting sh\n");
+ printf("[210810201] 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..11c56d9
--- /dev/null
+++ b/user/pingpong.c
@@ -0,0 +1,59 @@
+#include "kernel/types.h"
+#include "user.h"
+#include "kernel/stat.h"
+int main(int argc,char* argv[])
+{
+ int pid;
+
+ int pipe1[2];
+ int pipe2[2];
+
+
+ pipe(pipe1);
+ pipe(pipe2);
+ pid = fork();
+
+
+ if (pid == 0) //子进程
+ {
+
+ int childpid;
+ close (pipe2[1]);
+ close (pipe1[0]);
+ char childchar[10];
+
+ read(pipe2[0],childchar,20);
+ childpid = getpid();
+ printf("%d: received %s\n",childpid,childchar);
+ //关闭读端
+ close(pipe2[0]);
+
+ write(pipe1[1],"pong",100);
+ close(pipe1[1]);
+
+ }
+
+ else if (pid>0)//父进程
+ {
+
+ int fatherpid;
+ close (pipe2[0]);
+ close (pipe1[1]);
+ char fatherchar[10];
+
+ write(pipe2[1],"ping",20);
+ close(pipe2[1]);
+
+ fatherpid = getpid();
+
+
+ read(pipe1[0],fatherchar,100);
+ printf("%d: received %s\n",fatherpid,fatherchar);
+ close (pipe1[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..e1888e3
--- /dev/null
+++ b/user/sleep.c
@@ -0,0 +1,16 @@
+#include "kernel/types.h"
+#include "user.h"
+//eg:在执行命令“cp file_a file_b”时,程序cp的main函数接收到的参数为:argc=3,argv=["cp", "file_a", "file_b",null]
+//不难理解执行“sleep x”时,argc = 2,argv=["sleep","x",null]
+int main(int argc,char *argv[])
+{
+ if (argc!=2){
+ printf("Sleep needs one argument!\n");
+ exit(-1);
+ }
+ int ticks = atoi(argv[1]);
+ sleep(ticks);
+ printf("(nothing happens for a little while)\n");
+ exit(0);
+
+}
\ No newline at end of file