linux schedule and queue

http://blog.sina.com.cn/s/blog_78d30f6b0102uyaf.html

http://www.xuebuyuan.com/1198936.html

http://www.360doc.com/content/13/0411/11/11645800_277544630.shtml


1.使用內核提供的共享隊列

#include<linux/module.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/types.h>
#include<linux/kthread.h>
#include<linux/wait.h>
#include<linux/string.h>
#include<linux/sysctl.h>
#include<linux/workqueue.h>


static void defense_work_handler(struct work_struct *work);

static DECLARE_DELAYED_WORK(defense_work, defense_work_handler);

static void defense_work_handler(struct work_struct *work)
{
        printk(KERN_INFO "defense_work_handler function.\n");
        schedule_delayed_work(&defense_work, msecs_to_jiffies(5000));
}

static int __init main_init(void)
{
        schedule_delayed_work(&defense_work, 1 * HZ);

        return 0;
}

static void __exit main_exit(void)
{
        cancel_delayed_work_sync(&defense_work);
}

module_init(main_init);
module_exit(main_exit);
MODULE_LICENSE("GPL");

2.使用自定義工作隊列

#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/proc_fs.h>
#include<linux/workqueue.h>
#include<linux/sched.h>
#include<linux/init.h>
#include<linux/interrupt.h>
#include<linux/delay.h>

struct workqueue_struct *test_workqueue;
struct delayed_work test_delaywork;

void delay_func(struct work_struct *work)
{

        printk(KERN_ALERT "delay_func() called...\n");

        queue_delayed_work(test_workqueue, &test_delaywork, msecs_to_jiffies(5000));
}

static int __init example_init(void)
{

        test_workqueue = create_workqueue("test_workqueue");
        if (!test_workqueue)
                return 1;

        INIT_DELAYED_WORK(&test_delaywork, delay_func);

        queue_delayed_work(test_workqueue, &test_delaywork, 500);
        return 0;
}

static void __exit example_exit(void)
{
        int ret;
        ret = cancel_delayed_work(&test_delaywork);

        flush_workqueue(test_workqueue);
        destroy_workqueue(test_workqueue);
        printk(KERN_INFO "Goodbye! ret=%d\n", ret);
}

module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章