MIT XV6 - 1.2 Lab: Xv6 and Unix utilities - pingpong

接上文 MIT XV6 - 1.1 Lab: Xv6 and Unix utilities - user/_sleep 是什么?做什么?

pingpong

不务正业了那么久(然而并没有,虽然还在探索sleep,但是教材我已经看完了前三章了),让我们赶紧继续下去

在进行本实验之前请务必阅读完教材 Chapter 1,尤其是1.3对于PIPE的介绍,实验具体要求如下:

Write a user-level program that uses xv6 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 “pid: received ping”, where pid 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 “pid: received pong”, and exit. Your solution should be in the file user/pingpong.c.

Some hints:

  • Add the program to UPROGS in Makefile.
  • Use pipe to create a pipe.
  • Use fork to create a child.
  • Use read to read from a pipe, and write to write to a pipe.
  • Use getpid to find the process ID of the calling process.
  • User programs on xv6 have a limited set of library functions available to them. You can see the list in user/user.h; the source (other than for system calls) is in user/ulib.c, user/printf.c, and user/umalloc.c.

Run the program from the xv6 shell and it should produce the following output:

make qemu
...
init: start sh
$ pingpong
4: received ping
3: received pong
$

整体还是比较简单的,主要你得理解forkpipe的用法,以及一些要点,比如fork的返回值如果是子进程,那么会返回0;read会一直等到有足够的输入或者文件描述符被关闭啊。这些都在课本中有描述。

以下是实验源码(这注释,不用想,一定是AI写的…) GitHub已经同步

/*
 * pingpong.c - A simple program demonstrating inter-process communication using pipes
 * 
 * This program creates a parent-child process pair that communicate through a pipe.
 * The parent sends a "ping" message to the child, and the child responds with a "pong".
 * 
 * Communication Flow:
 * 1. Parent creates a pipe
 * 2. Parent forks a child process
 * 3. Both processes have access to the pipe's read and write ends
 * 4. Parent writes "p" to pipe and waits for response
 * 5. Child reads "p" from pipe, prints "received ping", and writes "p" back
 * 6. Parent reads "p" from pipe and prints "received pong"
 * 
 * Timing Diagram:
 * 
 * Parent Process          Child Process
 *     |                       |
 *     |--pipe creation------>|
 *     |                       |
 *     |--fork()------------->|
 *     |                       |
 *     |--write("p")--------->|
 *     |                       |
 *     |<--read("p")----------|
 *     |                       |
 *     |<--write("p")---------|
 *     |                       |
 *     |--read("p")---------->|
 *     |                       |
 *     |--wait()------------->|
 *     |                       |
 *     |<--exit()-------------|
 *     |                       |
 */

#include "kernel/types.h"  // Include kernel type definitions
#include "user/user.h"     // Include user-level system call definitions

int main(int argc, char *argv[]) {
  int p[2];                // Array to store pipe file descriptors
  pipe(p);                 // Create a pipe, p[0] for reading, p[1] for writing
  
  char buf[1];            // Buffer to store single character messages
  
  if (fork() == 0) {      // Child process
    read(p[0], buf, 1);   // Read "p" from parent
    printf("%d: received ping\n", getpid());  // Print child's PID and message
    write(p[1], "p", 1);  // Send "p" back to parent
    close(p[0]);          // Close read end
    close(p[1]);          // Close write end
    exit(0);              // Exit child process
  } else {                // Parent process
    write(p[1], "p", 1);  // Send "p" to child
    read(p[0], buf, 1);   // Wait for child's response
    printf("%d: received pong\n", getpid());  // Print parent's PID and message
    wait(0);              // Wait for child to exit
    close(p[0]);          // Close read end
    close(p[1]);          // Close write end
    exit(0);              // Exit parent process
  }
  return 0;
}

实验结果

make qemu
qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 128M -smp 3 -nographic -global virtio-mmio.force-legacy=false -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0

xv6 kernel is booting

hart 1 starting
hart 2 starting
init: starting sh
$ pingpong
4: received ping
3: received pong
$ QEMU: Terminated

ut结果

./grade-lab-util pingpong
make: `kernel/kernel' is up to date.
== Test pingpong == pingpong: OK (1.1s) 

我觉得得找时间看看他ut都做了什么。

### 回答1:xv6是一个基于Unix操作系统,它是一个教学用途的操作系统,旨在教授操作系统的基本概念和实现。它是在MIT的x86架构上开发的,包括了Unix的一些基本功能,如进程管理、文件系统、内存管理等。xv6的源代码是公开的,可以用于学习和研究。 Unix utilitiesUnix操作系统中的一些基本工具,如ls、cd、cp、mv、rm等。这些工具可以帮助用户管理文件和目录,执行各种操作。这些工具的实现是基于Unix的系统调用,可以通过编写C程序来调用这些系统调用实现相应的功能。这些工具是Unix操作系统的基础,也是其他操作系统的参考。 ### 回答2lab: 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、付费专栏及课程。

余额充值