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 definitionsint main(int argc, char *argv[]) {int p[2];                // Array to store pipe file descriptorspipe(p);                 // Create a pipe, p[0] for reading, p[1] for writingchar buf[1];            // Buffer to store single character messagesif (fork() == 0) {      // Child processread(p[0], buf, 1);   // Read "p" from parentprintf("%d: received ping\n", getpid());  // Print child's PID and messagewrite(p[1], "p", 1);  // Send "p" back to parentclose(p[0]);          // Close read endclose(p[1]);          // Close write endexit(0);              // Exit child process} else {                // Parent processwrite(p[1], "p", 1);  // Send "p" to childread(p[0], buf, 1);   // Wait for child's responseprintf("%d: received pong\n", getpid());  // Print parent's PID and messagewait(0);              // Wait for child to exitclose(p[0]);          // Close read endclose(p[1]);          // Close write endexit(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.0xv6 kernel is bootinghart 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都做了什么。

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

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

相关文章

前端面经-VUE3篇(二)--vue3组件知识(一)组件注册、props 与 emits、透传、插槽(Slot)

组件允许我们将 UI 划分为独立的、可重用的部分&#xff0c;并且可以对每个部分进行单独的思考。在实际应用中&#xff0c;组件常常被组织成一个层层嵌套的树状结构&#xff1a; 一、注册 Vue 组件本质上是一个可以复用的 自定义 HTML 元素&#xff0c;为了在其他组件中使用一…

LeetCode —— 102. 二叉树的层序遍历

&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️Take your time ! &#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️…

Linux第20节 --- inode和文件系统

一、没有被打开的文件 如果一个文件没有被打开&#xff0c;那么该文件存储在哪里&#xff1f; 该文件是存储在磁盘当中的&#xff01; 文件 文件内容 文件属性&#xff01; 文件的内容是按照数据块存储的&#xff1b;文件的属性其实就是inode&#xff08;是一个128字节的…

1.PowerBi保姆级安装教程

1.进入power bi网站 PowerBi下载链接 2.下载power bi软件 3.双击安装 4.下一步 5.下一步 6.下一步 7.下一步 8.安装 9.双击桌面图标

Android Studio中OpenCV应用详解:图像处理、颜色对比与OCR识别

文章目录 一、OpenCV在Android中的集成与配置1.1 OpenCV简介1.2 在Android Studio中集成OpenCV1.2.1 通过Gradle依赖集成1.2.2 通过模块方式集成1.2.3 初始化OpenCV 1.3 OpenCV基础类介绍 二、指定区域图像抓取与对比2.1 图像抓取基础2.2 指定区域图像抓取实现2.2.1 从Bitmap中…

前端面试每日三题 - Day 22

今天我们将深入探讨 JavaScript 中的 Set 和 Map 数据结构&#xff0c;了解它们的特性及应用场景。接下来&#xff0c;我们会分析 React 的 Suspense 和 Concurrent Mode 的工作原理&#xff0c;探索它们如何提升应用的性能和用户体验。最后&#xff0c;我们将学习如何设计一个…

[Vue]编程式导航

在 Vue 中&#xff0c;编程式导航是通过 JavaScript 代码&#xff08;而非 <router-link> 标签&#xff09;动态控制路由跳转的核心方式。这个方法依赖于 Vue Router 提供的 API&#xff0c;能更灵活地处理复杂场景&#xff08;如异步操作、条件跳转等&#xff09;。 一、…

邹晓辉教授十余年前关于围棋程序与融智学的思考,体现了对复杂系统本质的深刻洞察,其观点在人工智能发展历程中具有前瞻性意义。我们可以从以下三个维度进行深入解析:

邹晓辉教授十余年前关于围棋程序与融智学的思考&#xff0c;体现了对复杂系统本质的深刻洞察&#xff0c;其观点在人工智能发展历程中具有前瞻性意义。我们可以从以下三个维度进行深入解析&#xff1a; 一、围棋程序的二元解构&#xff1a;数据结构与算法的辩证关系 1.1.形式…

The Traitor King (10 player 25 player)

The Traitor King 十字军试炼尾王成就。叛变的国王&#xff1a;在30秒内杀死40只虫群甲虫。考验团队配合的成就。比不朽者&#xff0c;黑曜石31等等强度大&#xff0c;甚至感觉比宝库地风火难。

数据结构一 单链表

1.单链表 1.数据结构简介 程序数据结构算法 数据 数据&#xff08;data&#xff09;是客观事物的一个符号表示 数据元素&#xff08;data element&#xff09;是数据的基本单位&#xff0c;一 个数据元素可以由若干个数据项&#xff08;data item&#xff09;组成。数据项…

GPU集群监控系统开发实录:基于Prometheus+Grafana的算力利用率可视化方案

一、科研场景下的GPU监控痛点 在深度学习模型训练、分子动力学模拟等科研场景中&#xff0c;GPU集群的算力利用率直接影响着科研效率。笔者在参与某高校计算中心的运维工作时&#xff0c;发现以下典型问题&#xff1a; 资源黑洞现象&#xff1a;多课题组共享GPU时出现"抢…

【计算机视觉】三维重建: MVSNet:基于深度学习的多视图立体视觉重建框架

MVSNet&#xff1a;基于深度学习的多视图立体视觉重建框架 技术架构与核心算法1. 算法流程2. 关键创新 环境配置与实战指南硬件要求安装步骤数据准备&#xff08;DTU数据集&#xff09; 实战流程1. 模型训练2. 深度图推断3. 点云生成 常见问题与解决方案1. CUDA内存不足2. 特征…

智能家居的OneNet云平台

一、声明 该项目只需要创建一个产品&#xff0c;然后这个产品里面包含几个设备&#xff0c;而不是直接创建几个产品 注意&#xff1a;传输数据使用到了不同的power&#xff0c;还有一定要手机先联网才能使用云平台 二、OneNet云平台创建 &#xff08;1&#xff09;Temperatur…

aidermacs开源程序使用 Aider 在 Emacs 中进行 AI 配对编程

一、软件介绍 文末提供程序和源码下载 Aidermacs 通过集成 Aider&#xff08;最强大的开源 AI 配对编程工具之一&#xff09;为 Emacs 带来了 AI 驱动的开发。如果您缺少 Cursor&#xff0c;但更喜欢生活在 Emacs 中&#xff0c;Aidermacs 提供了类似的 AI 功能&#xff0c;同…

加密算法(一)-对称加密(DES、AES、3DES、Blowfish、Twofish)一篇了解所有主流对称加密,轻松上手使用。

一、对称加密算法 对称加密算法采用相同的密钥来进行加密和解密操作。其优点是加密和解密速度快&#xff0c;不过密钥的管理和分发存在一定的安全风险。 1.1、DES(已不推荐使用) 这是早期的对称加密算法&#xff0c;密钥长度为 56 位。但由于密钥长度较短&#xff0c;如今已不…

深度优先VS广度优先:算法选择的核心逻辑与实战指南

摘要 深度优先搜索&#xff08;DFS&#xff09;与广度优先搜索&#xff08;BFS&#xff09;是图结构遍历与路径分析的基础算法&#xff0c;也是最常见的搜索框架&#xff0c;在路径规划、社交网络分析、游戏AI等领域均有广泛应用。本文从算法思想、数据结构选择、时空复杂度和…

2025深圳杯、东三省数学建模B题数模AI全网专业性第一

为什么选择使用我的数模AI&#xff1f; 1.轻松辅导学生 2.小白也能翻身碾压大佬 3.突破知识壁垒&#xff0c;缩短与大佬的差距&#xff0c;打破不公平的教学资源&#xff0c;扭转差距 4.辅助商业服务&#xff0c;成本低 5.大模型本身有一定随机性&#xff0c;所以也不用担心…

使用MGeo模型高精度实现文本中地址识别

一、功能与安装 1、模型地址 模型是阿里开发的门址高精度识别模型。 https://modelscope.cn/models/iic/mgeo_geographic_elements_tagging_chinese_base/summary 注意&#xff1a;不能自己安装包&#xff0c;没法解决依赖问题&#xff0c;直接按照官方要求安装下面的包&am…

【Vue】Vue与UI框架(Element Plus、Ant Design Vue、Vant)

个人主页&#xff1a;Guiat 归属专栏&#xff1a;Vue 文章目录 1. Vue UI 框架概述1.1 主流Vue UI框架简介1.2 选择UI框架的考虑因素 2. Element Plus详解2.1 Element Plus基础使用2.1.1 安装与引入2.1.2 基础组件示例 2.2 Element Plus主题定制2.3 Element Plus的优缺点分析 3…

MLPerf基准测试工具链定制开发指南:构建领域特异性评估指标的实践方法

引言&#xff1a;基准测试的领域适配困局 MLPerf作为机器学习性能评估的"黄金标准"&#xff0c;其通用基准集在实际科研中常面临‌领域适配鸿沟‌&#xff1a;医疗影像任务的Dice系数缺失、NLP场景的困惑度指标偏差等问题普遍存在。本文通过逆向工程MLPerf v3.1工具…