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.pswp.cn/news/255994.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/255994.shtml
英文地址,請注明出處:http://en.pswp.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進入防火墻管理 按空格鍵取消防火墻啟用 轉…