code:readp - ikarishinjieva/unixV6-code-analyze-chs GitHub Wiki
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);
7769 /* If the head (read) has caught up with
- ip 置锁
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 }
- 清 管道文件偏移
- 清 管道文件大小
- 若 有进程 等待写此管道,则唤醒此进程
- 睡眠原因 : (struct inode)+1
7781
7782 /* If there are not both reader and
7783 * writer active, return without
7784 * satisfying read.
7785 */
7786 prele(ip);
7787 if(ip->i_count < 2)
- 释放 ip
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);
7800 }
- 置参数,并调用 readi 读管道文件
- 更新 rp 参数
- 解锁 ip
7801 /* ------------------------- */