文档介绍:linux内核线程
函数说明:kthread_create:创建线程。struchread_run(threadfn, data, namefmt, ...)
函数说明:kthread_create:创建线程。struct task_struct *kthread_create(int (*threadfn)(void *data),void *data,const char *namefmt, ...);线程创建后,不会马上运行,而是需要将kthread_create() 返回的task_struct指针传给wake_up_process(),然后通过此函数运行线程。kthread_run :创建并启动线程的函数:struct task_struct *kthread_run(int (*threadfn)(void *data),void *data,const char *namefmt, ...);kthread_stop:通过发送信号给线程,使之退出。int kthread_stop(struct task_struct *thread);线程一旦启动起来后,会一直运行,除非该线程主动调用do_exit函数,或者其他的进程调用kthread_stop函数,结束线程的运行。但如果线程函数正在处理一个非常重要的任务,它不会被中断的。当然如果线程函数永远不返回并且不检查信号,它将永远都不会停止。代码:#include #include #ifndef SLEEP_MILLI_SEC#define SLEEP_MILLI_SEC(nMilliSec)\do { \long timeout = (nMilliSec) * HZ / 1010; \while(timeout > 0) \{ \timeout = schedule_timeout(timeout); \} \}while(0);#endifstatic struct task_struct * MyThread = NULL;static int MyPrintk(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(1010);printk("%s\n",mydata);}kfree(mydata);return 0;}static int __init init_kthread(void){MyThread = kthread_run(MyPrintk,"hello world","mythread");return 0;}static void __exit exit_kthread(void){if(MyThread){printk("stop MyThread\n");kthread_stop(MyThread)