Do you know how to terminate Kernel Thread?

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?

Reply to
demiahn
Loading thread data ...

"demiahn" wrote in news:gv0nlq$695$1 @news.kreonet.re.kr:

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

Reply to
Gil Hamilton

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?

Reply to
demiahn

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

Reply to
Rene

"demiahn" wrote in news:gv2c1a$ti6$1 @news.kreonet.re.kr:

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

*ask*

exit

then

ask

Reply to
Gil Hamilton

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.