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

news2025/5/12 22:29:00

接上文 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 process
  if (pipe_left[PIPE_WRITE] == PIPE_INVALID) {
    int ret = pipe(pipe_left);
    if (ret < 0) {
      exit(1);
    }

    ret = fork();
    if (ret == 0) {
      // Child process
      close_pipe_if_valid(pipe_left[PIPE_WRITE]);

      // Print the prime number this process represents
      print_prime(n);

      // Initialize right pipe for passing numbers to next process
      int pipe_right[2] = {PIPE_INVALID, PIPE_INVALID};
      int received_number = 0;

      // Read numbers from left pipe and filter them
      while (read(pipe_left[PIPE_READ], &received_number,
                  sizeof(received_number)) > 0) {
        // Skip numbers that are multiples of current prime
        if (received_number % n == 0) {
          continue;
        }

        // Pass non-multiples to next process
        deliver_prime(received_number, pipe_right);
      }

      // Clean up pipes
      close_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 complete
      wait(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 pipe
    if (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 process
  int p[2] = {PIPE_INVALID, PIPE_INVALID};

  // Print the first prime number
  print_prime(START_PRIME);

  // Process numbers from START_PRIME + 1 to MAX_PRIMES
  for (int i = START_PRIME + 1; i <= MAX_PRIMES; i++) {
    // Skip multiples of START_PRIME
    if (i % START_PRIME == 0) {
      continue;
    }

    // Process the number through the pipe system
    deliver_prime(i, p);
  }

  // Clean up pipes
  close(p[PIPE_READ]);
  close(p[PIPE_WRITE]);

  // Wait for child process to complete
  wait(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.0

xv6 kernel is booting

hart 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.0

xv6 kernel is booting

hart 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?留个坑吧…

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

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

相关文章

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

别再让才华被埋没&#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…

【音视频工具】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 …

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…

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 …

实践005-Gitlab CICD全项目整合

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

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

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

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.…

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

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

ideal创建Springboot项目(Maven,yml)

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

Pycharm(十九)深度学习

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

Scrapyd 详解:分布式爬虫部署与管理利器

Scrapyd 是 Scrapy 官方提供的爬虫部署与管理平台&#xff0c;支持分布式爬虫部署、定时任务调度、远程管理爬虫等功能。本文将深入讲解 Scrapyd 的核心功能、安装配置、爬虫部署流程、API 接口使用&#xff0c;以及如何结合 Scrapy-Redis 实现分布式爬虫管理。通过本文&#x…

驱动开发硬核特训 · 专题篇:Vivante GPU 与 DRM 图形显示体系全解析(i.MX8MP 平台实战)

视频教程请关注 B 站&#xff1a;“嵌入式Jerry”。 一、背景导读&#xff1a;GPU 与 DRM 到底谁负责“显示”&#xff1f; 在嵌入式 Linux 图形系统中&#xff0c;“画面怎么显示出来”的问题&#xff0c;表面看似简单&#xff0c;实则涉及多个内核子系统与用户态组件的协同&…