创建内核线程的函数:
thread = kthread_create(taskq_thread, tqt, "%s/%d", name, i);
wake_up_process(thread);
thread = kthread_create(taskq_thread, tqt, "%s/%d", name, i);
wake_up_process(thread);
其中taskq_thread为该内核函数需要做的事情。
所需要包含头文件
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/err.h>
For Example:
resume complete时usb driver会调用do_rebind_interface() 来实现device的重新枚举。这会拖慢系统resume的速度,因此create一个线程来单独完成这件事情。
static int USBResumeSpeed_Improve(void *pvArg) // 函数返回值必须为int型 { struct usb_device *udev = pvArg; do_rebind_interface(udev); return 0; } /* The device lock is held by the PM core */ int usb_resume_complete(struct device *dev) { struct usb_device *udev = to_usb_device(dev); int err; struct task_struct *ResumeThreadStruct; /* For PM complete calls, all we do is rebind interfaces * whose needs_binding flag is set */ if (udev->state != USB_STATE_NOTATTACHED) { if(udev->descriptor.idProduce == 0x7662 ){ char ResumeThreadName[10] = "RES_WIFI"; ResumeThreadStruct = kthread_create(USBResumeSpeed_Improve, udev, ResumeThreadName); if(IS_ERR(ResumeThreadStruct)) { err = PTR_ERR(ResumeThreadStruct); ResumeThreadStruct = NULL; return err; } wake_up_process(ResumeThreadStruct); } else{ do_rebind_interfaces(udev); } } return 0; }