2008-10-15 14:50:57

by Peter Teoh

[permalink] [raw]
Subject: Problem with set_current_state() and schedule()

I am puzzled over this:

The module just consist of three function:

static int myfunction(void)
{
dbg("%s - enter....", __FUNCTION__);

while (!kthread_should_stop()) {
dbg("%s - executing....", __FUNCTION__);
set_current_state(TASK_INTERRUPTIBLE);
printk("sssssssssssssssssssssssssssssssss\n");


schedule(); ======> ////// removed for part 2
printk("sssssssssssssssssssssssssssssssss\n");======>//////
removed for part 2


msleep(1000);
schedule();
}
return 0;
dbg("%s exit", __FUNCTION__);
}

int init_module(void)
{

g_task = kthread_run(myfunction, NULL, "mykthread");

return 0;
}

void cleanup_module(void)
{
printk(KERN_INFO "Goodbye mykthread.\n");
kthread_stop(g_task);
}

Below is the dmesg out .... noticed two parts (based on timing output):

Part 1:
[ 1160.348000] mykthread: myfunction - enter....
[ 1160.348000] mykthread: myfunction - executing....
[ 1160.348000] sssssssssssssssssssssssssssssssss
[ 1208.632000] Goodbye mykthread.=======> this is because I "rmmod",
otherwise it got stuck in the previous statement.

Part 2:
[ 1208.632000] sssssssssssssssssssssssssssssssss
[ 1225.612000] mykthread: myfunction - enter....
[ 1225.612000] mykthread: myfunction - executing....
[ 1225.612000] sssssssssssssssssssssssssssssssss
[ 1226.616000] mykthread: myfunction - executing....
[ 1226.616000] sssssssssssssssssssssssssssssssss
[ 1227.620000] mykthread: myfunction - executing....
[ 1227.620000] sssssssssssssssssssssssssssssssss
[ 1228.624000] mykthread: myfunction - executing....
[ 1228.624000] sssssssssssssssssssssssssssssssss
[ 1229.628000] mykthread: myfunction - executing....
[ 1229.628000] sssssssssssssssssssssssssssssssss
[ 1230.632000] mykthread: myfunction - executing....
[ 1230.632000] sssssssssssssssssssssssssssssssss
[ 1231.636000] mykthread: myfunction - executing....
[ 1231.636000] sssssssssssssssssssssssssssssssss
[ 1232.640000] mykthread: myfunction - executing....
[ 1232.640000] sssssssssssssssssssssssssssssssss
[ 1233.644000] mykthread: myfunction - executing....
[ 1233.644000] sssssssssssssssssssssssssssssssss
[ 1234.648000] mykthread: myfunction - executing....
[ 1234.648000] sssssssssssssssssssssssssssssssss
[ 1235.652000] mykthread: myfunction - executing....
[ 1235.652000] sssssssssssssssssssssssssssssssss
[ 1236.656000] mykthread: myfunction - executing....
[ 1236.656000] sssssssssssssssssssssssssssssssss
[ 1237.660000] mykthread: myfunction - executing....
[ 1237.660000] sssssssssssssssssssssssssssssssss
[ 1238.664000] mykthread: myfunction - executing....
[ 1238.664000] sssssssssssssssssssssssssssssssss
[ 1239.668000] mykthread: myfunction - executing....
[ 1239.668000] sssssssssssssssssssssssssssssssss
[ 1240.672000] mykthread: myfunction - executing....
[ 1240.672000] sssssssssssssssssssssssssssssssss
[ 1241.676000] mykthread: myfunction - executing....
[ 1241.676000] sssssssssssssssssssssssssssssssss
[ 1242.680000] mykthread: myfunction - executing....
[ 1242.680000] sssssssssssssssssssssssssssssssss

For part 1:

After printing one line, the program never seemed to continue anymore.
why? It got stuck in the first schedule().

For part 2:

Just remove the "remove for part 2". And u can immediately see that
it starts to loop forever. So effectively there is only one
schedule() in the while loop.

Why?

I am quite puzzled......thanks in advance for the help. :-).

--
Regards,
Peter Teoh


2008-10-15 15:16:33

by Gilad Ben-Yossef

[permalink] [raw]
Subject: Re: Problem with set_current_state() and schedule()

Peter Teoh wrote:

> I am puzzled over this:
>
> The module just consist of three function:
>
> static int myfunction(void)
> {
> dbg("%s - enter....", __FUNCTION__);
>
> while (!kthread_should_stop()) {
> dbg("%s - executing....", __FUNCTION__);
> set_current_state(TASK_INTERRUPTIBLE);
> printk("sssssssssssssssssssssssssssssssss\n");
>
>
> schedule(); ======> ////// removed for part 2
> printk("sssssssssssssssssssssssssssssssss\n");======>//////
> removed for part 2
>
>
> msleep(1000);
> schedule();
> }
> return 0;
> dbg("%s exit", __FUNCTION__);
> ...
>
> For part 1:
>
> After printing one line, the program never seemed to continue anymore.
> why? It got stuck in the first schedule().
>
You set the state of your task to TASK_INTERRUPTIBLE, that is, not valid
to run and then call scheduler to give control to some other task. You
don't include any code that actually sets the task state back to
TASK_RUNNING, that is legible for scheduling so the schedule will never
enter your task again. It effectively sleeps for ever.

You either need to use schedule_timeout with some delay or have some
other means to set your task state back to TASK_RUNNING or better yet,
use wait_event and friends and don't call schedule() directly.

> For part 2:
>
> Just remove the "remove for part 2". And u can immediately see that
> it starts to loop forever. So effectively there is only one
> schedule() in the while loop.
>

> Why?
>
Thats' because msleep() changes your task state to TASK_RUNNING when it
finishes sleeping, so you're calling the scheduler with a TASK_RUNNING
state and it will schedule your task back sometime (perhaps even
immediately).
> I am quite puzzled......thanks in advance for the help. :-).
>
>
Go read chapter 7 of "Linux Device Drivers, Third Edition" :-)

Gilad


--
Gilad Ben-Yossef
Chief Coffee Drinker

Codefidence Ltd.
The code is free, your time isn't.(TM)

Web: http://codefidence.com
Email: [email protected]
Office: +972-8-9316883 ext. 201
Fax: +972-8-9316885
Mobile: +972-52-8260388

The Doctor: Don't worry, Reinette, just a nightmare.
Everyone has nightmares. Even monsters from under the
bed have nightmares, don't you, monster?
Reinette: What do monsters have nightmares about?
The Doctor: Me!