How to wait for a linux kernel thread (kthread)to exit?

后端 未结 4 1923
再見小時候
再見小時候 2021-01-03 07:36

I have just started programing on Linux kernel threads. I have a problem which I would like to share with you guys. My code is:



        
相关标签:
4条回答
  • 2021-01-03 08:00
    /* Wait for kthread_stop */
    set_current_state(TASK_INTERRUPTIBLE);
    while (!kthread_should_stop()) {
        schedule();
        set_current_state(TASK_INTERRUPTIBLE);
    }
    

    Check this article for more information: "Sleeping in the Kernel".

    0 讨论(0)
  • 2021-01-03 08:05

    It depends what you're doing, but you may not even want to start your own kernel threads.

    You can submit a job to be run on the kernel's global workqueue using schedule_work().

    If you do use your own thread you typically write it as a loop around kthread_should_stop(). Then the code which wants the thread to terminate calls kthread_stop(), which tells the thread to stop and then waits for it to stop.

    0 讨论(0)
  • 2021-01-03 08:16

    YOu can also go for completions. The linux mass storage driver http://lxr.free-electrons.com/source/drivers/usb/storage/usb.c has a very good implementation of kthreads. Good Luck.

    0 讨论(0)
  • 2021-01-03 08:22

    Read about wait queues. This is a good place to look at.

    Essentially, you need to keep track of how many threads are running (lock protected counter), and put the creating thread on a wait queue, until all threads are done. The last thread finishing its job can wake up the thread on the wait queue, so that it can repeat the process with another pair of threads.

    0 讨论(0)
提交回复
热议问题