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.pswp.cn/web/78264.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/78264.shtml
英文地址,請注明出處:http://en.pswp.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工具…