Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Do you know how to terminate Kernel Thread?

2,364 views
Skip to first unread message

demiahn

unread,
May 20, 2009, 6:56:58 AM5/20/09
to
I created Thread with kernel_thread function.
But I don't know how to terminate this Thread.

Do you know how to terminate Kernel Thread?


Gil Hamilton

unread,
May 20, 2009, 8:06:19 AM5/20/09
to
"demiahn" <ahnda...@gmail.com> wrote in news:gv0nlq$695$1
@news.kreonet.re.kr:

> I created Thread with kernel_thread function.
> But I don't know how to terminate this Thread.
>
> Do you know how to terminate Kernel Thread?

You don't terminate a kernel thread, at least not externally. You *ask*
it to quit and it then calls do_exit() (or it can simply return an exit
status, which causes do_exit to be called on its behalf).

BTW, calling kernel_thread directly is deprecated. The kthread_*
functions provide a cleaner interface. The most straightforward
equivalent is to call kthread_run(). The resulting kernel thread then
typically executes in a loop like this:

while (!kthread_should_stop())
{
do_some_work();
...
schedule(); /* Block waiting for more work */
}
/* Caller asked us to stop. Return exit status */
return 0;


Then an external (kernel-mode) caller can execute kthread_stop() to ask
the thread to exit.

GH

demiahn

unread,
May 20, 2009, 9:50:34 PM5/20/09
to
Hi Gil,

My Kernel Thread is waiting some event.
while (1)
{
wait_event_interruptible();

do_some_work();
}

So, kthread_stop is will not terminated my thread.

How about using kill_proc function?
Isn't it possible to kill it?

"Gil Hamilton" <gil_ha...@hotmail.com> wrote in message
news:Xns9C11527403DF0gi...@85.214.105.209...

Rene

unread,
May 21, 2009, 6:06:01 AM5/21/09
to
demiahn schreef:

Please reply at the bottom.

Can't You use some semaphore that is checked in the while loop (so not
(while(1) but while (somesignal)), that You can set from somewhere else?
That way You can set it to untrue and the while loop will not be
executed anymore and the thread will terminate in the normal way.

Good luck!
Rene

Gil Hamilton

unread,
May 21, 2009, 9:35:47 AM5/21/09
to
"demiahn" <ahnda...@gmail.com> wrote in news:gv2c1a$ti6$1
@news.kreonet.re.kr:

> My Kernel Thread is waiting some event.
> while (1)
> {
> wait_event_interruptible();
>
> do_some_work();
> }
>
> So, kthread_stop is will not terminated my thread.
>
> How about using kill_proc function?
> Isn't it possible to kill it?

It may well be possible to do so but killing a kernel thread is wrong
because kernel threads are supposed to be controlled by code running
entirely within the kernel. If you do not block signals (as kernel
threads are expected to do), then there is nothing to prevent a user
from sending your thread a signal from the command line. But a kernel
thread starts and stops based on its own timetable, not that of a user.
If a user accidentally kills the kernel thread, how would he then
restart it?

wait_event_interruptible takes a 'condition' as one of its arguments.
So, if your current code is:
wait_event_interruptible(wq, is_more_work_to_do())

simply replace that with:
wait_event(wq, is_more_work_to_do() || kthread_should_stop())
if (kthread_should_stop())
break;

Now your thread can be started with kthread_run and stopped with
kthread_stop.

GH

0 new messages