-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path赛题一.cpp
More file actions
317 lines (295 loc) · 7.47 KB
/
赛题一.cpp
File metadata and controls
317 lines (295 loc) · 7.47 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
//======================lab1==========================
//sleep
#include "kernel/types.h"
#include "user/user.h"
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(2, "usage: sleep [ticks num]\n");
exit(1);
}
int ticks = atoi(argv[1]);
int ret = sleep(ticks);
exit(ret);
}
//pingpong
#include "kernel/types.h"
#include "user/user.h"
int main(int argc, char* argv[]) {
int parent_pid, child_pid;
int parent_to_child[2], child_to_parent[2];
char message = 'a';
pipe(parent_to_child);
pipe(child_to_parent);
child_pid = fork();
if (child_pid == 0) {
parent_pid = getpid();
close(parent_to_child[1]);
close(child_to_parent[0]);
read(parent_to_child[0], &message, 1);
printf("%d: received ping\n", parent_pid);
write(child_to_parent[1], &message, 1);
exit(0);
}
else {
parent_pid = getpid();
close(parent_to_child[0]);
close(child_to_parent[1]);
write(parent_to_child[1], &message, 1);
read(child_to_parent[0], &message, 1);
printf("%d: received pong\n", parent_pid);
exit(0);
}
}
//primes
#include "kernel/types.h"
#include "user/user.h"
void run_prime_sieve(int listenfd) {
int prime = 0;
while (1) {
if (read(listenfd, &candidate, sizeof(int)) == 0) {
close(listenfd);
if (has_child) {
close(child_pipe[1]);
wait(0);
}
exit(0);
}
if (prime == 0) {
prime = candidate;
printf("prime %d\n", prime);
continue;
}
if (candidate % prime != 0) {
if (!has_child) {
pipe(child_pipe);
has_child = 1;
if (fork() == 0) {
close(child_pipe[1]);
close(listenfd);
run_prime_sieve(child_pipe[0]);
}
else {
close(child_pipe[0]);
}
}
write(child_pipe[1], &candidate, sizeof(int));
}
}
}
int main(int argc, char* argv[]) {
int initial_pipe[2];
pipe(initial_pipe);
for (int num = 2; num <= 35; num++) {
write(initial_pipe[1], &num, sizeof(int));
}
close(initial_pipe[1]);
run_prime_sieve(initial_pipe[0]);
exit(0);
}
//find
#include "kernel/types.h"
#include "kernel/fcntl.h"
#include "kernel/fs.h"
#include "kernel/stat.h"
#include "user/user.h"
char* basename(char* pathname) {
char* prev = 0;
char* curr = strchr(pathname, '/');
while (curr != 0) {
prev = curr;
curr = strchr(curr + 1, '/');
}
return prev;
}
void find(char* curr_path, char* target) {
char buf[512], * p;
int fd;
struct dirent de;
struct stat st;
if ((fd = open(curr_path, O_RDONLY)) < 0) {
fprintf(2, "find: cannot open %s\n", curr_path);
return;
}
if (fstat(fd, &st) < 0) {
fprintf(2, "find: cannot stat %s\n", curr_path);
close(fd);
return;
}
switch (st.type) {
case T_FILE: {
char* f_name = basename(curr_path);
int match = 1;
if (f_name == 0 || strcmp(f_name + 1, target) != 0) {
match = 0;
}
if (match)
printf("%s\n", curr_path);
close(fd);
break;
}
case T_DIR: {
memset(buf, 0, sizeof(buf));
uint curr_path_len = strlen(curr_path);
memcpy(buf, curr_path, curr_path_len);
buf[curr_path_len] = '/';
p = buf + curr_path_len + 1;
while (read(fd, &de, sizeof(de)) == sizeof(de)) {
if (de.inum == 0 || strcmp(de.name, ".") == 0 ||
strcmp(de.name, "..") == 0)
continue;
memcpy(p, de.name, DIRSIZ);
p[DIRSIZ] = 0;
find(buf, target);
}
close(fd);
break;
}
}
}
int main(int argc, char* argv[]) {
if (argc != 3) {
fprintf(2, "usage: find [directory] [target filename]\n");
exit(1);
}
find(argv[1], argv[2]);
exit(0);
}
//xargs
#include "kernel/param.h"
#include "kernel/types.h"
#include "user/user.h"
#define buf_size 512
int main(int argc, char* argv[]) {
char buf[buf_size + 1] = { 0 };
uint occupy = 0;
char* xargv[MAXARG] = { 0 };
int stdin_end = 0;
for (int i = 1; i < argc; i++) {
xargv[i - 1] = argv[i];
}
while (!(stdin_end && occupy == 0)) {
if (!stdin_end) {
int remain_size = buf_size - occupy;
int read_bytes = read(0, buf + occupy, remain_size);
if (read_bytes < 0) {
fprintf(2, "xargs: read returns -1 error\n");
}
if (read_bytes == 0) {
close(0);
stdin_end = 1;
}
occupy += read_bytes;
}
char* line_end = strchr(buf, '\n');
while (line_end) {
char xbuf[buf_size + 1] = { 0 };
memcpy(xbuf, buf, line_end - buf);
xargv[argc - 1] = xbuf;
int ret = fork();
if (ret == 0) {
if (!stdin_end) {
close(0);
}
if (exec(argv[1], xargv) < 0) {
fprintf(2, "xargs: exec fails with -1\n");
exit(1);
}
}
else {
memmove(buf, line_end + 1, occupy - (line_end - buf) - 1);
occupy -= line_end - buf + 1;
memset(buf + occupy, 0, buf_size - occupy);
int pid;
wait(&pid);
line_end = strchr(buf, '\n');
}
}
}
exit(0);
}
//============================lab2=============================
//trace
#include "kernel/param.h"
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
int
main(int argc, char *argv[])
{
int i;
char *nargv[MAXARG];
if(argc < 3 || (argv[1][0] < '0' || argv[1][0] > '9')){
fprintf(2, "Usage: %s mask command\n", argv[0]);
exit(1);
}
if (trace(atoi(argv[1])) < 0) {
fprintf(2, "%s: trace failed\n", argv[0]);
exit(1);
}
for(i = 2; i < argc && i < MAXARG; i++){
nargv[i-2] = argv[i];
}
exec(nargv[0], nargv);
exit(0);
}
//sysinfo
//在 kernel/sysproc.c 中实现 sys_sysinfo() 函数
#include "sysinfo.h"
uint64
sys_sysinfo(void)
{
struct sysinfo info;
uint64 addr; // 用户空间指针
if(argaddr(0, &addr) < 0)
return -1;
info.freemem = kfreemem();
info.nproc = procnum();
if(copyout(myproc()->pagetable, addr, (char *)&info, sizeof(info)) < 0)
return -1;
return 0;
}
//获取空闲内存量,在 kernel/kalloc.c 中添加:
uint64
kfreemem(void)
{
struct run *r;
uint64 n = 0;
acquire(&kmem.lock);
r = kmem.freelist;
while(r){
n += PGSIZE;
r = r->next;
}
release(&kmem.lock);
return n;
}
//获取进程数量,在 kernel/proc.c 中添加:
uint64
procnum(void)
{
struct proc *p;
uint64 n = 0;
for(p = proc; p < &proc[NPROC]; p++){
acquire(&p->lock);
if(p->state != UNUSED){
n++;
}
release(&p->lock);
}
return n;
}
#include "kernel/types.h"
#include "kernel/sysinfo.h"
#include "user/user.h"
int
main(int argc, char *argv[])
{
struct sysinfo info;
if(sysinfo(&info) < 0){
printf("sysinfo failed\n");
exit(1);
}
printf("free memory: %d bytes\n", info.freemem);
printf("process count: %d\n", info.nproc);
exit(0);
}