LAB1
xv6我运行虚拟环境的是ubuntu64位20.04.4,最好不要使用更新的版本,很有可能make qemu之后无法运行的情况。
sleep
#include"kernel/types.h"
#include"kernel/stat.h"
#include"user/user.h"
int
main(int argc, char const *argv[])
{
if (argc<2){
printf("You should print the sleeping time\n");
exit(1);
}
int time=atoi(argv[1]);
printf("(nothing happens for a little while )\n");
sleep(time);
exit(0);
}
运行没有问题,但是我似乎需要学着将错误信息这样输出。
fprintf(2,"usage : sleep <time>\n");
pingpong
这是我写的最初版本,特别需要注意那就是父子进程中不使用的pipe的文件描述符尽快close掉,特别是写端。不然数据为空的管道read会堵塞。
#include"kernel/types.h"
#include"kernel/stat.h"
#include"user/user.h"
int main(){
int p1[2];
int p2[2];
pipe(p1);
pipe(p2);
int pid;
char buf1[10];
char buf2[10];
if (fork()>0){
//主进程
close(p2[1]);
close(p1[0]);
write(p1[1],"p",1);
close(p1[1]);
if (read(p2[0],buf1,1)==1){
close(p2[0]);
pid=getpid();
printf("%d: received pong\n",pid);
}
}else{
close(p1[1]);
close(p2[0]);
if (read(p1[0],buf2,1)