Linux驱动

1、头文件

#include <linux/kthread.h>

2、定义变量

static pid_t thread_id;    //线程ID
static struct completion th_exit;    //同步信号量

3、初始化

init_completion(&th_exit);

4、建立线程

thread_id = kernel_thread(fuction, NULL, CLONE_FS | CLONE_FILES);

6、线程函数实体

int fuction0(void *arg) 
{     
//	daemonize("mythread");  //释放资源? 这句会导致线程执行完毕却不退出。类似于僵尸线程
	allow_signal(SIGKILL);  //定义接受信号
	while (!signal_pending (current))   //返回不为0表示有信号处理
	{
		silabs_device_start(0);    //任务函数
		schedule();            //线程休眠,调度其他线程
	}
	complete_and_exit(&th_exit0, 1);    //接收到信号量,退出线程
}

7、任务函数和信号量

silabs_device_start
{
  .................................
  .................................
  kill_proc (thread_id, SIGTERM, 1);  //杀掉线程
  wait_for_completion(&th_exit);    //激活信号量
}

  

原文地址:https://www.cnblogs.com/FarmPick/p/5161037.html