Kernel thread scheduling

Anand Moon moon.linux at yahoo.com
Sat Mar 21 02:33:31 EDT 2015


Hi Jeff,

kernel_thread just like daemon process. These are self monitoring process.


These process get scheduled by the kernel when their is any state change
from TASK_INTERRUPTIBLE to TASK_RUNNING or their is some condition that get full field.

If you don't schedule() the kernel thread state is not get updated by the scheduler.


Below example can help clear the concept.


http://lxr.free-electrons.com/source/drivers/vhost/vhost.c#L225


-Anand Moon


On Saturday, March 21, 2015 4:58 AM, Jeff Haran <Jeff.Haran at citrix.com> wrote:
-----Original Message-----
From: kernelnewbies-bounces at kernelnewbies.org [mailto:kernelnewbies-bounces at kernelnewbies.org] On Behalf Of Vincenzo Scotti
Sent: Friday, March 20, 2015 4:20 PM
To: kernelnewbies at kernelnewbies.org
Subject: Kernel thread scheduling

Hello,
I am actually studying kernel threads, and I have some doubts about them.
Let's take for example this snippet of code

static int thread_function(void *data)
{
    while (!kthread_should_stop()) {
        schedule();
    }

    pr_err("Stopped");
    return 0;
}

This way it works just fine, and waits until I call kthread_stop on it.
But if I comment out that schedule() call, it just hangs my system when I load it (it is part of a module). I see that the loop-schedule-wakeup pattern is used among all the others kernel threads. But I don't get why I need to call the scheduler explicitly.
I know that the kernel is fully preemptible, and in my interpretation I thought that it could stop every running thread, even in kernel space, using a timer-based interrupt handler, to give cpu to other threads. Doesn't this pattern resemble a voluntary preemption model?

Where am I wrong?

Are you sure your kernel is configured with kernel preemption on? It is a configurable option. Grep for PREEMPT in your .config file.

Jeff


_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies



More information about the Kernelnewbies mailing list