简体   繁体   English

在用户提供的时间每天在linux内核模块中调度任务

[英]scheduling tasks in linux kernel module everyday at a user provided time

I am writing a linux kernel module which schedules a task using schedule_delayed_work at a particular time which in turn send a signal to a user space program to do some task. 我正在编写一个Linux内核模块,该模块在特定时间使用schedule_delayed_work调度任务,然后依次向用户空间程序发送信号以执行某些任务。 What I did is manually given the time in milliseconds (say 5000ms) and changed it to jiffies using "msec to jiffies" function and tested it and worked. 我所做的是手动指定时间(以毫秒为单位)(例如5000ms),并使用“毫秒转换为吉菲斯”功能将其更改为吉菲斯,并对其进行了测试和工作。

My use case is that the user will give a time (say 5 pm) and the module has to schedule it to send the signal everyday at 5 pm to the user program. 我的用例是用户要给一个时间(比如说下午5点),模块必须安排它每天在下午5点将信号发送给用户程序。 I am totally confused in how to calculate the milliseconds from the user given time for everyday basis. 我对于如何从用户给定的时间每天计算毫秒感到完全困惑。

I used workqueue to create a queue and then the task to accomplish and doing the scheduling. 我使用工作队列创建队列,然后使用任务来完成并进行调度。

My kernel module: 我的内核模块:

    static void wq_handler_function(struct work_struct *work);
    static unsigned long delay;

    static struct workqueue_struct *my_wq; // my workqueue
    static DECLARE_DELAYED_WORK(my_work, wq_handler_function); //my work/task


    static void wq_handler_function(struct work_struct *work)
    {
       printk(KERN_DEBUG "handler function called\n");
       if(my_wq)
       {
          /*Do some work like sending signal to user space*/
          schedule_delayed_work(&my_work, delay); /*reschedule after the first scheduled time finished*/
       }
    }

    int sig_init_module(void)
    {
       printk(KERN_DEBUG "signal module initiated\n");
       delay = msecs_to_jiffies(5000); //Manually given 5000ms (5 sec) for scheuling
       if(!my_wq)
          my_wq = create_workqueue("my_queue");

          if(my_wq)
          {
             schedule_delayed_work(&my_work, delay); /*schedule for the first time while module initiates*/
          }
        return 0;
     }

     void sig_cleanup_module(void)
     {
        flush_scheduled_work();
        cancel_delayed_work_sync(&my_work);

        flush_workqueue(my_wq);
        destroy_workqueue(my_wq);

        printk(KERN_DEBUG "signal module removed\n");
     }

     module_init(sig_init_module);
     module_exit(sig_cleanup_module);

Kindly help me to have a solution for this. 请帮助我解决这个问题。 Thanks in advance!!!. 提前致谢!!!。

I don't understand why kernel modification would be desirable or necessary. 我不明白为什么需要修改内核。 If you want something periodically done (eg log rotation), add it to cron. 如果您希望定期进行某些操作(例如日志轮换),请将其添加到cron中。 Another option would be to use timerfd. 另一个选择是使用timerfd。

use mktime() function in kernel code which takes the wall time as arguments and directly returns the jiffies value. 在内核代码中使用mktime()函数,该函数将获取时间作为参数并直接返回jiffies值。 For info about mktime, see this http://www.makelinux.net/ldd3/chp-7-sect-2 有关mktime的信息,请参见此http://www.makelinux.net/ldd3/chp-7-sect-2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM