mit 6.1810--1.Xv6 and Unix utilities

Xv6 and Unix utilities

完成这一章花了远多于建议的时间,很大程度上是因为我对xv6内核和c语法并不熟悉,比如传实参和字符串函数。

sleep

考虑一下异常情况就行。

pingpong

关于管道:

  • read/write 的状态号对应 0/1
  • 读写结束后要及时close

find

文件有不同类型,参考 user/ls.c 的框架。

prime

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
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

/*
给当前数写入管道
管道中第一个数必为质数,记录并输出
用第一个数筛管道里剩下的所有数,把筛出来的加入下一个管道。
*/
void sieve(int last_read_pipe)
{
int p, nums;
if (read(last_read_pipe, &p, sizeof p) != sizeof p){
close(last_read_pipe);
return;
}
printf("prime %d\n", p);

int fd[2];
if (pipe(fd) < 0)
{
fprintf(2, "pipe failed\n");
exit(1);
}

int pid = fork();
if(pid < 0){
fprintf(2, "fork failed\n");
exit(1);
}

if (pid)
{
close(fd[0]);
while (read(last_read_pipe, &nums, sizeof nums) == sizeof nums)
{
if (nums % p)
{
write(fd[1], &nums, sizeof nums);
}
}
close(fd[1]), close(last_read_pipe);
wait(0);
exit(0);
}
else
{
close(fd[1]), close(last_read_pipe);
sieve(fd[0]);
close(fd[0]);
exit(0);
}
}

int main(int argc, char *argv[])
{
int fd[2];
if (pipe(fd) < 0)
{
fprintf(2, "pipe failed\n");
exit(1);
}
const int n = 35;
for (int i = 2; i <= n; ++i){
write(fd[1], &i, sizeof i);
}
close(fd[1]);
sieve(fd[0]);
exit(0);
}

xargs

malloc 动态申请内存,用 strlen\strcpy 处理字符串

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
#include "kernel/types.h"
#include "kernel/stat.h"
#include "kernel/param.h"
#include "user/user.h"

// self real_pro pra1 pra2 ...
int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(2, "xargs failed\n");
exit(1);
}

// get stdin and attach it to the end
const int MAX_ARG_LEN = MAXPATH;
int argc_exev = argc - 1, index = -1;
char c;
char bufline[MAX_ARG_LEN];
char *argv_exev[MAXARG];

// argv[1 -> argc-1] => argv_exev
for (int i = 1; i < argc; ++i)
{
argv_exev[i - 1] = (char *)malloc((strlen(argv[i]) + 1) * sizeof(char));
strcpy(argv_exev[i - 1], argv[i]);
}

// read and push
while (read(0, &c, sizeof(c)) == sizeof(c))
{
bufline[++index] = c;
if (c == '\n' || c == '\0' || c == ' ')
{
if(index == 0){
index = -1;
continue;
}
++argc_exev;
bufline[index] = '\0';
argv_exev[argc_exev - 1] = (char *)malloc((strlen(bufline) + 1)* sizeof(char));
strcpy(argv_exev[argc_exev - 1], bufline);
index = -1;
}
}
if(index != -1){
++argc_exev;
bufline[++index] = '\0';
argv_exev[argc_exev - 1] = (char *)malloc((strlen(bufline) + 1) * sizeof(char));
strcpy(argv_exev[argc_exev - 1], bufline);
index = -1;
}
argv_exev[argc_exev] = 0;

// exev
int pid = fork();
if(pid < 0){
fprint(2, "fork failed\n");
exit(1);
}
if (pid){
wait(0);
}else {
exec(argv_exev[0], argv_exev);
}
exit(0);
}