📄 thread_drv.c
字号:
//#include <linux/config.h>#include <linux/version.h>#include <linux/init.h>#include <linux/list.h>#include <linux/module.h>#if defined(MODVERSIONS)#include <linux/modversions.h>#endif#include <linux/kernel.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/sched.h>#include <linux/signal.h>#include "kthread.h"#define NTHREADS 2/* the variable that contains the thread data */kthread_t example[NTHREADS];/* prototype for the example thread */static void example_thread(kthread_t *kthread);void producer_thd(void * arg);void consumer_thd(void* arg);/* load the module */int init_module(void){ int i; /* create new kernel threads */ start_kthread(producer_thd, &example[0]); start_kthread(consumer_thd,&example[1]); return(0);}/* remove the module */void cleanup_module(void){ int i; /* terminate the kernel threads */ for (i=0; i<NTHREADS; i++) stop_kthread(&example[i]); return;}/* this is the thread function that we are executing */static void example_thread(kthread_t *kthread){ /* setup the thread environment */ init_kthread(kthread, "example thread"); printk("hi, here is the kernel thread\n"); /* an endless loop in which we are doing our work */ for(;;) { /* fall asleep for one second */ interruptible_sleep_on_timeout(&kthread->queue, HZ); /* We need to do a memory barrier here to be sure that the flags are visible on all CPUs. */ mb(); /* here we are back from sleep, either due to the timeout (one second), or because we caught a signal. */ if (kthread->terminate) { /* we received a request to terminate ourself */ break; } /* this is normal work to do */ printk("example thread: thread woke up\n"); } /* here we go only in case of termination of the thread */ /* cleanup the thread, leave */ exit_kthread(kthread); /* returning from the thread here calls the exit functions */}void producer_sig_hdl(int arg){ printk("Received Signal from consumer \n");}void consumer_sig_hdl(int arg){ printk("Received signal from producer ");} void producer_thd(void* arg){ kthread_t* pthd_Data = (kthread_t*) arg; struct sigaction sa; init_kthread(pthd_Data, "Producer Thread"); sa.sa_handler = producer_sig_hdl; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGUSR1,&sa,NULL); for(;;) { /* fall asleep for one second */ interruptible_sleep_on_timeout(&pthd_Data->queue, HZ); printk("woke up producer thread \n"); kill_pid(&(example[1].thread->pid),SIGUSR1,1); if(pthd_Data->terminate == 1) break; } /* cleanup the thread, leave */ exit_kthread(pthd_Data);}void consumer_thd(void* arg){ kthread_t* pthd_Data = (kthread_t*) arg; init_kthread(pthd_Data, "Producer Thread"); sigaction(pthd_Data->thread->pid,consumer_sig_hdl,1); for(;;) { /* fall asleep for one second */ interruptible_sleep_on_timeout(&pthd_Data->queue, HZ); printk("woke up producer thread \n"); kill_pid(&(example[0].thread->pid),SIGUSR1,1); if(pthd_Data->terminate == 1) break; } /* cleanup the thread, leave */ exit_kthread(pthd_Data);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -