Use kernel programming in Linux to schedule tasks

Source: Internet
Author: User
Use kernel programming in Linux to implement scheduling tasks-general Linux technology-Linux programming and kernel information. The following is a detailed description. Schedule a task

Often, we have "Chores" that must be done regularly or frequently ". If this task is completed by a process, we can put it into the crontab file. If this task is completed by a kernel module, we have two possible options. The first is to put a process into the crontab file, which will wake up the module through a system call when necessary, such as opening a file. However, this is very inefficient. We need to run a new process outside the crontab and read a new execution table into the memory. All these are just to wake up a kernel module in the memory.

We do not need to do this. We can create a function that is called during every interruption of time. The method is to create a task, which is contained in a struct tq_struct, and contains a pointer to the function entry address. Then, we use queue_task to put this task into a task list called tq_timer, which is a list of tasks to be executed during the next interruption. Because we want this function to be continuously executed, we need to put it back in tq_timer every call for the next interruption.

Another point is worth remembering. When a module is deleted by rmmod, its index counter is checked first. If the value is 0, module_cleanup is called. Then, this module and all its functions are deleted from the memory. No one checks whether the clock task list still contains pointers to these functions, but it is unavailable now. A long time later (from the computer's perspective, it may be 1% seconds in human eyes), the kernel had a clock interruption and tried to call all functions in the task list. Unfortunately, this function does not exist. In most cases, its memory is not used yet, and you get an extreme error message. However, if other codes are in the same address, the situation will be very bad. Unfortunately, we do not have a method to deregister a task from the task list.

Since the cleanup_module function cannot return an error horse (it is a void function), the solution is not to let it return. Instead, call sleep_on or module_sleep_on (note 10.1) to suspend the rmmod process. Before that, it sets a variable to notify the function called when the clock is interrupted to stop appending itself. Then, when the next clock is interrupted, the rmmod process will be awakened, and our functions are no longer in the queue, so that we can safely delete the module.
QUOTE:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.