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

Table of Contents

Source

  • 令当前进程进入睡眠,睡眠指定秒数
  • 共传入一个参数,设为t
  • t : 指定秒数,令进程睡眠t秒

5976 /* sleep system call

5977  * not to be confused with the sleep internal routine.

5978  */

5979 sslep()

5980 {

5981     char *d[2];

5982

5983     spl7();

5984     d[0] = time[0];

5985     d[1] = time[1];

5986     dpadd(d, u.u_ar0[R0]);

  • 令 d = time + t
  • d 为 睡眠到期时间
5987

5988     while(dpcmp(d[0], d[1], time[0], time[1]) > 0) {

5989          if(dpcmp(tout[0], tout[1], time[0], time[1]) <= 0 ||

5990           dpcmp(tout[0], tout[1], d[0], d[1]) > 0) {

5991           tout[0] = d[0];

5992           tout[1] = d[1];

5993          }

  • tout为所有调用sslep睡眠的进程中最早的到期时间,参考睡眠原因
  • 更新tout( tout = min(tout , d) )
5994          sleep(tout, PSLEP);
5995     }

5996     spl0();

5997 }

5998 /* ------------------------- */

5999

Extend

原理

  • 调用内核函数 sslep
    • 更新tout (tout为所有调用sleep睡眠的进程中最早的睡眠到期时间)
    • 令进程睡眠,睡眠原因tout
  • 时钟中断发生,调用clock函数
    • 判断是否有进程睡眠到期(tout == 当前系统时间)
    • 若有,则唤醒所有睡眠在tout上的进程
  • 进程被唤醒,继续执行sslep
    • 若睡眠到期,则函数返回
    • 若睡眠未到期,则再度睡眠,睡眠原因tout

Param

(sleep = 35.; not in assembler)
(seconds in r0)
sys sleep
⚠️ **GitHub.com Fallback** ⚠️