当前位置: 首页 > article >正文

MIT XV6 - 1.3 Lab: Xv6 and Unix utilities - primes

接上文 MIT XV6 - 1.2 Lab: Xv6 and Unix utilities - pingpong

primes

继续实验,实验介绍和要求如下 (原文链接 译文链接) :

Write a concurrent prime sieve program for xv6 using pipes and the design illustrated in the picture halfway down this page and the surrounding text. This idea is due to Doug McIlroy, inventor of Unix pipes. Your solution should be in the file user/primes.c.

Your goal is to use pipe and fork to set up the pipeline. The first process feeds the numbers 2 through 35 into the pipeline. For each prime number, you will arrange to create one process that reads from its left neighbor over a pipe and writes to its right neighbor over another pipe. Since xv6 has limited number of file descriptors and processes, the first process can stop at 35.

Some hints:

  • Be careful to close file descriptors that a process doesn’t need, because otherwise your program will run xv6 out of resources before the first process reaches 35.
  • Once the first process reaches 35, it should wait until the entire pipeline terminates, including all children, grandchildren, &c. Thus the main primes process should only exit after all the output has been printed, and after all the other primes processes have exited.
  • Hint: read returns zero when the write-side of a pipe is closed.
  • It’s simplest to directly write 32-bit (4-byte) ints to the pipes, rather than using formatted ASCII I/O.
  • You should create the processes in the pipeline only as they are needed.
  • Add the program to UPROGS in Makefile.

大致意思呢,参考 Unix pipes的创造者,大佬Doug McIlroy的 Bell Labs and CSP Threads,基于多进程和管道来筛选一定范围内的素数.

在这里插入图片描述
什么意思呢,每个进程呢就像一个过滤器,把自己处理不了的数据扔给下一级,我是这么理解的啊,这样可能更符合这个实验?

地铁路上想好了怎么写,回来一遍过

/** Prime Number Sieve using Pipes* * This program implements the Sieve of Eratosthenes algorithm using pipes and processes* to find prime numbers concurrently. The algorithm works as follows:* * 1. Each process represents a prime number and filters out its multiples* 2. Numbers are passed through pipes between processes* 3. Each process reads numbers from its left pipe and writes non-multiples to its right pipe* * Process Communication Flow:* * Process 1 (2) -> Process 2 (3) -> Process 3 (5) -> Process 4 (7) -> ...*    [pipe1]         [pipe2]         [pipe3]          [pipe4]* * Each process:* 1. Reads numbers from left pipe* 2. If number is not divisible by its prime, passes it to right pipe* 3. Creates new child process for first number it receives* * Example flow for numbers 2-10:* * Time   Process 1    Process 2    Process 3    Process 4* ---------------------------------------------------------* t0      reads 2      -            -            -* t1      sends 3      reads 3      -            -* t2      sends 5      sends 5      reads 5      -* t3      sends 7      sends 7      sends 7      reads 7* t4      sends 9      reads 9      -            -* * Note: Numbers 4, 6, 8, 10 are filtered out by Process 1 (multiples of 2)*       Numbers 9 is filtered out by Process 2 (multiple of 3)*       Only prime numbers reach their corresponding processes*/#include "kernel/types.h"
#include "user/user.h"// Constants for prime number calculation
#define START_PRIME 2        // First prime number to start with
#define MAX_PRIMES 35       // Maximum number to check for primes// Pipe file descriptor constants
#define PIPE_INVALID -1      // Invalid pipe descriptor
#define PIPE_READ 0          // Read end of pipe
#define PIPE_WRITE 1         // Write end of pipe/*** Prints a prime number to the console* @param n The prime number to print*/
void print_prime(int n) { printf("prime %d\n", n); }/*** Safely closes a pipe file descriptor if it's valid* @param p The pipe file descriptor to close*/
void close_pipe_if_valid(int p) {if (p != PIPE_INVALID) {close(p);}
}/*** Delivers a number through the pipe system and creates new processes for primes* @param n The number to process* @param pipe_left The left pipe for receiving numbers*/
void deliver_prime(int n, int pipe_left[2]) {// If pipe is not initialized, create it and fork a new processif (pipe_left[PIPE_WRITE] == PIPE_INVALID) {int ret = pipe(pipe_left);if (ret < 0) {exit(1);}ret = fork();if (ret == 0) {// Child processclose_pipe_if_valid(pipe_left[PIPE_WRITE]);// Print the prime number this process representsprint_prime(n);// Initialize right pipe for passing numbers to next processint pipe_right[2] = {PIPE_INVALID, PIPE_INVALID};int received_number = 0;// Read numbers from left pipe and filter themwhile (read(pipe_left[PIPE_READ], &received_number,sizeof(received_number)) > 0) {// Skip numbers that are multiples of current primeif (received_number % n == 0) {continue;}// Pass non-multiples to next processdeliver_prime(received_number, pipe_right);}// Clean up pipesclose_pipe_if_valid(pipe_left[PIPE_READ]);close_pipe_if_valid(pipe_right[PIPE_READ]);close_pipe_if_valid(pipe_right[PIPE_WRITE]);// Wait for child process to completewait(0);exit(0);} else if (ret > 0) {// Parent process continues} else {printf("fork error, current index: %d\n", n);exit(1);}} else {// printf("deliver_prime: %d\n", n);// Write number to pipeif (write(pipe_left[PIPE_WRITE], &n, sizeof(n)) <= 0) {exit(1);}}
}/*** Main function that initiates the prime number calculation* @param argc Number of command line arguments* @param argv Command line arguments* @return 0 on successful execution*/
int main(int argc, char *argv[]) {// Initialize pipe for first processint p[2] = {PIPE_INVALID, PIPE_INVALID};// Print the first prime numberprint_prime(START_PRIME);// Process numbers from START_PRIME + 1 to MAX_PRIMESfor (int i = START_PRIME + 1; i <= MAX_PRIMES; i++) {// Skip multiples of START_PRIMEif (i % START_PRIME == 0) {continue;}// Process the number through the pipe systemdeliver_prime(i, p);}// Clean up pipesclose(p[PIPE_READ]);close(p[PIPE_WRITE]);// Wait for child process to completewait(0);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
$ primes
prime 2
prime 3
prime 5
prime 7
prime 11
prime 13
prime 17
prime 19
prime 23
prime 29
prime 31

他实验中提到一点 Since xv6 has limited number of file descriptors and processes, the first process can stop at 35.,于是我把 MAX_PRIMES改成了100并加了一些错误日志,试一下什么时候会"资源耗尽"。

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 2 starting
hart 1 starting
init: starting sh
$ primes
prime 2
prime 3
prime 5
prime 7
prime 11
prime 13
prime 17
prime 19
prime 23
prime 29
prime 31
prime 37
prime 41
pipe error, current index: 43
$ 

为什么是43?留个坑吧…

相关文章:

MIT XV6 - 1.3 Lab: Xv6 and Unix utilities - primes

接上文 MIT XV6 - 1.2 Lab: Xv6 and Unix utilities - pingpong primes 继续实验&#xff0c;实验介绍和要求如下 (原文链接 译文链接) : Write a concurrent prime sieve program for xv6 using pipes and the design illustrated in the picture halfway down this page and…...

Android学习总结之kotlin协程面试篇

一、协程基础概念与原理类真题 真题 1&#xff1a;协程是线程吗&#xff1f;为什么说它是轻量级的&#xff1f;&#xff08;字节跳动 / 美团&#xff09; 解答&#xff1a; 本质区别&#xff1a; 线程是操作系统调度的最小单位&#xff08;内核态&#xff09;&#xff0c;协…...

从前端视角看网络协议的演进

别再让才华被埋没&#xff0c;别再让github 项目蒙尘&#xff01;github star 请点击 GitHub 在线专业服务直通车GitHub赋能精灵 - 艾米莉&#xff0c;立即加入这场席卷全球开发者的星光革命&#xff01;若你有快速提升github Star github 加星数的需求&#xff0c;访问taimili…...

Docker中运行的Chrome崩溃问题解决

问题 各位看官是否在 Docker 容器中的 Linux 桌面环境&#xff08;如Xfce&#xff09;上启动Chrome &#xff0c;遇到了令人沮丧的频繁崩溃问题&#xff1f;尤其是在打开包含图片、视频的网页&#xff0c;或者进行一些稍复杂的操作时&#xff0c;窗口突然消失&#xff1f;如果…...

【沉浸式求职学习day36】【初识Maven】

沉浸式求职学习 Maven1. Maven项目架构管理工具2.下载安装Maven3.利用Tomcat和Maven进入一个网站 Maven 为什么要学习这个技术&#xff1f; 在Java Web开发中&#xff0c;需要使用大量的jar包&#xff0c;我们手动去导入&#xff0c;这种操作很麻烦&#xff0c;PASS&#xff01…...

ES面试题系列「一」

1、Elasticsearch 是什么&#xff1f;它与传统数据库有什么区别&#xff1f; 答案&#xff1a;Elasticsearch 是一个基于 Lucene 的分布式、开源的搜索和分析引擎&#xff0c;主要用于处理大量的文本数据&#xff0c;提供快速的搜索和分析功能。与传统数据库相比&#xff0c;E…...

【音视频工具】MP4BOX使用

这里写目录标题 使用介绍 使用 下面这个网站直接使用&#xff1a; MP4Box.js - JavaScript MP4 Reader/Fragmenter (gpac.github.io) 介绍 MMP4Box 是 GPAC 项目开发的一款命令行工具&#xff0c;专门用于处理 MP4 格式多媒体文件&#xff0c;也可操作 AVI、MPG、TS 等格…...

Linux中常见开发工具简单介绍

目录 apt/yum 介绍 常用命令 install remove list vim 介绍 常用模式 命令模式 插入模式 批量操作 底行模式 模式替换图 vim的配置文件 gcc/g 介绍 处理过程 预处理 编译 汇编 链接 库 静态库 动态库&#xff08;共享库&#xff09; make/Makefile …...

laravel 使用异步队列,context带的上下文造成反序列化出问题

2025年5月8日17:03:44 如果你是单个应用&#xff0c;异步递交任务&#xff0c;是在应用内部使用&#xff0c;一般不会发生这样的问题 但是现在app项目是 app是一个应用&#xff0c;admin是一个应用&#xff0c;app吧为了接口性能吧异步任务丢给admin去执行&#xff0c;如果两个…...

flow-matching 之学习matcha-tts cosyvoice

文章目录 matcha 实现cosyvoice 实现chunk_fmchunk_maskcache_attn stream token2wav 关于flow-matching 很好的原理性解释文章&#xff0c; 值得仔细读&#xff0c;多读几遍&#xff0c;关于文章Flow Straight and Fast: Learning to Generate and Transfer Data with Rectifi…...

视频编解码学习三之显示器续

一、现在主流的显示器是LCD显示器吗&#xff1f; 是的&#xff0c;现在主流的显示器仍然是 LCD&#xff08;液晶显示器&#xff0c;Liquid Crystal Display&#xff09;&#xff0c;但它已经细分为多种技术类型&#xff0c;并和其他显示技术&#xff08;如OLED&#xff09;形成…...

ubuntu22.04在 Docker容器中安装 ROS2-Humble

22.04 安装 docker 容器并实现rviz功能 1 docker pull命令拉取包含ROS-Humble的镜像&#xff1a; docker pull osrf/ros:humble-desktop-full-jammy docker images验证该镜像是否拉取成功。 使用镜像osrf/ros:humble-desktop-full-jammy创建并运行容器 sudo docker run -it…...

【JavaWeb+后端常用部件】

回顾内容看&#xff1a; 一、获取请求参数的方法 参考&#xff1a;[JavaWeb]——获取请求参数的方式(全面&#xff01;&#xff01;&#xff01;)_java 获取请求参数-CSDN博客 Json格式的Body加备注RequestBody{id}动态路径加备注PathVariableid?&name?直接接收就好 i…...

Redis 重回开源怀抱:开源精神的回归与未来展望

在开源软件的广袤天地里&#xff0c;Redis 一直是备受瞩目的明星项目。近期&#xff0c;Redis 宣布重新回归开源&#xff0c;这一消息犹如一颗石子投入平静的湖面&#xff0c;在技术社区激起层层涟漪。今天&#xff0c;就让我们深入了解 Redis 这一重大转变背后的故事、意义以及…...

弹窗表单的使用,基于element-ui二次封装

el-dialog-form 介绍 基于element-ui封装的弹窗式表单组件 示例 git地址 https://gitee.com/chenfency/el-dialog-form.git 更新日志 2021-8-12 版本1.0.0 2021-8-17 优化组件&#xff0c;兼容element原组件所有Attributes及Events 2021-9-9 新增tip提示 安装教程 npm install …...

Unity打包安卓失败 Build failure 解决方法

【Unity】打包安卓失败 Build failure 的解决方法_com.android.build.gradle.internal.res.linkapplicat-CSDN博客 unity在打包时设置手机屏幕横屏竖屏的方法_unity打包默认横屏-CSDN博客...

Flink + Kafka 数据血缘追踪与审计机制实战

一、引言 在实时数据系统中,“我的数据从哪来?去往何处?” 是业务方最关心的问题之一。 尤其在以下场景下: 📉 金融风控:模型出现预警,需回溯数据源链路。 🧾 合规审计:监管要求提供数据全流程路径。 🛠 运维排查:Kafka Topic 数据乱序或错发后快速定位来源。 …...

实践005-Gitlab CICD全项目整合

文章目录 环境准备环境准备集成Kubernetes Gitlab CICD项目整合项目整合整合设计 后端Java项目部署后端Java项目静态检查后端Java项目镜像构建创建Java项目部署文件创建完整流水线 前端webui项目部署前端webui项目镜像构建创建webui项目部署文件创建完整流水线 构建父子类型流水…...

懒人美食帮SpringBoot订餐系统开发实现

概述 快速构建一个订餐系统&#xff0c;今天&#xff0c;我们将通过”懒人美食帮”这个基于SpringBoot的订餐系统项目&#xff0c;为大家详细解析从用户登录到多角色权限管理的完整实现方案。本教程特别适合想要学习企业级应用开发的初学者。 主要内容 1. 用户系统设计与实现…...

css animation 动画属性

animation // 要绑定的关键帧规则名称 animation-name: slidein;// 定义动画完成一个周期所需的时间&#xff0c;秒或毫秒 animation-duration: 3s;// 定义动画速度曲线 animation-timing-function: ease;// 定义动画开始前的延迟时间 animation-delay: 1s;// 定义动画播放次数…...

MySQL 从入门到精通(六):视图全面详解 —— 虚拟表的灵活运用

在数据库开发中&#xff0c;我们经常需要重复执行复杂的多表查询&#xff0c;或是需要限制用户只能访问特定数据。这时候&#xff0c;MySQL 的 视图&#xff08;View&#xff09;就能大显身手。作为一种 “虚拟表”&#xff0c;视图不存储实际数据&#xff0c;却能基于 SQL 查询…...

手机隐私数据彻底删除工具:回收或弃用手机前防数据恢复

软件介绍 有这样一款由吾爱网友chenwangjun 原创开发的数据处理软件&#xff0c;名为 AndroidDiskClear。它的核心功能十分强大&#xff0c;能够将你手机里已经删除的各类文件&#xff0c;像图片、普通文件、文字信息等彻底清除干净&#xff0c;有效杜绝数据恢复类软件的二次恢…...

数据压缩实现案例

在driver中修改代码 package com.root.mapreduce.compress; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.…...

python实战项目69:基于Python爬虫的链家二手房数据采集方法研究

python实战项目69:链家二手房数据采集 一、项目需求1.1 房地产数据价值1.2 传统数据获取局限性1.3 技术可行性二、数据采集流程2.1 需求分析2.2 网页结构分析2.3 请求发送与反爬策略2.4 数据解析2.5 数据存储三、结论与展望四、完整代码一、项目需求 本文针对房地产数据分析需…...

xml与注解的区别

功能xml配置注解定义bean bean标签 id属性 class属性 Component Controller Service Repository ComponentScan 设置依赖注入 setter注入&#xff08;set方法&#xff09; 构造器注入&#xff08;构造方法&#xff09; Autowired Qualifier Value 配置第三方bean bean标签 静…...

FlySecAgent:——MCP全自动AI Agent的实战利器

最近&#xff0c;出于对人工智能在网络安全领域应用潜力的浓厚兴趣&#xff0c;我利用闲暇时间进行了深入研究&#xff0c;并成功开发了一款小型轻量化的AI Agent安全客户端FlySecAgent。 什么是 FlySecAgent&#xff1f; 这是一个基于大语言模型和MCP&#xff08;Model-Contr…...

利用flask设计接口

Flask 接口设计详尽指南&#xff08;整合知识库优化版&#xff09; 目录 基础概念与安装接口设计规范核心功能实现高级特性扩展错误处理与调试部署与优化完整示例 基础概念与安装 安装 Flask pip install Flask项目结构建议 my_flask_api/ ├── app.py # 主…...

ideal创建Springboot项目(Maven,yml)

以下是使用 IntelliJ IDEA 创建基于 Maven 的 Spring Boot 项目并使用 YAML 配置文件的详细步骤&#xff1a; 一、创建 Spring Boot 项目 启动项目创建向导 打开 IntelliJ IDEA&#xff0c;点击“File”->“New”->“Project”。 在弹出的“New Project”窗口中&#…...

Pycharm(十九)深度学习

一、深度学习概述 1.1 什么是深度学习 深度学习是机器学习中的一种特殊方法,它使用称为神经网络的复杂结构,特别是“深层”的神经网络,来学习和做出预测。深度学习特别适合处理大规模和高维度的数据,如图像、声音和文本。深度学习、机器学习和人工智能之间的关系如下图所…...

XSS 攻击:深入剖析“暗藏在网页中的脚本“与防御之道

XSS (Cross-Site Scripting)&#xff0c;即跨站脚本攻击&#xff0c;是 Web 安全领域中最常见也最具危害性的漏洞之一。攻击者通过巧妙的手段将恶意的 JavaScript、HTML 或其他脚本代码注入到正常的 Web 页面中。当其他用户浏览这些被注入了恶意脚本的页面时&#xff0c;这些脚…...