Pages

Tasklet-2 using tasklet_init

In the previous post we saw the basics of a tasklet, now let us look at an example module that creates a tasklet.

We will use the function "tasklet_init" to create the tasklet in this example.

In this module we will create a read proc entry called "initask". On reading this proc entry a tasklet will get initialized and scheduled.

The function executed on reading the proc etnry will be :



Working :

task = kmalloc(sizeof(struct tasklet_struct),GFP_KERNEL);

This line allocates memory for a new tasklet and returns a pointer to the structure.

tasklet_init(task,task_fn,0);

This initates the tasklet "task" . The function that will be executed as a part of the tasklet is "task_fn" which has to be implemented in the module.

In our "task_fn" we will just print the state and value of count of the tasklet. In real tasklets, it is here that the main work happens.

task_fn:



The init and exit functions are going to be pretty simple as we have nothing much to do in them.

init and exit functions:

Thus the full code of the module with the required header files will look as follows tasklet_init.c

Save the code as tasklet_init.c

The Makefile required for this is



To see the output run the following commands



If there are no errors then continue.



Ouptut:

You shoud see the above lines being printed in the logs.

We scheduled the tasklet when proc entry was read, our processor was not heavily loaded hence the tasklet got scheduled immediately and thus the "task_fn" got executed.

The state "2" signifies the tasklet is running and the count being 0 signifies tasklet is enabled.

1 comment:

  1. excellent article explained in simple terms...I was struggling to understand our code, it became very easy looking into this..

    thanks a lot!!!

    -Hari

    ReplyDelete