1.支持库:
#include <linux/sched.h> //wake_up_process()
#include <linux/kthread.h>//kthread_create()、kthread_run()
2.
static struct task_struct *along_task = NULL; // 存储线程的信息
along_task = kthread_create(along_thread_t, NULL, "along_task"); // 创建线程,创建后线程是停止的,kthread_run()创建的线程是直接运行的。
3. 唤醒线程
wake_up_process(along_task);
4. 线程处理函数
static int along_thread_t(void *test)
{
while(1)
{
printk("---------------------> along along_thread 1 = %d \n", along_time);
set_current_state(TASK_INTERRUPTIBLE); // 设置线程状态,可以让出cpu。TASK_UNINTERRUPTIBLE状态的进程会忽略信号,而处于TASK_INTERRUPTIBLE状态的进程如果收到信号会被唤醒并处理信号(然后再次进入等待睡眠状态)
// if(kthread_should_stop()) return 0;
if(along_time == 9)
{
along_proc_info();
printk("---------------> along while return 2 ok\n");
// kthread_stop(along_task);
}
printk("-------------------------> along sleep 1 \n");
schedule(); //sleep,处理完线程,进入睡眠,让出cpu
printk("-------------------------> along sleep 2 \n");
}
return 0;
}