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

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

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

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

 

        一、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);

 

 

     

Logo

更多推荐