LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

Reply
 
Search this Thread
Old 08-21-2009, 02:50 AM   #1
rkemiset
LQ Newbie
 
Registered: Aug 2009
Posts: 6

Rep: Reputation: 0
kthread_stop on a already completed thread


hi,

i am facing one issue in using kthread_stop().

basically i have created one kernel thread using kthread_create(), that threadfn contains small operations which may not take much time.

Now the problem is, i am just using kthread_stop() after creation and wakeup process of the threadfn, by the time of calling kthread_stop() that threadfn already completed its job. now the control is not coming back from this kthread_stop().

how to overcome this problem... and also if i give an invalid pointer to kthread_stop() what will be the behavior and how to overcome it???

thanks,
rajesh
 
Old 08-25-2009, 05:23 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
It is not good practice to initiate the end of a thread from more than one context (any type of thread).

You have to decide where the responsibility for stopping the thread lies. If it is within the thread, then it will call do_exit. If it is external to the thread, then the thread will poll kthread_should_stop() until it is true and return.

In other words, you should not call kthread_stop() if the thread itself will exit.

If you just want to wait at the end until the kthread_stop has been called, you might do something like this:

Code:
set_current_state(TASK_INTERRUPTIBLE);
while (!kthread_should_stop())
{
   schedule();
   set_current_state(TASK_INTERRUPTIBLE);
}
set_current_state(TASK_RUNNING);
return 0;
 
Old 04-16-2012, 11:50 AM   #3
Shahbaz Youssefi
LQ Newbie
 
Registered: Apr 2012
Posts: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by neonsignal View Post
If it is within the thread, then it will call do_exit.
Can you please tell me how to then clean up the thread? I use
Code:
do_exit
to terminate the thread (from the thread itself), but I believe the thread becomes a zombie. How do you clean the thread up?
 
Old 04-16-2012, 06:27 PM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Best to start a new forum thread which will be seen by more people, as this is an old one.

I don't know the answer to your question, and you may need to post more information about your code on the new forum thread.

Incidentally, it is normal for a thread to become a zombie immediately after it exits; the issue is more likely to be something to do with the parent thread causing a problem with the cleanup.
 
Old 04-17-2012, 08:46 AM   #5
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,366

Rep: Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106
If you have something that needs to be dealt with by a kernel thread, then most likely that thread would be persistent: once started, it would never end. (It might be sleeping.) But, if the thread is a "one-shot," it should in any case be fully the master of its own affairs: it does its work, then it, gracefully and on its own terms, dies. Don't point a pistol at its head. (This principle is IMHO pretty much true for any thread-oriented design, kernel or otherwise. There really is no way to close the timing-holes that otherwise would occur, and those timing holes would cause "random" misbehavior ... for example, in the middle of a very important sales presentation, even after a gaggle of dedicated programmers had been testing it for days.)

Last edited by sundialsvcs; 04-17-2012 at 08:48 AM.
 
Old 04-18-2012, 02:48 PM   #6
Shahbaz Youssefi
LQ Newbie
 
Registered: Apr 2012
Posts: 5

Rep: Reputation: Disabled
@sundialsvcs, The threads start upon initialization of the module and die when the kernel module is removed. They are persistent, but eventually every thread has to die. I am not pointing a pistol at it at all! The thread terminates, no problem.

The thing is, if the thread becomes a zombie, that means the parent needs to tell the OS it doesn't need it anymore, so it would be cleaned up. In pthreads, this is by pthread_join. I wanted to see if with kthreads, there is an equivalent function or not, or if its even needed.
 
Old 04-18-2012, 03:44 PM   #7
rainbowsally
Member
 
Registered: Oct 2011
Posts: 47
Blog Entries: 125

Rep: Reputation: Disabled
Maybe have it print its pid, maybe even to a file for debugging.

It doesn't sound like you're sure it's left there as a zombie or not, but there is no 'parent' for it to signal so you may not even have a problem?

You may even be able to do a pgrep to see what is in fact running by name.

Best of luck. :-)
 
Old 04-19-2012, 07:24 PM   #8
Shahbaz Youssefi
LQ Newbie
 
Registered: Apr 2012
Posts: 5

Rep: Reputation: Disabled
@rainbowsally, indeed, I am not sure if the threads become zombies after do_exit or not. I was hoping you could clear it for me.

Well, the thread calling module_init creates the thread, so it's its parent. When it finishes, I guess init will become the threads parent. I believe there is always a parent for the thread. Since it is init, though, there shouldn't be a problem, because init periodically cleans up its terminated children.

Ok, now that I have said it out loud, I understand that there is no need for cleanup. Thread of clean_up is not the thread's parent so it can't cleanup anyway. Is that true?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 11:28 AM
Completed the switch... gizmogadgetus LinuxQuestions.org Member Success Stories 5 12-22-2005 12:26 AM
Completed LFS but ...... chakkaradeepcc Linux From Scratch 2 05-15-2005 12:34 AM
lfs 5.0 completed miguetoo Linux From Scratch 1 11-26-2003 11:00 AM
Migration Completed jeremy LQ Suggestions & Feedback 2 11-03-2003 12:23 AM


All times are GMT -5. The time now is 04:19 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration