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

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

文章目录

  • [1. 作业要求](#1. 作业要求)
    • [primes (moderate)/(hard)](#primes (moderate)/(hard))
  • [2. 实现过程](#2. 实现过程)
    • [2.1 代码实现](#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_10和fd_11 fd10和fd11)。
  • 主进程 p 1 p_1 p1将2~35写到第1个管道的写描述符 f d 1 1 fd_11 fd11。
  • 子进程 n p 1 np_1 np1从第1个管道的读描述符 f d 1 0 fd_10 fd10中read第一个数字打印:2,为了将主进程发来的剩余数字继续发送到右边,必然不是写到 f d 1 1 fd_11 fd11,这样和上面图片的数据方向就不一样了。我们需要在子进程创建新的管道 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_21 fd21,在子进程 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类型的指针即可。
  • 主要关闭不需要的管道描述符号。
cpp 复制代码
#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 pipe
    int 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里加入对这个函数的编译:

c 复制代码
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,如题所述正确打印:

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

cpp 复制代码
./grade-lab-util primes

完活!

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

相关推荐
李剑一3 天前
早在八十年代我们就有国产操作系统,但为什么最后还是Windows一统天下,答案藏在时间里
操作系统
Java成神之路-3 天前
虚拟内存核心精讲:页表、VMA、缺页异常与mmap(单级页表 + 多级页表对照)
操作系统
OpenAnolis小助手5 天前
RISC-V 基础软件的工程化与性能优化实践
性能优化·操作系统·龙蜥社区·risc-v
青梅味猪大肠6 天前
【操作系统-60】临近适应算法
java·linux·开发语言·嵌入式硬件·算法·操作系统
PMAOS9 天前
4G 功能机操作系统选型指南:KaiOS、Mocor、MAUI、PMAOS 的能力边界与验证方法
嵌入式硬件·ai·操作系统
罗四强9 天前
第10篇:打包上架:签名、打包 HAP、发布到华为应用市场
分布式·操作系统·harmonyos·arkts·鸿蒙系统
Liaiyang6611 天前
空圈容错视角-HarmonyOS 7 架构体检:从被动容错到主动免疫的真伪验证与生态反推
操作系统·harmonyos·架构设计·ai-agent·端云协同·鸿蒙7·空圈容错
OpenAnolis小助手11 天前
龙蜥社区亮相 GOSIM AI Vision Forum 共话“Agentic Native”开源新
人工智能·ai·开源·操作系统·龙蜥社区·agent
阿里云基础软件11 天前
Agent 任务提速 10.71%,内存占用降低 21.60%|Alibaba Cloud Linux 4 重大发布
java·linux·人工智能·操作系统·agent
OpenAnolis小助手12 天前
多租户公有云共享 CXL 内存池场景下的内存卸载研究
人工智能·操作系统·龙蜥社区·risc-v·agentic os