linux 工作队列之schedule_work

      在许多情况下,设备驱动程序不需要有自己的工作队列。如果我们只是偶尔需要向队列中提交任务,

则一种更简单、更有效的办法是使用内核提供的共享的默认工作队列。但是,如果我们使用了这个默认

的工作队列,则应该记住我们正在和他人共享该工作队列。这意味着,我们不应该长期独占该队列,即

:不能长时间休眠,而且我们的任务可能需要更长的时间才能获得处理器。

 

        一、schedule使用步骤:

        1、定义相关数据

                 static struct work_struct  jiq_work;

        2、编写要提交到工作队列中的函数

                static_void jiq_print_wq(struct work_struct *work)

                {

                        ........

                }

        3、完成数据的初始化工作

                INIT_WORK(&jiq_work, jiq_print_wq);

        4、将任务提交到工作队列

                schedule_work(&jiq_work);

 

        二、示例               

        #include <linux/module.h>

        #include <linux/init.h>

        #include <linux/workqueue.h>

 

        static struct workqueue_struct *wq = NULL;

        static struct work_struct work;

 

        static void work_handler(struct work_struct *data)

        {

                printk(KERN_ALERT"work handler function");

        }

 

        static int __init test_init(void)

        {

                wq = create_singlethread_workqueue("my_workqueue");

                if(!wq)

                        goto err;

                INIT_WORK(&work, work_handler);

                queue_work(&work);

                return 0;

              

                err:

                        return -1;

        }

 

        static void __exit test_exit(void)

        {

                destroy_workqueue(wq);

        }

 

        MODULE_LICENSE("GPL");

        module_init(test_init);

        module_exit(rest_exit);

 

          

 

 

 

 

 

 

 

 

 

 schedule_work(&jiq_work);

 

 

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值