Related functions:
Kthread_create (): Creating Kernel threads
Copy Code code as follows:
struct task_struct *kthread_create (int (*THREADFN) (void *data), void *data, const char namefmt[], ...); Kernel thread can be created with Kernel_thread, but in the execution function it is necessary to release the resource with Daemonize and hang it under INIT, and completion wait for the process to complete. To simplify the operation, the kthread_create is defined.
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.
Copy Code code as follows:
struct task_struct *kthread_run (int (*THREADFN) (void *data), void *data,const char *namefmt, ...); It's actually a macro, made up of kthread_create () and wake_up_process ().
It's actually a macro, made up of kthread_create () and wake_up_process ().
Copy Code code as follows:
#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 (): Sends a signal to the thread to make it exit.
Copy Code code as follows:
int kthread_stop (struct task_struct *thread);
Once the thread is started, it runs until the thread is active calling the Do_exit function, or another process calls the Kthread_stop function to end the thread's running.
But if the thread function is working on a very important task, it will not 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 invoked, the thread function cannot be finished running. Otherwise, the Kthread_stop function waits all the time.
General framework for Kernel threads
int threadfunc (void *data) {
...
while (1) {
Set_current_state (task_uninterruptible);
if (Kthread_should_stop ()) break;
if () {//condition is true
Conduct business process
}
else{//condition is False
Let the CPU run other threads and reschedule within a specified time
Schedule_timeout (HZ);
}
}
...
return 0;
}
Thread-related test commands
You can use the top command to view the CPU utilization of threads, including kernel threads. The order is as follows:
Top–p Thread Number
You can use the following command to find the thread number:
PS Aux|grep Thread Name
Sample programs: Use modules to load kernel threads, which enable each 1s to print characters in the kernel.
(makefile omitted, the same writing as a previous blog.) )
Copy Code code as follows:
#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, ' I ', strlen (data) +1);
strncpy (Mydata,data,strlen (data));
while (!kthread_should_stop ())
{
SLEEP_MILLI_SEC (1000);
PRINTK ("%s\n", 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, ' I ', strlen (data) +1);
strncpy (Mydata,data,strlen (data));
while (!kthread_should_stop ())
{
sleep_milli_sec (1000);
printk ("%s\n", 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");
Supplementary Note:
This delay macro can in some cases cause a kernel-thread CPU usage to be too high. According to the Schedule_timeout () source analysis, it is only a cycle to make the thread become task_running state, this thread does not really sleep. Workaround: Add set_current_state (task_interruptible) at the beginning of the while loop.