2008 May 14th Wednesday (五月 十四日 水曜日)

Scheduling task example. /* sched.c */ #include <linux/kernel.h> /*We're doing kernel work*/ #include <linux/module.h> /*Specifically, a module*/ #include <linux/proc_fs.h> /*Necessary because we use the proc fs*/ #include <linux/workqueue.h> /*We scheduale tasks here*/ #include <linux/sched.h> /*We need to put ourselves to sleep and wake up later*/ #include <linux/init.h> /*For __init and __exit*/ #include <linux/interrupt.h> /*For irqreturn_t*/ struct proc_dir_entry *Our_Proc_File; #define PROC_ENTRY_FILENAME "sched" #define MY_WORK_QUEUE_NAME "WQsched.c" /* * The number of times the timer interrupt has been called so far */ static int TimerIntrpt=0; static void intrpt_routine(void*); static int die=0; /*set this to 1 for shut down*/ /* * The work queue structure for this task, from workqueue.h */ static struct workqueue_struct *my_workqueue; static struct work_structTask; static DECLARE_WORK(Task,intrpt_routine,NULL); /* * This function will be called on every timer interrupt. Notice the void* * pointer - task functions can be used for more than one purpose, each time * getting a different parameter. */ staticvoidintrpt_routine(void*irrelevant) {     /*     * Increment the counter     */     TimerIntrpt++;     /*     * If cleanup wants us to die     */     if(die==0)         queue_delayed_work(my_workqueue,&Task,100); } /* * Put data into the proc fs file. */ ssize_t procfile_read(char *buffer, char **buffer_location, off_t offset, int buffer_length, int *eof, void *data) {     int len; /*The number of bytes actually used*/     /*     * It's static so it will still be in memory     * when we leave this function     */     static char my_buffer[80];     static int count=1;     /*     * We give all of our information in one go, so if the anybody asks us     * if we have more information the answer should always be no.     */     if(offset>0)         return0;     /*     * Fill the buffer and get its length     */     len=sprintf(my_buffer,"Timer called%d times so far/n",TimerIntrpt);     count++;     /*     * Tell the function which called us where the buffer is     */     *buffer_location=my_buffer;         return len; } int __init init_module(){     int rv = 0;         /*     * Put the task in the work_timer task queue, so it will be executed at     * next timer interrupt     */     my_workqueue=create_workqueue(MY_WORK_QUEUE_NAME);     queue_delayed_work(my_workqueue,&Task,100);         Our_Proc_File = create_proc_entry(PROC_ENTRY_FILENAME, 0644, NULL);         printk(KERN_INFO "Trying to create /proc/test:/n");         if (Our_Proc_File == NULL){         rv= -ENOMEM;         remove_proc_entry("test", &proc_root);     }     else{         Our_Proc_File->read_proc = procfile_read;         Our_Proc_File->owner = THIS_MODULE;         Our_Proc_File->mode = S_IFREG | S_IRUGO;         Our_Proc_File->uid = 0;         Our_Proc_File->gid = 0;         Our_Proc_File->size = 80;                 printk(KERN_INFO "Success!/n");     }         return rv; } void __exit cleanup_module(){     remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);     printk(KERN_INFO "/proc/%sremoved/n", PROC_ENTRY_FILENAME);         die=1; /*keep intrp_routine from queueing itself*/     cancel_delayed_work(&Task); /*no"newones"*/     flush_workqueue(my_workqueue); /*wait till all "oldones" finished*/     destroy_workqueue(my_workqueue);         /*     * Sleepuntilintrpt_routineiscalledonelasttime.Thisis     * necessary, because otherwise we'll deallocate the memory holding     * intrpt_routine and Task while work_timer still references them.     * Notice that here we don't allow signals to interruptus.     *     * Since WaitQ is now not NULL, this automatically tells the interrupt     * routine it's time to die.     */ } MODULE_LICENSE("GPL");
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值