Supplementary instructions on the creation use and exit of kernel threads and on latency macros

  • 2020-04-01 21:39:39
  • OfStack

Related functions:

Kthread_create (): creates a kernel thread


struct task_struct *kthread_create(int (*threadfn)(void *data), void *data, const char namefmt[], ...);  kernel thread You can use kernel_thread Create, but must be used in execution functions daemonize Release the resource and hook it up init Now, we still need to use it completion Wait for this process to be completed. To simplify the operation, it is defined kthread_create . 

Instead of running immediately after the thread is created, you need to pass the task_struct pointer returned by kthread_create() to wake_up_process() and then run the thread through this function.
 

Kthread_run () : a function that creates and starts a thread.


struct task_struct *kthread_run(int (*threadfn)(void *data),void *data,const char *namefmt, ...); It's actually a macro kthread_create() and wake_up_process() Composition.  

It's actually a macro, consisting of kthread_create() and wake_up_process().

#define kthread_run(threadfn, data, namefmt, ...)                     /
({                                                            /
    struct task_struct *__k                                        /
           = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); /
    if (!IS_ERR(__k))                                        /
           wake_up_process(__k);                                /
    __k;                                                     /
})

Kthread_stop () : exits the thread by sending a signal to it.

int kthread_stop(struct task_struct *thread);

Once a thread is started, it continues to run, unless the thread actively calls the do_exit function, or another process calls the kthread_stop function to terminate the thread.
But if the thread function is working on a very important task, it won't be interrupted. Of course, if the thread function never returns and does not check the signal, it will never stop.

Also, when the kthread_stop function is called, the thread function cannot have finished running. Otherwise, the kthread_stop function will keep waiting.

 

General framework for kernel threads

{int threadfunc (void * data)

              ...

              While (1) {

                            Set_current_state (TASK_UNINTERRUPTIBLE);

                            Break the if (kthread_should_stop ());

                            If (){// condition true

                                          // conduct business processing

                            }

                            Else {// condition false

                                          // allows the CPU to run other threads and reschedule them for a specified time

                                          Schedule_timeout (HZ);

                            }

              }

              ...

              Return 0;

}

 

Thread-related test commands

You can use the top command to see the CPU utilization of threads (including kernel threads). The command is as follows:

Top: thread number

You can use the following command to find the thread number:

Ps aux|grep thread name

 

Example program: load kernel thread with module, and print characters in kernel every 1s.

(the makefile was omitted and written the same way as the previous post.)


#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>   //wake_up_process()
#include <linux/kthread.h> //kthread_create(),kthread_run()
#include <linux/err.h> //IS_ERR(),PTR_ERR()
#ifndef SLEEP_MILLI_SEC
#define SLEEP_MILLI_SEC(nMilliSec)
do { 
long timeout = (nMilliSec) * HZ / 1000; 
while(timeout > 0) 
{ 
timeout = schedule_timeout(timeout); 
} 
}while(0);
#endif
static struct task_struct *my_task = NULL;
static int my_kthread(void *data)  
{  
    char *mydata = kmalloc(strlen(data)+1,GFP_KERNEL);
    memset(mydata,'0',strlen(data)+1);
    strncpy(mydata,data,strlen(data));
    while(!kthread_should_stop())
    {
        SLEEP_MILLI_SEC(1000);
        printk("%sn",mydata);
    }
    kfree(mydata);
    return 0;
}  
static int __init kernel_thread_init(void)
{
    int err;
    printk(KERN_ALERT "Kernel thread initalizing...n");
    my_task = kthread_create(my_kthread,"hello world","mythread");
    if(IS_ERR(my_task)){
        printk("Unable to start kernel thread./n");
        err = PTR_ERR(my_task);
        my_task = NULL;
        return err;
    }
    wake_up_process(my_task);
    return 0;#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>   //wake_up_process()
#include <linux/kthread.h> //kthread_create(),kthread_run()
#include <linux/err.h> //IS_ERR(),PTR_ERR()
#ifndef SLEEP_MILLI_SEC
#define SLEEP_MILLI_SEC(nMilliSec)
do { 
long timeout = (nMilliSec) * HZ / 1000; 
while(timeout > 0) 
{ 
timeout = schedule_timeout(timeout); 
} 
}while(0);
#endif
static struct task_struct *my_task = NULL;
static int my_kthread(void *data)  
{  
    char *mydata = kmalloc(strlen(data)+1,GFP_KERNEL);
    memset(mydata,'0',strlen(data)+1);
    strncpy(mydata,data,strlen(data));
    while(!kthread_should_stop())
    {
        SLEEP_MILLI_SEC(1000);
        printk("%sn",mydata);
    }
    kfree(mydata);
    return 0;
}  
static int __init kernel_thread_init(void)
{
    int err;
    printk(KERN_ALERT "Kernel thread initalizing...n");
    my_task = kthread_create(my_kthread,"hello world","mythread");
    if(IS_ERR(my_task)){
        printk("Unable to start kernel thread./n");
        err = PTR_ERR(my_task);
        my_task = NULL;
        return err;
}
static void __exit kernel_thread_exit(void)
{
    if(my_task){
        printk(KERN_ALERT "Cancel this kernel thread.n");
        kthread_stop(my_task);
        printk(KERN_ALERT "Canceled.n");
        }
}

module_init(kernel_thread_init);
module_exit(kernel_thread_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("anonymous");

Additional notes:

This delay macro can cause excessive CPU usage in kernel threads in some cases. According to an analysis of the schedule_timeout() source, it just cycles the thread into the TASK_RUNNING state, and the thread doesn't really sleep. Solution: add set_current_state(TASK_INTERRUPTIBLE) to the beginning of the while loop.


Related articles: