xv6 实验1 Unix Utilities
Lab util: Uinx utilities
- https://pdos.csail.mit.edu/6.S081/2020/schedule.html
- https://pdos.csail.mit.edu/6.828/2020/labs/util.html
sleep
在 UPROGS 项中最后一行添加 $U/_sleep\1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18UPROGS=\
$U/_cat\
$U/_echo\
$U/_forktest\
$U/_grep\
$U/_init\
$U/_kill\
$U/_ln\
$U/_ls\
$U/_mkdir\
$U/_rm\
$U/_sh\
$U/_stressfs\
$U/_usertests\
$U/_grind\
$U/_wc\
$U/_zombie\
$U/_sleep\
sleep.c1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int
main(int argc, char *argv[])
{
if(argc == 2) {
fprintf(2, "(nothing happens for a little while)\n");
sleep(atoi((const char*) argv[1])); // convert ascii to integer and use system call sleep
exit(0);
}
else{
fprintf(2, "Usage:sleep number...\n"); // put the error masssage to the file descriptor 2
exit(1); // terminate the process,
// status = 1: indicates failure, and reported to wait()
}
}
pingpong
分析
通过fork、pipe和getpid系统调用,来实现pingpong
- fork在父进程中的返回值是pid,在子进程中的返回值是0
- pipe初始化的管道,其数组的第一个元素是读端,第二个元素是写端
实现
在 UPROGS 项中最后一行添加 $U/_pingpong\
pingpong.c1
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
int
main(int argc, char *argv[])
{
int p1[2]; // parent -> child
int p2[2]; // child -> parent
char buffer[] = {'x'};
int length = sizeof(buffer);
if(argc != 1){
fprintf(2, "Usage: pingpong\n");
exit(1);
}
pipe(p1);
pipe(p2);
if(fork() == 0){ // child
close(p1[1]);
close(p2[0]);
if(read(p1[0], buffer, length) == 1){ // child receives the byte
printf("%d: received ping\n", getpid());
write(p2[1], buffer, length); // child sends the byte to child
close(p2[1]);
exit(0);
}
close(p2[1]);
exit(1);
}else{ // parent
close(p1[0]);
close(p2[1]);
write(p1[1], buffer, length); // parent sends a byte to the child
close(p1[1]);
wait((int *)0);
if(read(p2[0], buffer, length) == 1){ // parent receives the byte
printf("%d: received pong\n", getpid());
}
exit(0);
}
}
primes
分析

实现
在 UPROGS 项中最后一行添加 $U/_primes\
1 |
|
find
在 UPROGS 项中最后一行添加 $U/_find\
1 |
|
xargs
在 UPROGS 项中最后一行添加 $U/_xargs\
1 |
|
git查看修改记录
查看文件的变化1
2
3
4
5
6
7
8
9grand@Lubuntu:~/xv6-labs-2020$ git diff origin/util util --stat
Makefile | 5 +++++
gradelib.pyc | Bin 0 -> 22972 bytes
user/find.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
user/pingpong.c | 40 ++++++++++++++++++++++++++++++++++++++++
user/primes.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
user/sleep.c | 17 +++++++++++++++++
user/xargs.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 260 insertions(+)
查看具体修改内容git diff origin/util util1
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
305diff --git a/Makefile b/Makefile
index 0fbaf7c..b2addd4 100644
--- a/Makefile
+++ b/Makefile
@@ -149,6 +149,11 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
+ $U/_sleep\
+ $U/_pingpong\
+ $U/_find\
+ $U/_xargs\
+ $U/_primes\
ifeq ($(LAB),syscall)
diff --git a/gradelib.pyc b/gradelib.pyc
new file mode 100644
index 0000000..634a0f3
Binary files /dev/null and b/gradelib.pyc differ
diff --git a/user/find.c b/user/find.c
new file mode 100644
index 0000000..8070f86
--- /dev/null
+++ b/user/find.c
@@ -0,0 +1,84 @@
+#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)+1);
+ return buf;
+}
+
+void
+find(char *path, char *file)
+{
+ 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;
+ }
+
+ if(fstat(fd, &st) < 0){ // 获取不了该路径的状态
+ fprintf(2, "find: cannot stat %s\n", path);
+ close(fd);
+ return;
+ }
+
+ switch(st.type){
+ case T_FILE: // 如果这是一个文件
+ if(strcmp(fmtname(path), file) == 0){
+ printf("%s\n", path);
+ }
+ 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 || de.inum == 1)
+ continue;
+ if (strcmp(de.name, ".") == 0 || strcmp(de.name, "..") == 0)
+ continue;
+ memmove(p, de.name, DIRSIZ); // 在路径后面添加文件名or目录
+ p[DIRSIZ] = 0; // the end of string is '\0'
+ find(buf, file);
+ }
+ break;
+
+ default:
+ break;
+ }
+ close(fd);
+}
+
+
+int
+main(int argc, char *argv[])
+{
+ if(argc != 3){
+ fprintf(2, "Usage: find <path> <file>\n");
+ exit(1);
+ }
+ find(argv[1], argv[2]);
+ exit(0);
+}
diff --git a/user/pingpong.c b/user/pingpong.c
new file mode 100644
index 0000000..caecc4b
--- /dev/null
+++ b/user/pingpong.c
@@ -0,0 +1,40 @@
+#include "kernel/types.h"
+#include "kernel/stat.h"
+#include "user/user.h"
+
+int
+main(int argc, char *argv[])
+{
+ int p1[2]; // parent -> child
+ int p2[2]; // child -> parent
+ char buffer[] = {'x'};
+ int length = sizeof(buffer);
+ if(argc != 1){
+ fprintf(2, "Usage: pingpong\n");
+ exit(1);
+ }
+ pipe(p1);
+ pipe(p2);
+ if(fork() == 0){ // child
+ close(p1[1]);
+ close(p2[0]);
+ if(read(p1[0], buffer, length) == 1){ // child receives the byte
+ printf("%d: received ping\n", getpid());
+ write(p2[1], buffer, length); // child sends the byte to child
+ close(p2[1]);
+ exit(0);
+ }
+ close(p2[1]);
+ exit(1);
+ }else{ // parent
+ close(p1[0]);
+ close(p2[1]);
+ write(p1[1], buffer, length); // parent sends a byte to the child
+ close(p1[1]);
+ wait((int *)0);
+ if(read(p2[0], buffer, length) == 1){ // parent receives the byte
+ printf("%d: received pong\n", getpid());
+ }
+ exit(0);
+ }
+}
diff --git a/user/primes.c b/user/primes.c
new file mode 100644
index 0000000..6f9180e
--- /dev/null
+++ b/user/primes.c
@@ -0,0 +1,67 @@
+#include "kernel/types.h"
+#include "user/user.h"
+
+void
+close_pipe(int *p) {
+ close(p[0]);
+ close(p[1]);
+}
+
+void
+primes() {
+ int n, p, len;
+ int fd[2];
+
+ // read the frist number from previous prime
+ if ((len = read(0, &n, sizeof(int))) <= 0 || n <= 0) {
+ close(1);
+ exit(0);
+ }
+
+ // print first number to console
+ printf("prime %d\n", n);
+
+ pipe(fd);
+ if (fork() == 0) { // child process
+ close(0);
+ dup(fd[0]); // link the std i/o 0 to fd[0]
+ close_pipe(fd);
+ primes();
+ } else { // parent process
+ close(1);
+ dup(fd[1]); // link the std i/o 1 to fd[1]
+ close_pipe(fd);
+ while ((len = read(0, &p, sizeof(int))) > 0 && p > 0) { // read the number from the prime
+ if (p % n != 0) {
+ write(1, &p, sizeof(int)); // filter the number
+ }
+ }
+ close(1);
+ wait((int *)0);
+ }
+ exit(0);
+}
+
+int
+main(void) {
+ int i;
+ int fd[2];
+
+ pipe(fd);
+ if (fork() == 0) { // child process
+ close(0);
+ dup(fd[0]); // link the std i/o 0 to the fd[0]
+ close_pipe(fd);
+ primes();
+ } else {
+ close(1);
+ dup(fd[1]); // link the std i/o 1 to the fd[1]
+ close_pipe(fd);
+ for (i = 2; i <= 35; i++) {
+ write(1, &i, sizeof(int));
+ }
+ close(1);
+ wait((int *)0);
+ }
+ exit(0);
+}
diff --git a/user/sleep.c b/user/sleep.c
new file mode 100644
index 0000000..1c4d51d
--- /dev/null
+++ b/user/sleep.c
@@ -0,0 +1,17 @@
+#include "kernel/types.h"
+#include "kernel/stat.h"
+#include "user/user.h"
+
+int
+main(int argc, char *argv[])
+{
+ if(argc == 2){
+ sleep(atoi((const char*) argv[1])); // convert ascii to integer and use system call sleep
+ exit(0);
+ }
+ else{
+ fprintf(2, "Usage:sleep number...\n"); // put the error masssage to the file descriptor 2
+ exit(1); // terminate the process,
+ //status = 1: indicates failure, and reported to wait()
+ }
+}
diff --git a/user/xargs.c b/user/xargs.c
new file mode 100644
index 0000000..1423af7
--- /dev/null
+++ b/user/xargs.c
@@ -0,0 +1,47 @@
+#include "kernel/types.h"
+#include "user/user.h"
+#include "kernel/param.h"
+
+int
+main(int argc, char *argv[])
+{
+ char *arguments[MAXARG];
+ char buff[255];
+ char *p = buff, *q = buff;
+ int i, offset;
+ if(argc <= 1){
+ fprintf(2, "Usage: xargc <command>...\n");
+ exit(1);
+ }
+
+ for(i = 1; i < argc; i++){ // get the command which is behind the "xargs"
+ arguments[i-1] = argv[i];
+ }
+ i = i - 1;
+ offset = i; // backup
+
+ while(read(0, p, 1)){
+ if(*p == '\n'){ // meet the '\n' and execute it with arguments
+ *p = '\0';
+ arguments[i] = q;
+ if(fork() == 0){
+ exec(arguments[0], arguments);
+ exit(0);
+ }else{
+ wait((int*)0);
+ p = buff;
+ q = p;
+ i = offset;
+ }
+ }else if(*p == ' ' || *p == '\t'){ // meet the ' ' or '\t', find an argument
+ *p = '\0';
+ arguments[i] = q;
+ i = i + 1;
+ p = p + 1;
+ q = p;
+ }else{
+ p = p + 1;
+ }
+ }
+ exit(0);
+}

