Linux Kernel Development -- kernel thread

Source: Internet
Author: User

The kernel often needs to execute some operations in the background. This task can be completed through the kernel thread-a standard process that runs independently in the kernel space. The difference between a kernel thread and a common process is that the kernel thread does not have an independent address space and the MM pointer is set to null. It runs only in the kernel space and never switches to the user space; like normal processes, they can be scheduled or preemptible.

In fact, the kernel thread can only be created by other kernel threads. In the existing kernel thread, create a new kernel thread method:

1. kernel_thread

Int kernel_thread (INT (* fN) (void *), void * Arg, unsigned long flags );

Kernel_thread is implemented through do_fork: do_fork (flags | clone_vm | clone_untraced, 0, & regs, 0, null, null ).

Note that you must use daemonize to release resources and mount them to init in the FN execution function. You also need to use completion to wait for the completion of this process.

2. kthread_run

Struct task_struct * kthread_run (INT (* threadfn) (void * data), void * data, const char * namefmt ,...);

Create and start the kernel thread.

3. kthread_create

Struct task_struct * kthread_create (INT (* threadfn) (void * data), void * data, const char namefmt [],...);

Int wake_up_process (struct task_struct * P );

Kthread_create creates a new kernel thread, but the thread is stopped. Use wake_up_process to start it.

Example

 1 # Include <Linux/kthread. h> 2 # Include <Linux/module. h> 3   4   # Ifndef sleep_milli_sec  5   # Define Sleep_milli_sec (nmillisec )\ 6       Do {\  7       Long Timeout = (nmillisec) * Hz/ 1000  ;\  8       While (Timeout> 0  )\  9   {\  10   _ Set_current_state (task_interruptible );\  11 Timeout = Schedule_timeout (timeout );\ 12   }\  13 } While ( 0  );  14   # Endif  15   16   Static   Struct Task_struct * mythread = NULL;  17   18   Static  Int Myprintk ( Void * Data)  19   {  20       Char * Mydata = kmalloc (strlen (data) + 1  , Gfp_kernel );  21 Memset (mydata, '  \ 0  ' , Strlen (data) + 1  ); 22   Strncpy (mydata, Data, strlen (data ));  23       While (! Kthread_should_stop ())  24   {  25 Sleep_milli_sec ( 1000  );  26 Printk ( "  % S \ n  "  , Mydata ); 27   }  28   Kfree (mydata );  29       Return   0  ;  30   }  31   32   Static   Int _ Init init_kthread ( Void  )  33  {  34 Mythread = kthread_run (myprintk, "  Hello World  " , "  Mythread  "  );  35       Return   0  ;  36   }  37   38  Static   Void _ Exit exit_kthread ( Void  )  39   {  40       If  (Mythread)  41   {  42 Printk ( "  Stop mythread \ n  "  );  43  Kthread_stop (mythread );  44   }  45   }  46   47   Module_init (init_kthread );  48   Module_exit (exit_kthread );  49 Module_license ( "  Dual BSD/GPL  " );

Refer:

http://blog.chinaunix.net/uid-20196318-id-136979.html http://blog.csdn.net/unbutun/article/details/4528407 http://blog.csdn.net/flyingcloud_2008/article/details/5857464

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.