#include #include #include #define THREAD_PRIORITY (80) #define THREAD_SCHEDULER SCHED_FIFO #define MODULE_NAME "test-module" #define KLOG_PREFIX MODULE_NAME ": " struct task_struct *thread; static ktime_t thread_next; static ktime_t now; /* * Threaded Runloop */ static int runloop(void *unused) { int ret; /* Daemonize */ daemonize(MODULE_NAME); /* Allow SIGTERM signal */ allow_signal(SIGTERM); /* Calculate first shot */ now = ktime_get(); thread_next = ktime_add_us(now, 500); do { set_current_state(TASK_UNINTERRUPTIBLE); ret = schedule_hrtimeout(&thread_next, HRTIMER_MODE_ABS); if(ret == -EINTR) { printk(KERN_INFO KLOG_PREFIX "Signal received\n"); continue; } thread_next = ktime_add_us(thread_next, 500); } while (!kthread_should_stop()); printk(KERN_INFO KLOG_PREFIX "Thread exited\n"); return 0; } int __init init_test_module(void) { struct sched_param param = { .sched_priority = THREAD_PRIORITY }; /* Kernel Thread erzeugen */ if(thread == NULL) thread = kthread_create((void *)&runloop, NULL, "testthread"); // Realtime scheduler sched_setscheduler(thread, THREAD_SCHEDULER, ¶m); wake_up_process(thread); return 0; } void __exit cleanup_test_module(void) { if(thread != NULL) kthread_stop(thread); thread = NULL; } MODULE_LICENSE("GPL"); MODULE_AUTHOR("Stefan Agner