comparison slides/2018/20180403/slide.md @ 23:c0ec001d8a28

update
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Thu, 05 Apr 2018 11:34:39 +0900
parents slides/20180403/slide.md@7143a82401fa
children
comparison
equal deleted inserted replaced
22:7143a82401fa 23:c0ec001d8a28
1 title: 近況報告
2 author: Takahiro Shimizu
3 profile:
4 lang: Japanese
5
6 # 今週の進捗
7
8 - xv6をcloneして読み始めました
9 - [Xv6, a simple Unix-like teaching operating system](https://pdos.csail.mit.edu/6.828/2017/xv6.html)
10 - 教科書もありました
11 - [xv6 a simple, Unix-like teaching operating system](https://pdos.csail.mit.edu/6.828/2017/xv6/book-rev10.pdf)
12 - 木金で読み会をやる予定です
13
14 # systemcall
15
16 - `user.h` `usys.S`でそれぞれ定義されていました
17
18 ```c
19 // system calls
20 int fork(void);
21 int exit(void) __attribute__((noreturn));
22 int wait(void);
23 int pipe(int*);
24 int write(int, void*, int);
25 int read(int, void*, int);
26 int close(int);
27 int kill(int);
28 int exec(char*, char**);
29 int open(char*, int);
30 int mknod(char*, short, short);
31 int unlink(char*);
32 int fstat(int fd, struct stat*);
33 int link(char*, char*);
34 int mkdir(char*);
35 int chdir(char*);
36 int dup(int);
37 int getpid(void);
38 char* sbrk(int);
39 int sleep(int);
40 int uptime(void);
41 ```
42
43 # printf
44
45 - printfは `%d, %x, %p, %s.` のみをサポートしています
46 - コンパイラ構成論で書いたような素朴な実装でした
47 - print時の進数は `printint` の引数で分けているようです
48
49 ```c
50 void
51 printf(int fd, char *fmt, ...)
52 {
53 char *s;
54 int c, i, state;
55 uint *ap;
56
57 state = 0;
58 ap = (uint*)(void*)&fmt + 1;
59 for(i = 0; fmt[i]; i++){
60 c = fmt[i] & 0xff;
61 if(state == 0){
62 if(c == '%'){
63 state = '%';
64 } else {
65 putc(fd, c);
66 }
67 } else if(state == '%'){
68 if(c == 'd'){
69 printint(fd, *ap, 10, 1);
70 ap++;
71 } else if(c == 'x' || c == 'p'){
72 printint(fd, *ap, 16, 0);
73 ap++;
74 } else if(c == 's'){
75 s = (char*)*ap;
76 ap++;
77 if(s == 0)
78 s = "(null)";
79 while(*s != 0){
80 putc(fd, *s);
81 s++;
82 }
83 } else if(c == 'c'){
84 putc(fd, *ap);
85 ap++;
86 } else if(c == '%'){
87 putc(fd, c);
88 } else {
89 // Unknown % sequence. Print it to draw attention.
90 putc(fd, '%');
91 putc(fd, c);
92 }
93 state = 0;
94 }
95 }
96 }
97 ```
98
99 # sysemcallを呼ぶ前
100
101 - まだ処理が追いきれていません。
102
103 ```
104 static void
105 printint(int fd, int xx, int base, int sgn)
106 {
107 static char digits[] = "0123456789ABCDEF";
108 char buf[16];
109 int i, neg;
110 uint x;
111
112 neg = 0;
113 if(sgn && xx < 0){
114 neg = 1;
115 x = -xx;
116 } else {
117 x = xx;
118 }
119
120 i = 0;
121 do{
122 buf[i++] = digits[x % base];
123 }while((x /= base) != 0);
124 if(neg)
125 buf[i++] = '-';
126
127 while(--i >= 0)
128 putc(fd, buf[i]);
129 }
130 ```
131
132 # putc
133
134 - 素朴にwriteを読んでいる
135
136 ```
137 static void
138 putc(int fd, char c)
139 {
140 write(fd, &c, 1);
141 }
142
143 ```
144
145 # sh
146
147 - main関数
148 - まずforkが入る
149 - cd以外はparseしてrunする世界観のようです
150
151 ```C
152 int
153 main(void)
154 {
155 static char buf[100];
156 int fd;
157
158 // Ensure that three file descriptors are open.
159 while((fd = open("console", O_RDWR)) >= 0){
160 if(fd >= 3){
161 close(fd);
162 break;
163 }
164 }
165
166 // Read and run input commands.
167 while(getcmd(buf, sizeof(buf)) >= 0){
168 if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' '){
169 // Chdir must be called by the parent, not the child.
170 buf[strlen(buf)-1] = 0; // chop \n
171 if(chdir(buf+3) < 0)
172 printf(2, "cannot cd %s\n", buf+3);
173 continue;
174 }
175 if(fork1() == 0)
176 runcmd(parsecmd(buf));
177 wait();
178 }
179 exit();
180 }
181 ```