Lab01:Xv6 and Unix utilities

实验测试方法

实验的测试方法主要有2个:

  1. 进入到Xv6系统中,执行相应的命令
  2. 使用实验提供的评分测试
    • 对于单个实验,可以使用 make GRADEFLAGS=application grade其中application为要测试的实验应用,例如sleep实验对应的评分测试命令为 make GRADEFLAGS=sleep grade;
    • 对于整个实验,可以直接使用 make grade 进行评测

对于Lab1的评分测试,感觉不太稳定,多次测试中会随机出现测试失败的情况,但是根据失败的测试样例进入到Xv6中模拟测试又没发现什么错误。换了机器测试又没这种情况了,或许与测试环境有关?或者是Xv6在返回结果的过程中引入了其他未知问题,暂时找不到原因,不纠结这个了,还是干正事要紧。
已做的练习提交到github中,可以自行拉取并切换到相应的分支查看。https://github.com/kk140906/mit6s081_labs.git 。

sleep(easy)

Implement the UNIX program sleep for xv6; your sleep should pause for a user-specified number of ticks. A tick is a notion of time defined by the xv6 kernel, namely the time between two interrupts from the timer chip. Your solution should be in the file user/sleep.c.

user/sleep.c

#include "kernel/types.h"
#include "user/user.h"int main(int argc ,char **argv) {if (argc != 2) {const char *info = "sleep take one argument.\n";write(2,info,strlen(info));exit(1);}int ticks = atoi(argv[1]);sleep(ticks);exit(0);
}

更改 Makefile

Makefile位于xv6-labs-2020实验的根目录下,打开后定位到 “UPROGS=\” 在最后添加 “$U/_sleep\”。

pingpong(easy)

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

user/pingpong.c

#include "kernel/types.h"
#include "user/user.h"int main(int argc, char** argv)
{int p[2];char buf[512] = { 0 };pipe(p);if (fork() == 0 && read(p[0], buf, sizeof(buf) - 1) == 1) {printf("%d: received ping\n", getpid());// printf("%s\n", buf);write(p[1], "c", 1);close(p[0]);close(p[1]);exit(0);}write(p[1], "p", 1);wait(0);if (read(p[0], buf, sizeof(buf) - 1) == 1)printf("%d: recieved pong\n", getpid());// printf("%s\n", buf);close(p[0]);close(p[1]);exit(0);
}

更改 Makefile

Makefile位于xv6-labs-2020实验的根目录下,打开后定位到 “UPROGS=\” 在最后添加 “$U/_pingpong\”。

primes(moderate/hard)

Write a concurrent version of prime sieve using pipes. This idea is due to Doug McIlroy, inventor of Unix pipes. The picture halfway down this page and the surrounding text explain how to do it. Your solution should be in the file user/primes.c.

user/primes.c

#include "kernel/types.h"
#include "user/user.h"#define MAX_PRIMES 35void pipeline(int fd) {int prime;// 进入管线中时先读一次,把这一次的数值作为当前管线的处理的基础数值if (read(fd, &prime, sizeof(int)) <= 0) {close(fd);exit(1); }printf("prime %d\n", prime);int p[2] = {-1};pipe(p);if (fork() == 0) {close(p[1]);pipeline(p[0]);exit(0);}close(p[0]);int val;while (read(fd, &val, sizeof(int))) {if (val % prime == 0)continue;write(p[1], &val, sizeof(int));}close(fd);close(p[1]);wait(0);exit(0);
}int main(int argc, char **argv) {int p[2] = {-1};pipe(p);if (fork() == 0) {// xv6 资源不多,能提前关闭的文件描述符都需要提前关闭close(p[1]);// p[0] 在管线中关闭pipeline(p[0]);exit(0);}close(p[0]);for (int i = 2; i <= MAX_PRIMES; ++i) {write(p[1], &i, sizeof(int));}close(p[1]);wait(0);exit(0);
}

更改 Makefile

Makefile位于xv6-labs-2020实验的根目录下,打开后定位到 “UPROGS=\” 在最后添加 “$U/_primes\”。

find (moderate)

Write a simple version of the UNIX find program: find all the files in a directory tree with a specific name. Your solution should be in the file user/find.c.

user/find.c

#include "kernel/types.h"
#include "kernel/fcntl.h"
#include "kernel/fs.h"
#include "kernel/stat.h"
#include "user/user.h"#define MAX_PATH_LEN 256
typedef enum { false, true } bool;
bool match(const char *dirs, const char *file) {const char *p = dirs + strlen(dirs);char formated_dirs[MAX_PATH_LEN];while (*p != '/')p--;strcpy(formated_dirs, ++p);return !strcmp(formated_dirs, file);
}void find(char *dir, const char *file) {int fd;if ((fd = open(dir, O_RDONLY)) < 0) {fprintf(2, "find: cannot open %s\n", dir);return;}struct stat st;if (fstat(fd, &st) < 0) {fprintf(2, "find: cannot stat %s\n", dir);close(fd);return;}char dirs[MAX_PATH_LEN] = {0};struct dirent de;char *p;switch (st.type) {case T_DIR:strcpy(dirs, dir);p = dirs + strlen(dirs);*p++ = '/';while (read(fd, &de, sizeof(de)) == sizeof(de)) {// 不再继续处理 "." 和 ".." 目录if (de.inum == 0 || !strcmp(de.name,".") || !strcmp(de.name,".."))continue;memmove(p, de.name, DIRSIZ);p[DIRSIZ] = 0;if (stat(dirs, &st) < 0) {fprintf(2, "find: cannot stat %s\n", dir);close(fd);}if (st.type == T_FILE && match(dirs, file)) {printf("%s\n", dirs);} else if (st.type == T_DIR && dirs[strlen(dirs) - 1] != '.' ) {find(dirs, file);}}break;default:break;}
}int main(int argc, char **argv) {if (argc != 3) {fprintf(2, "usage: find [dir] [file].");exit(1);}find(argv[1], argv[2]);exit(0);
}

更改 Makefile

Makefile位于xv6-labs-2020实验的根目录下,打开后定位到 “UPROGS=\” 在最后添加 “$U/_find\”。

xargs (moderate)

Write a simple version of the UNIX xargs program: read lines from the standard input and run a command for each line, supplying the line as arguments to the command. Your solution should be in the file user/xargs.c.

user/xargs.c

#include "kernel/param.h"
#include "kernel/types.h"
#include "user/user.h"int getline(char *buf) {char c;char *s = buf;while (read(0, &c, 1) == 1 && c != '\n') {*buf++ = c;}return strlen(s);
}int main(int argc, char **argv) {if (argc < 2) {fprintf(2, "xargs take one argument at least.");exit(1);}char *args[MAXARG];for (int i = 0; i < argc - 1; ++i) {args[i] = argv[i + 1];}char buf[MAXPATH] = {0};while (getline(buf)) {args[argc - 1] = buf;args[argc] = 0;if (fork() == 0) {exec(argv[1], args);exit(0);}wait(0);memset(buf, 0, MAXPATH);}exit(0);
}

更改 Makefile

Makefile位于xv6-labs-2020实验的根目录下,打开后定位到 “UPROGS=\” 在最后添加 “$U/_xargs\”。

原文地址:https://blog.nas-kk.top/?p=385

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/255994.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

jQuery学习- 位置选择器

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title>位置选择器</title><script src"js/jquery.js"></script><script type"text/javascript">$(function(){//获取第一个li$(&quo…

数据类型之元组

存多个值&#xff0c;对比列表来说&#xff0c;元组不可变&#xff08;是可以当做字典的key的&#xff09;&#xff0c;主要是用来读 与列表类型比&#xff0c;只不过[]换成()age(11,22,33,44,55) #本质agetuple((11,22,33,44,55)) print(type(age)) age[0]12 t(1,2,[a,b]) pri…

cocos2d-x3.6 连连看连通画线

我的博客&#xff1a;http://blog.csdn.net/dawn_moon 网上看到非常多人写的连连看&#xff0c;都没有画连线的实现。事实上要话连线挺简单的。cocos2d-x 提供了一个非常方便的绘图形的类。DrawNode。这个类封装了非常多画线条&#xff0c;多边形的方法。非常方便&#xff0c;非…

阿里云大数据计算服务MaxCompute(上篇)

关于阿里云大数据计算服务MaxCompute的详细内容&#xff1a; 阿里云大数据计算服务MaxCompute使用教程 &#xff08;MaxCompute&#xff08;原ODPS&#xff09;是一项大数据计算服务&#xff0c;它能提供快速、完全托管的PB级数据仓库解决方案&#xff0c;使您可以经济并高效的…

Vue3、TypeScript 实现图片数量及大小随宽度自适应调整

前言 过了这么久&#xff0c;想起自己还有个博客&#xff0c;更点内容吧&#xff01; 来&#xff0c;上需求&#xff01; 最近在做个前端界面&#xff0c;要求在一行中展示一些图片&#xff0c;展示的图片数量随着窗口宽度大小进行变化&#xff0c;除此之外还有以下要求&…

【tensorFlow】——图像数据增强、读取图像、保存图像

#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/4/13 10:54 # @Author : @linlianqin # @Site : # @File : 数据增强(distorted).py # @Software: PyCharm # @description:一些基于TensorFlow的数据处理方法import tensorflow as tf import cv2 im…

数据分析方法有哪些_数据分析方法

数据分析方法有哪些_数据分析方法 随着大数据的到来&#xff0c;数据分析师成为大数据时代一颗冉冉升起的新星&#xff0c;现在企业越来越重视大数据&#xff0c;数据分析师这个职业也成为企业争抢的对象。那么数据分析师的分析数据的方法都有哪些呢&#xff1f; 1、数据分析遵…

苹果Iphone/Ipad--L2T虚拟教程

1 Iphone和Ipad同为IOS&#xff0c;设置方法相同。首先进入IOS系统的“设置”程序。 2 点击“通用”进入通用设置&#xff0c;点击“”; 3 选择"添加设置 "&#xff1b; 4 选择L2TP方式&#xff0c;填写必要信息&#xff1a;描述、服务器地址 、您注册充值的账号及密…

记忆化搜索的应用

记忆化搜索的应用 一般来说&#xff0c;动态规划总要遍历所有的状态&#xff0c;而搜索可以排除一些无效状态。更重要的是搜索还可以剪枝&#xff0c;可能剪去大量不必要的状态&#xff0c;因此在空间开销上往往比动态规划要低很多。 如何协调好动态规划的高效率与高消费之间的…

【深度学习】——DNN后向传播、CNN后向传播文章汇总

深度神经网络&#xff08;DNN&#xff09;模型与前向传播算法 深度神经网络&#xff08;DNN&#xff09;反向传播算法(BP) 卷积神经网络CNN的前向和后向传播&#xff08;一&#xff09; 卷积神经网络CNN的前向和后向传播&#xff08;二&#xff09; 有batch normalization的卷积…

ajaxReturn 之前dump调试,导致$.ajax不能正常运行

ajaxReturn 之前dump调试&#xff0c;导致$.ajax不能正常运行 以后调试的时候&#xff0c;注意下这个情况转载于:https://www.cnblogs.com/bushe/p/5180317.html

Veebot-自动静脉抽血机器人

Veebot-自动静脉抽血机器人 我们可能都有过被抽血的经验。护士让你握紧拳头&#xff0c;用一根橡皮条压住你上臂的血管&#xff0c;在你的肘部内侧寻找你的静脉&#xff0c;有时还需要拍打几下&#xff0c;摸到隆起的静脉血管&#xff0c;一针下去。有时候碰到技术好的护士&…

idea 转普通项目为maven 项目

1、项目上右键 Add Framework Support。 2、选择maven&#xff0c;点击OK。 转载于:https://www.cnblogs.com/mayanze/p/8042489.html

HDOJ5547 SudoKu

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid5547 题目大意&#xff1a;填数独。。。 思路&#xff1a;爆搜 1 #include <stdio.h>2 #include <string.h>3 #include <iostream>4 #include <algorithm>5 using namespace std;6 boo…

【深度学习之ResNet】——深度残差网络—ResNet总结

目录 论文名称&#xff1a;Deep Residual Learning for Image Recognition 摘要&#xff1a; 1、引言 2、为什么会提出ResNet残差网络呢&#xff1f; 3、深度残差网络结构学习&#xff08;Deep Residual learning&#xff09; &#xff08;1&#xff09;残差单元 &#xf…

Atitit.  c# 语法新特性 c#2.0 3.0 4.0 4.5 5.0 6.0   attilax总结

Atitit. c# 语法新特性 c#2.0 3.0 4.0 4.5 5.0 6.0 attilax总结 1.1. C# 1.0-纯粹的面向对象 1.2. C# 2.0-泛型编程新概念 1.3. C# 2.0的另一个突出的特性就是匿名方法 1.4. C#3.0 linq 1.5. C# 4.0动态编程 dynamic 1.6. C# 4.5 异步编程 async和await 1.7. C# 5.0 更方便…

关于SafeMove White Paper功能

ABB机器人网站有一个 Safemove 功能的介绍&#xff0c;在Overview页面右半版有一篇文档是 SafeMove White Paper &#xff0c;在45页的 pdf 文档中&#xff0c;详细了介绍工业机器人的安全原则&#xff0c;以及ABB工业机器人自身 EPS (Electronic Position Switches) 和 SafeMo…

面试疑难点解析

List,Set,Map,有什么区别&#xff1f; List和Set实际上市实现了Collection接口&#xff0c;那么Collection接口的原理你能简单描述一下吗&#xff1f; List接口可以插入多个NULL值&#xff0c;并且重复值&#xff0c;而且LIST是一个有序的集合。 Set是一个不可重复的集合&#…

【深度学习】——日常知识点总结(持续更新)

设计卷积网络的原则&#xff1a; 1、最后转为一维有两种方式&#xff1a;1&#xff09;全局平均池化&#xff1b;2&#xff09;扁平化直接转化为一维的 2、在卷积层的大小变化时尽量保证特征图大小减小n倍时&#xff0c;特征图的个数也增加n倍&#xff0c;维持网络的复杂度&a…

主机无法访问虚拟机的httpd服务

症状&#xff1a;虚拟机装的centos6.3 通过桥接的方式与主机连接 虚拟机通过yum安装httpd服务 在主机浏览器中输入 虚拟机ip 无法访问虚拟机Apache 虚拟机和主机可以相互ping通 解决&#xff1a;关掉虚拟机的防火墙就可以了 命令setup进入防火墙管理 按空格键取消防火墙启用 转…