#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) { struct sched_param param = { .sched_priority = THREAD_PRIORITY }; /* Set realtime priorty */ sched_setscheduler(thread, THREAD_SCHEDULER, ¶m); /* 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); schedule_hrtimeout(&thread_next, HRTIMER_MODE_ABS); 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) { /* Create kernel thread */ thread = kthread_run((void *)&runloop, NULL, MODULE_NAME); if (IS_ERR(thread)) { printk(KERN_INFO KLOG_PREFIX ": unable to start kernel thread\n"); return -ENOMEM; } return 0; } void __exit cleanup_test_module(void) { if(thread != NULL) kthread_stop(thread); thread = NULL; } MODULE_LICENSE("GPL"); MODULE_AUTHOR("Stefan Agner