zoukankan      html  css  js  c++  java
  • 《现代操作系统》精读与思考笔记 第五章 输入/输出

      本系列博文是《现代操作系统(英文第三版)》(Modern Operating Systems,简称MOS)的阅读笔记,定位是正文精要部分的摘录理解和课后习题精解,因此不会事无巨细的全面摘抄,仅仅根据个人情况进行记录和推荐。由于是英文版,部分内容会使用英文原文。

      课后习题的选择标准:尽量避免单纯的概念考察(如:What is spooling?)或者简单的数值计算,而是能够引起思考加深理解的题目。为了保证解答的正确性,每道题都会附上原书解答,而中文部分会适当加入自己的见解。原书答案下载地址(需注册)

    1.用代码描述程序驱动I/O、中断驱动I/O、DMA的I/O(P344~347)

    情景:

      使用打印机打印一个字符串。

      打印机的接口是两个寄存器:printer _data_ register用来存放下一个要打印的字符,printer _status_reg存放当前的状态供读取。

      由于打印机的限制,一次只能打印这个字符串的一个字符。不考虑进程竞争,打印机独占使用。

      字符串必须先拷贝至内核空间,然后再打印。

    (1)程序控制I/O

    copy_from_user(buffer, p, count);        /* p is the kernel buffer */
    for (i = 0; i <count; i++) {           /* loop on every character */
        while (*printer _status_reg != READY) ;  /*  loop until ready */ 
        *printer _data_ register = p[i];      /* output one character */ 
    }
    return_ to_ user(); 

      这种行为方式也称轮询(poll)或忙等(busy waiting)。

    (2)中断驱动I/O

    //当使用打印的系统调用时执行的代码
    copy_trom_user(buffer, p, count); 
    enable_interrupts( ); 
    while (*printer _status_ reg != READY) 
      ;
    *printer _data_ register = p[0]; scheduler();
    //打印机的中断服务例程,每次中断时执行
    if (count== 0) { 
      unblock_ user(); 
    } else {  
      *printer _data_ register= p[i]; 
      count = count-1; 
      i = i + 1; 
    }
    acknowledge_interrupt( );
    return_from_interrupt( ); 

    (3)DMA

    //当使用打印的系统调用时执行的代码
    copy_trom_user(buffer, p, count); 
    set_up_DMA_controller( ); 
    scheduler();
    //打印机的中断服务例程,每次中断时执行
    acknowledge_interrupt( ); 
    unblock_ user(); 
    return_from_interrupt( );

      本质上,DMA是由DMA控制器完成的程序控制I/O,一些需要完成的工作没有体现在上门的代码中,需要特殊的硬件来完成,从而解放了CPU。

      DMA往往比主CPU慢得多。如果DMA控制器不能完全发挥I/O的速度,或者CPU在等待DMA中断时没有别的事要做,程序控制I/O和中断驱动I/O可能比DMA更好。

      大多数情况下使用DMA是值得的。

     

    2.为何厂商宣传与实际能使用的磁盘容量不符?这和很多原因有关。(P377~378)

    • 一个容量为200*109B的磁盘,可能是按照200GB来进行销售的;
    • (低级)格式化时,磁盘上一部分内容作为前导码(preamble)、一部分作为纠错码(ECC)预留,其余才可供存放用户的数据,这部分容量可能只有170*109B;
    • 操作系统报告容量只有158GB,因为软件将存储器的1GB作为230(1,073,741,824)B而不是109B处理。

      值得注意的是,kilo、mega、giga、tera(也即K、M、G、T)只有在存储器和磁盘容量时才代表210、220、230、240

    课后习题选

    16.Compare RAID level 0 through 5 with respect to read performance, write per­ formance, space overhead, and reliability.

    译:

      比较0至5级RAID的读写性能、空间开销、可靠性。

    Answer:

      Read performance: RAID levels 0, 2, 3, 4, and 5 allow for parallel reads to service one read request. However, RAID level 1 further allows two read re-quests to simultaneously proceed. Write performance: All RAID levels provide similar write performance. Space overhead: There is no space overhead in level 0 and 100% overhead in level 1. With 32-bit data word and six parity drives, the space overhead is about 18.75% in level 2. For a 32-bit data word, the space overhead in level 3 is about 3.13%. Finally, assuming 33 drives in levels 4 and 5, the space overhead is 3.13% in them. Reliability: There is no reliability support in level 0. All other RAID levels can survive one disk crash. In addition, in levels 3, 4 and 5, a single random bit error in a word can be detected, while in level 2, a single random bit error in a word can be detected and corrected.

    分析:

      先补上图5-20便于观察。

        

      读效率,0、2、3、4、5级允许并行一次读取,而1级可以同步地进行两次读取;

      写效率,所有RAID写效率是相同的;

      空间开销:

        0级没有额外空间开销;

        1级使用100%(相较于有效数据)的开销;

        32位字并使用6位进行奇偶校验的2级产生了18.75%的开销;

        同样条件下3级使用了3.125%的开销。

        注意到习题14分析了3级为什么比2级开销小,原因是3级需要知道哪台设备出现了故障来进行恢复,而2级不需要,可参考P365;

        图中的4级和5级同3级一样都是3.125%,它们只是把单位由位变成了磁盘条带(strip)。

      可靠性:

        0级没有可靠性保证,发生崩溃时不能恢复数据。

        1级只要有一个拷贝没有出错就可以完全恢复。

        3、4、5级都可以检测一位错误;

        2级可以发现并纠正随机一位错误。

      

    勘误:

    1.习题1的“Fig.1-5”并不对应于答案。无论是第一章图1-5还是第五章前几个图,都不符合答案“ a controller with two devices”的描述。

    2.习题8的“Fig.1-6(a)”应为“Fig.1-7(a)”。注意到如果不是这种流水线(最后一个stage才是execute unit),答案可能不同。

    3.习题12的答案,网络传输时间部分我计算的是1024/(10*106/8)=0.8192msec,计算过程中没有舍入,而参考答案是0.83msec,怀疑有误。

    4.习题32,60行每行80个字符,总字符数是4800而非“5280 characters,total”。此题计算过程中的部分叙述似乎不对,我没有理解,看书的时候第二、三问没有作答。

  • 相关阅读:
    SpringBoot(十):SpringBoot的简单事务管理
    SpringBoot(九):SpringBoot集成Mybatis
    独立式智能扫码插座
    STC-51开发板-单片机控制数码管&按键&点阵综合操作
    单片机定时器与数码管静态显示
    半导体器件
    电路模型与规律
    单片机-引脚并行口结构讲解
    单片机-基础知识,存储原理,引脚简介———(第一个小程序)
    C语言-综合知识点
  • 原文地址:https://www.cnblogs.com/wuyuegb2312/p/3445899.html
Copyright © 2011-2022 走看看