public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Gilad Ben-Yossef <gilad@codefidence.com>
To: Peter Teoh <htmldeveloper@gmail.com>
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: Re: Problem with set_current_state() and schedule()
Date: Wed, 15 Oct 2008 17:16:18 +0200	[thread overview]
Message-ID: <48F60942.8040209@codefidence.com> (raw)
In-Reply-To: <804dabb00810150750sb54377dhc83841dedee0f72b@mail.gmail.com>

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:  gilad@codefidence.com
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! 


      reply	other threads:[~2008-10-15 15:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-15 14:50 Problem with set_current_state() and schedule() Peter Teoh
2008-10-15 15:16 ` Gilad Ben-Yossef [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=48F60942.8040209@codefidence.com \
    --to=gilad@codefidence.com \
    --cc=htmldeveloper@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox