ref:https://pdos.csail.mit.edu/6.828/2020/xv6.html

实验:Lab: Xv6 and Unix utilities
环境搭建
实验环境搭建:https://blog.csdn.net/qq_45512097/article/details/126741793
 搭建了1天,大家自求多福吧,哎。~搞环境真是折磨人
 anyway,我搞好了:
 
开整实验
内容1:sleep
创建user/sleep.c
 在Makefile中UPROGS下添加$U/_sleep
 编写sleep.c
 运行测试程序grade-lab-util并且指明要测试的函数。如 grade-lab-util sleep
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
void main(int argc, char *argv[])
{
  if(argc != 2){
    // 2 输出到哪里,输出内容,
    fprintf(2,"usage:sleep <time> \n");
    exit(1);
  }
  int sec = atoi(argv[1]);
  sleep(sec);  
  //printf("usage:sleep %d \n",sec);
  exit(0);
}
修改makefile,只需要改这一个地方

验证命令,要退出qume才能执行,否则会报错,
 这种时候单项验证sleep
./grade-lab-util sleep
justin@DESKTOP-NIK28BI:~/vc6/xv6-labs-2020$ ./grade-lab-util sleep
fatal: detected dubious ownership in repository at '/mnt/d/code/vc6/xv6-labs-2020'
To add an exception for this directory, call:
        git config --global --add safe.directory /mnt/d/code/vc6/xv6-labs-2020
make: 'kernel/kernel' is up to date.
== Test sleep, no arguments == fatal: detected dubious ownership in repository at '/mnt/d/code/vc6/xv6-labs-2020'
To add an exception for this directory, call:
        git config --global --add safe.directory /mnt/d/code/vc6/xv6-labs-2020
sleep, no arguments: OK (1.3s)
== Test sleep, returns == sleep, returns: OK (0.8s)
== Test sleep, makes syscall == sleep, makes syscall: OK (1.0s)
内容2:pingpong
Write a program that uses UNIX system calls to ‘‘ping-pong’’ a byte between two processes over a pair of pipes, one for each direction.
 The parent should send a byte to the child;
 the child should print “: received ping”, where is its process ID, write the byte on the pipe to the parent(这里没有指定具体写什么内容,什么都可以,只是作为标志,且这个标志不需要也不要打印,否则会影响测评), and exit;
 the parent should read the byte from the child, print “: received pong”, and exit.
 Your solution should be in the file user/pingpong.c.
#include "kernel/types.h"
#include "user/user.h"
#define RD 0 // pipe的read端
#define WR 1 // pipe的write端
int main(){
    char buf[10]; //缓冲区字符数组,存放传递的信息
    int fd_c2p[1]; // child->parent
    int fd_p2c[1]; // parent->child
    int ret1 = pipe(fd_c2p);
    int ret2 = pipe(fd_p2c);
    if(ret1==-1){
        printf("child->parent pipe");
        exit(1);
    }
    if(ret2==-1){
        printf("parent->child pipe");
        exit(1);
    }
    int pid = fork();
    if(pid==0){
        //子进程
        // 关闭父管道的写入端和子管道的读取端
        close(fd_p2c[WR]); // 显示关闭是为了严谨,不关闭不会报错
        close(fd_c2p[RD]); //
        read(fd_p2c[RD],buf,4);
        printf("child:%d received %s\n", getpid(),buf);
        write(fd_c2p[WR],"pong",4);
    }else if(pid>0){
        close(fd_p2c[RD]);
        close(fd_c2p[WR]);
        // 父进程
        write(fd_p2c[WR],"ping",4);
        int status;
        int child_id = wait(&status);
        printf("child:%d done!\n",child_id);
        read(fd_c2p[RD],buf,4);
        printf("parent:%d received %s\n", getpid(),buf);
        printf("parent:%d done!\n", getpid());
    }
    exit(0);
    return 0;
}
hart 1 starting
hart 2 starting
init: starting sh
$ pingpong
child:4 received ping
child:4 done!
parent:3 received pong
parent:3 done!
注意没有exit(0); 会出现乱码,原因未知,欢迎大神留言解惑:
hart 1 starting
hart 2 starting
init: starting sh
$ pingpong
child:4 received ping
usertrap(): unexpected scause 0x000000000000000d pid=4
            sepc=0x0000000000000100 stval=0x0000000000003f64
child:4 done!
parent:3 received pong
parent:3 done!
usertrap(): unexpected scause 0x000000000000000d pid=3
            sepc=0x0000000000000100 stval=0x0000000000003f64
ref:https://xv6.dgs.zone/labs/requirements/lab1.html



















