MIT 6.S081 Lab1 Xv6 and Unix utilities

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值