code:readp - ikarishinjieva/unixV6-code-analyze-chs GitHub Wiki

Source

  • 从指定管道 读取数据
    • 若无数据可读,则睡眠等待
  • 共输入一个参数 fp

7755

7756 /* Read call directed to a pipe.

7757  */

7758 readp(fp)

7759 int *fp;

7760 {

7761     register *rp, *ip;

7762

7763     rp = fp;

7764     ip = rp->f_inode;

7765 loop:

7766     /* Very conservative locking.

7767      */

7768     plock(ip);

  • ip 置锁
7769     /* If the head (read) has caught up with

7770      * the tail (write), reset both to 0.

7771      */

7772     if(rp->f_offset[1] == ip->i_size1) {

  • 若 管道文件已读到底...
7773          if(rp->f_offset[1] != 0) {

7774           rp->f_offset[1] = 0;

7775           ip->i_size1 = 0;

7776           if(ip->i_mode&IWRITE) {

7777                ip->i_mode =& ~IWRITE;

7778                wakeup(ip+1);

7779           }

  • 清 管道文件偏移
  • 清 管道文件大小
  • 若 有进程 等待写此管道,则唤醒此进程
7780          }

7781

7782          /* If there are not both reader and

7783          * writer active, return without

7784          * satisfying read.

7785          */

7786          prele(ip);

  • 释放 ip
7787          if(ip->i_count < 2)

7788           return;

  • 若 写管道 不存在,则直接返回
7789          ip->i_mode =| IREAD;

7790          sleep(ip+2, PPIPE);

7791          goto loop;

7792     }

7793     /* Read and return

7794      */

7795     u.u_offset[0] = 0;

7796     u.u_offset[1] = rp->f_offset[1];

7797     readi(ip);

7798     rp->f_offset[1] = u.u_offset[1];

7799     prele(ip);

  • 置参数,并调用 readi 读管道文件
  • 更新 rp 参数
  • 解锁 ip
7800 }

7801 /* ------------------------- */

Ref

Caller

⚠️ **GitHub.com Fallback** ⚠️