Linux Device Driver Tutorial Part 15 – Workqueue in Linux Kernel Part 2

This is the Series on Linux Device Driver. The aim of this series is to provide the easy and practical examples that anyone can understand. In our previous tutorial we have seen the Workqueue using Static method through Device Driver Programming. Now we are going to see Linux Device Driver Tutorial Part 15 – Workqueue in Linux (Dynamic Creation Method).

Initialize work using Static Method

The below call creates a workqueue by the name work and the function that gets scheduled in the queue is work_fn.

INIT_WORK(work,work_fn)

Where,

name: The name of the “work_struct” structure that has to be created.
func: The function to be scheduled in this workqueue.

Schedule work to the Workqueue

These below functions used to allocate the work to the queue.

Schedule_work

This function puts a job in the kernel-global workqueue if it was not already queued and leaves it in the same position on the kernel-global workqueue otherwise.

int schedule_work( struct work_struct *work );

where,

work – job to be done

Returns zero if work was already on the kernel-global workqueue and non-zero otherwise.

Scheduled_delayed_work

After waiting for a given time this function puts a job in the kernel-global workqueue.

int scheduled_delayed_work( struct delayed_work *dwork, unsigned long delay );

where,

dwork – job to be done

delay – number of jiffies to wait or 0 for immediate execution

Schedule_work_on

This puts a job on a specific cpu.

int schedule_work_on( int cpu, struct work_struct *work );

where,

cpu– cpu to put the work task on

work– job to be done

Scheduled_delayed_work_on

After waiting for a given time this puts a job in the kernel-global workqueue on the specified CPU.

int scheduled_delayed_work_on(
           int cpu, struct delayed_work *dwork, unsigned long delay );

where,

cpu – cpu to put the work task on

dwork – job to be done

delay – number of jiffies to wait or 0 for immediate execution

Delete work from workqueue

There are also a number of helper functions that you can use to flush or cancel work on work queues. To flush a particular work item and block until the work is complete, you can make a call to flush_work. All work on a given work queue can be completed using a call to . In both cases, the caller blocks until the operation is complete. To flush the kernel-global work queue, call flush_scheduled_work.

int flush_work( struct work_struct *work );
void flush_scheduled_work( void );

Cancel Work from workqueue

You can cancel work if it is not already executing in a handler. A call to cancel_work_sync will terminate the work in the queue or block until the callback has finished (if the work is already in progress in the handler). If the work is delayed, you can use a call to cancel_delayed_work_sync.

int cancel_work_sync( struct work_struct *work );
int cancel_delayed_work_sync( struct delayed_work *dwork );

Check workqueue

Finally, you can find out whether a work item is pending (not yet executed by the handler) with a call to work_pending ordelayed_work_pending.

work_pending( work );
delayed_work_pending( work );

Programming

Driver Source Code

In that source code, When we read the /dev/etx_device interrupt will hit (To understand interrupts in Linux go to this tutorial). Whenever interrupt hits, I’m scheduling the work to the workqueue. I’m not going to do any job in both interrupt handler and workqueue function,  since it is a tutorial post. But in real workqueues, this function can be used to carry out any operations that need to be scheduled.

MakeFile

Building and Testing Driver

  • Build the driver by using Makefile (sudo make)
  • Load the driver using sudo insmod driver.ko
  • To trigger interrupt read device file (sudo cat /dev/etx_device)
  • Now see the Dmesg (dmesg)

[email protected]: dmesg

[11213.943071] Major = 246 Minor = 0
[11213.945181] Device Driver Insert…Done!!!
[11217.255727] Device File Opened…!!!
[11217.255747] Read function
[11217.255783] Shared IRQ: Interrupt Occurred
[11217.255845] Executing Workqueue Function
[11217.255860] Device File Closed…!!!

  • We can able to see the print “Shared IRQ: Interrupt Occurred“ and “Executing Workqueue Function
  • Unload the module using sudo rmmod driver

In our next tutorial we will discuss Workqueue using own worker thread.

Download our new Android app. You can learn all Embedded Tutorials from your Android Phone easily.

Click Here to Download App!