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

【MIT-OS6.S081作业1.3】Lab1-utilities primes

本文记录MIT-OS6.S081 Lab1 utilities 的primes函数的实现过程

文章目录

  • 1. 作业要求
    • primes (moderate)/(hard)
  • 2. 实现过程
    • 2.1 代码实现

1. 作业要求

primes (moderate)/(hard)

Write a concurrent version of prime sieve using pipes. This idea is due to Doug McIlroy, inventor of Unix pipes. The picture halfway down this page and the surrounding text explain how to do it. 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.
  • Your solution is correct if it implements a pipe-based sieve and produces the following output:

$ make qemu

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
$

2. 实现过程

2.1 代码实现

作业提供了算法的链接:
Bell Labs and CSP Threads
在这里插入图片描述

在这里插入图片描述

先读取左边传来的所有数字,打印出第一个数字p,然后继续循环read左边给到的数字n,如果p不能被n整除,那么就把n继续发送给右边。

为了实现这个功能,我们这里需要使用fork和管道,fork有两个进程:主进程和子进程,我们分析一下它们分别是怎么做的:

  • 第1个fork创建主进程 p 1 p_1 p1和子进程 n p 1 np_1 np1,从前面的ping-pong实验我们知道,主进程和子进程可以一个实现读,一个实现写,这是通过管道来实现的,于是我们创建一个管道 f d 1 fd_1 fd1(分别有 f d 1 [ 0 ] 和 f d 1 [ 1 ] fd_1[0]和fd_1[1] fd1[0]fd1[1])。
  • 主进程 p 1 p_1 p1将2~35写到第1个管道的写描述符 f d 1 [ 1 ] fd_1[1] fd1[1]
  • 子进程 n p 1 np_1 np1从第1个管道的读描述符 f d 1 [ 0 ] fd_1[0] fd1[0]中read第一个数字打印:2,为了将主进程发来的剩余数字继续发送到右边,必然不是写到 f d 1 [ 1 ] fd_1[1] fd1[1],这样和上面图片的数据方向就不一样了。我们需要在子进程创建新的管道 f d 2 fd_2 fd2,同时后面的逻辑还是类似的为主进程和子进程读写,我们依旧使用fork来创建得到主进程 p 2 p_2 p2和子进程 n p 2 np_2 np2(这里的 n p 1 np_1 np1 p 2 p_2 p2是一样的),于是我们可以把主进程 p 1 p_1 p1发来的剩余数字在子进程的主进程 p 2 p_2 p2里写到第2个管道的写描述符 f d 2 [ 1 ] fd_2[1] fd2[1],在子进程 n p 2 np_2 np2来处理同样的读写,后面继续进行这样的读写操作…
  • 我们可以使用递归【这样也就实现了提示说的You should create the processes in the pipeline only as they are needed.】。
  • 递归停止条件:子进程从管道的读描述符读取第一个数字时返回0【read returns zero when the write-side of a pipe is closed.】,那么递归终止,递归过程在递归函数里建立新的管道,但是读取数字需要知道前一个管道,所以递归传参是前一个管道描述符的指针(指针传参)。

注意事项:

  • write可以直接写入int类型的数字【It’s simplest to directly write 32-bit (4-byte) ints to the pipes, rather than using formatted ASCII I/O.】,传入int类型的指针即可。
  • 主要关闭不需要的管道描述符号。
#include "kernel/types.h"
#include "user/user.h"void processPrime(int* p)
{close(p[1]);int num;if (0 == read(p[0], &num, sizeof(num))){close(p[0]);exit(0);}printf("prime %d\n", num);// create new pipeint nPipe[2];pipe(nPipe);int ret = fork();if (ret == 0){processPrime(nPipe);}else{close(nPipe[0]);int nNum;while (read(p[0], &nNum, sizeof(nNum))){if (nNum % num != 0)write(nPipe[1], &nNum, sizeof(nNum));}close(p[0]);close(nPipe[1]);wait(0);}exit(0);
}
int main(int argc, char* argv[])
{int p[2];pipe(p);int ret = fork();if (ret == 0){processPrime(p);}    else{close(p[0]);for (int i = 2; i <= 35; ++i)write(p[1], &i, sizeof(i));close(p[1]);wait(0);}exit(0);
}

我们在Makefile里加入对这个函数的编译:

UPROGS=\$U/_cat\$U/_echo\$U/_forktest\$U/_grep\$U/_init\$U/_kill\$U/_ln\$U/_ls\$U/_mkdir\$U/_rm\$U/_sh\$U/_stressfs\$U/_usertests\$U/_grind\$U/_wc\$U/_zombie\$U/_sleep\ $U/_pingpong\ $U/_primes\ 

重新编译通过,测试primes,如题所述正确打印:

在这里插入图片描述

确实过了一会然后可以继续输入命令,然后我们再使用作业所说的测试命令:

./grade-lab-util primes

在这里插入图片描述

完活!

递归来写还是比较清晰的。

http://www.lryc.cn/news/502146.html

相关文章:

  • 游戏引擎学习第35天
  • learn-(Uni-app)输入框u-search父子组件与input输入框(防抖与搜索触发)
  • 设置IMX6ULL开发板的网卡IP的两种方法(临时生效和永久有效两种方法)
  • 流量转发利器之Burpsuite概述(1)
  • Transformer入门(6)Transformer编码器的前馈网络、加法和归一化模块
  • element-plus中的resetFields()方法
  • 【过滤器】.NET开源 ORM 框架 SqlSugar 系列
  • Jmeter Address already in use: connect 解决
  • C#常见错误—空对象错误
  • Leetcode数学部分笔记
  • 微信小程序web-view 嵌套h5界面 实现文件预览效果
  • 【汽车】-- 燃油发动机3缸和4缸
  • 轻量级的 HTML 模板引擎
  • Mysql | 尚硅谷 | 第02章_MySQL环境搭建
  • Maven学习(传统Jar包管理、Maven依赖管理(导入坐标)、快速下载指定jar包)
  • CTF: 在本地虚拟机内部署CTF题目docker
  • 视频推拉流EasyDSS无人机直播技术巡查焚烧、烟火情况
  • SpringBoot【十一】mybatis-plus实现多数据源配置,开箱即用!
  • 【嵌入式linux基础】关于linux文件多次的open
  • TPAMI 2023:When Object Detection Meets Knowledge Distillation: A Survey
  • 2024前端面试题(持续更新)
  • apache转nginx访问变成下载解决方法
  • 【iOS】OC高级编程 iOS多线程与内存管理阅读笔记——自动引用计数(三)
  • Oracle数据库使用dblink是时出现 ORA-12170:TNS:连接超时
  • OpenHarmony系统中实现Android虚拟化、模拟器相关的功能,包括桌面显示,详细解决方案
  • 决策曲线分析(DCA)中平均净阈值用于评价模型算法(R自定义函数)
  • 《经验分享 · 软考系统分析师》
  • 记录一下 js encodeURI和encodeURIComponent URL转码问题
  • 【C语言】二维前缀和/求子矩阵之和
  • SRS 服务器入门:实时流媒体传输的理想选择