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");





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值