進程是一個動態的實體,滿足條件的情況下,他一直在執行,但是有時候,進程需要條件得不到滿足的時候,他就會被掛起。但這是被動的,不是進程控制的,也就是說,進程訪問一個資源的時候,如果不能被滿足,進程會被系統掛起,等到條件滿足的時候,系統會喚起進程。
今天介紹的是一種進程主動睡眠的能力。即進程自己讓自己掛起,等到一定時間後,被系統喚醒(時間到或者收到信號)。這個能力由sleep函數提供。
unsigned int sleep(unsigned int seconds);
這個函數可以讓進程自己掛起seconds秒。我們看看這個函數的一些說明。
On Linux, sleep() is implemented via nanosleep(2). See the
nanosleep(2) man page for a discussion of the clock used.
即sleep函數是由操作系統的nanosleep函數實現的。我們看一下核心代碼。
asmlinkage long sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
{
struct timespec t;
unsigned long expire;
long ret;
expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
current->state = TASK_INTERRUPTIBLE;
expire = schedule_timeout(expire);
}
算出超時時間,然後掛起進程(可中斷掛起),然後調用schedule_timeout。
fastcall signed long __sched schedule_timeout(signed long timeout)
{
struct timer_list timer;
unsigned long expire;
// 算出超時時間
expire = timeout + jiffies;
init_timer(&timer);
// 超時時間
timer.expires = expire;
timer.data = (unsigned long) current;
// 超時回調
timer.function = process_timeout;
// 添加定時器
add_timer(&timer);
// 進程調度
schedule();
// 刪除定時器
del_singleshot_timer_sync(&timer);
// 超時或者被信號喚醒,被信號喚醒的話,可能還沒有超時
timeout = expire - jiffies;
out:
return timeout < 0 ? 0 : timeout;
}
接着往系統新增一個定時器,然後發送進程調度,該進程隨即進入掛起狀態。等到一定的時間後,進程會喚醒。另外我們注意到掛起的進程狀態是TASK_INTERRUPTIBLE,即可中斷的。意思是這種狀態的進程可以被信號喚醒。而TASK_UNINTERRUPTIBLE是不能被信號喚醒的。
等到超時的時候,執行process_timeout函數。
static void process_timeout(unsigned long __data)
{
wake_up_process((task_t *)__data);
}
代碼很簡單,就是喚醒被掛起的進程。__data是在
timer.data = (unsigned long) current;
中設置的。這就是進程主動睡眠(sleep)的大致原理。