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 35

void 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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: :xv6是一个基于Unix的操作系统,它是一个教学用途的操作系统,旨在教授操作系统的基本概念和实现。它是在MIT的x86架构上开发的,包括了Unix的一些基本功能,如进程管理、文件系统、内存管理等。xv6的源代码是公开的,可以用于学习和研究。 Unix utilitiesUnix操作系统中的一些基本工具,如ls、cd、cp、mv、rm等。这些工具可以帮助用户管理文件和目录,执行各种操作。这些工具的实现是基于Unix的系统调用,可以通过编写C程序来调用这些系统调用实现相应的功能。这些工具是Unix操作系统的基础,也是其他操作系统的参考。 ### 回答2: lab: xv6 and unix utilities 实验是一项旨在帮助学生深入理解操作系统和 Unix 工具使用的实验。该实验分为两个部分,第一部分教授学生如何构建和运行 xv6 操作系统;第二部分则重点教授 Unix 工具的使用。 在 xv6 操作系统部分,学生将学习到操作系统内核的基本结构和实现原理。实验将引导学生理解内存管理、进程调度、系统调用等关键操作系统概念。此外,学生还将学习如何编写简单的 shell 以及如何通过修改 xv6 内核代码来实现新的系统调用和功能。 在 Unix 工具部分,学生将探索 Unix 系统中广泛使用的常见工具。这些工具包括 vi 编辑器、grep、awk、sed 等。实验将介绍这些工具的基本使用方法以及它们在处理文本和数据时的实际应用。这部分实验还将让学生深入了解 shell 和 shell 脚本的编写,帮助他们在 Unix 环境中轻松地编写脚本和自动化任务。 lab: xv6 and unix utilities 实验对计算机科学专业的学生具有重要意义。通过完成这个实验,学生将建立起对操作系统和 Unix 工具的深入理解,为他们成为一名优秀的软件工程师奠定坚实的基础。同时,这个实验还将为学生提供实践经验,让他们能够将所学知识应用到真实的软件开发和运维中。 ### 回答3: Lab: xv6 and Unix Utilities是一个计算机科学领域的实验,旨在让学生深入了解Unix操作系统以及操作系统本身的自我管理机制。在这个实验中,学生需要从零开始构建一个类似于Unix的操作系统,在这个操作系统中,学生需要设计一些基本命令,例如ls,cat,grep等等,并且将它们与系统的底层API结合起来,以实现各种功能。此外,学生还需要了解和探索xv6这个开发工具,它是一个轻量级基于Unix的操作系统实现,具有一定的可移植性和简洁性,因此,它可以作为一个基础框架来实现一个完整的Unix操作系统。 这个实验的目标是让学生了解Unix的基本命令结构和API,以及操作系统内部的一些基本机制,例如进程管理,文件系统交互以及进程通信等等。此外,通过实现这些命令,学生还可以学到一些基本的C语言编程技能,例如文件操作,字符串处理以及进程管理等等。还可以学习到如何使用Git等版本控制工具,以及如何进行调试和测试代码的技巧。 在整个实验过程中,学生需要有较强的自我管理能力和综合运用能力,因为在实现这些命令的同时,他们还需要和其他团队成员进行交流和合作,以及不断改进和完善他们的代码。总之,这个实验是一个非常有趣且富有挑战性的计算机科学课程,通过完成这个实验,学生可以更好地了解操作系统的构造和运作机制,以及如何设计和开发高效的系统级应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值