a reasonable example of a work queue?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



  i'm writing a number of short examples to demonstrate various
features of the kernel and modules, and i threw together the following
to represent using a workqueue to schedule something to run every
second.  is this the accepted and sane way of doing this?  it works,
but i'm wondering if there's a better way.


#include <linux/module.h>	// for all modules
#include <linux/init.h>		// for entry/exit macros
#include <linux/kernel.h>	// for printk macros
#include <linux/workqueue.h>


static void work_func(struct work_struct *unused);
static DECLARE_DELAYED_WORK(work, work_func);

static int count ;

static void
work_func(struct work_struct *unused)
{
	printk(KERN_INFO "yee ha timer, %d.\n", ++count);
	schedule_delayed_work(&work, HZ);     // one second from now
}

static int hi(void)
{
	printk(KERN_INFO "Hello from timer.\n");
	schedule_delayed_work(&work, HZ);
	count = 0 ;
	return 0;
}

static void bye(void)
{
	printk(KERN_INFO "Goodbye from timer.\n");
	cancel_delayed_work(&work);
	flush_scheduled_work();
}

module_init(hi);
module_exit(bye);

MODULE_LICENSE("Dual BSD/GPL");

-- 
========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry
Waterloo, Ontario, CANADA

http://fsdev.net/wiki/index.php?title=Main_Page
========================================================================

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux