user23683193
user23683193

Reputation: 11

Why there is a state called `TASK_UNINTERRUPTIBLE` in Linux kernel?

Many programs in the Linux kernel follow this pattern:

for (;;) {
    set_current_state(TASK_UNINTERRUPTIBLE);
    if (CONDITION)
    break;
    schedule();
}
__set_current_state(TASK_RUNNING);

I'm wondering if here we don't set the state to TASK_UNINTERRUPTIBLE and just go to sleep, then if we are woken up by a signal then the condition if (CONDITION) won't meet, so we will go to sleep again by calling schedule() and that doesn't do any harm to the correctness, so why do we bother to set the state to TASK_UNINTERRUPTIBLE?

Another question is, if we enable PREEMPT then the linux kernel become a preemptive kernel so at any place when we are handling a syscall in the kernel mode, it may be preempted so why we just use set_current_state(TASK_UNINTERRUPTIBLE) to protect when we go to sleep actively by calling schedule()?

Upvotes: 1

Views: 50

Answers (0)

Related Questions