Timer : One Shot ================ .. tab-set:: .. tab-item:: Timer : One Shot * In this program, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to create the kernel timer ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to modify the timer's timeout ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use Timer API's ? * `timer_setup `_ * `mod_timer `_ * `del_timer `_ .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Key learnings in this program ` * :ref:`Explanation of program part by part ` * :ref:`List of headers ` * :ref:`Modules macro ` * :ref:`Initialize variables and functions ` * :ref:`Module init functions ` * :ref:`Module exit functions ` * :ref:`Timer APIs ` * :ref:`Mention init and exit functions ` * :ref:`See the full program below ` * :ref:`Makefile ` * :ref:`Compile and Load ` * :ref:`Summary of Timer APIs ` .. _p1_timerOneShot_0: .. tab-set:: .. tab-item:: Key learnings in this program * A timer is a specialized type of clock used for measuring specific time intervals. * ``timer_setup`` used to initialize and set up a timer object. .. code-block:: c timer_setup(&my_timer, timer_callback, 0); * ``mod_timer`` used to modify a timer’s timeout. This is a more efficient way to update the expires field of an active timer (if the timer is inactive it will be activated). For example, .. code-block:: c mod_timer(&my_timer, jiffies + msecs_to_jiffies(TIMEOUT)); * ``del_timer`` used to stop a running timer and prevent it's callback function from being executed. For example .. code-block:: c del_timer(&my_timer); .. _p1_timerOneShot_4: .. tab-set:: .. tab-item:: Explanation of program part by part * Here is the part by part explanation of the source code. .. _p1_timerOneShot_5: .. tab-set:: .. tab-item:: List of headers * Add the list of headers to the refer the APIs used in this source code. .. code-block:: c #include #include #include #include #include #include #include .. _p1_timerOneShot_6: .. tab-set:: .. tab-item:: Modules macro * Add the modules macro which gives information about the author, license and description. .. code-block:: c MODULE_LICENSE("GPL"); MODULE_AUTHOR("Linux_usr"); MODULE_DESCRIPTION("One Shot Timer"); .. _p1_timerOneShot_7: .. tab-set:: .. tab-item:: Initialize variables and functions * Initialize the functions and variables to be using in this program. .. code-block:: c static struct timer_list my_timer; static void timer_callback(struct timer_list *timer) .. _p1_timerOneShot_8: .. tab-set:: .. tab-item:: Module init function * Create the module init function to execute when the module is loaded to kernel. .. code-block:: static int __init timer_init(void) { pr_info("Hello world\n"); timer_setup(&my_timer, timer_callback, 0); mod_timer(&my_timer, jiffies + msecs_to_jiffies(TIMEOUT)); return 0; } .. _p1_timerOneShot_9: .. tab-set:: .. tab-item:: Module exit function * Create the module exit function which executes once when the module is unloaded from the kernel. .. code-block:: c static void __exit timer_exit(void) { del_timer(&my_timer); pr_info("Good bye, world\n"); } .. _p1_timerOneShot_10: .. tab-set:: .. tab-item:: Timer APIs * Create the timer APIs which will be executed once the timer is called. .. code-block:: c static void timer_callback(struct timer_list *timer) { pr_info("Inside Timer Callback function\n"); } .. _p1_timerOneShot_11: .. tab-set:: .. tab-item:: Mention init and exit functions * Add the module init and exit functions to execute it once the module is loaded and unloaded. .. code-block:: c module_init(timer_init); module_exit(timer_exit); .. _p1_timerOneShot_12: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: timer.c .. literalinclude:: p1_timerOneShot/timer.c :language: c :linenos: .. _p1_timerOneShot_1: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p1_timerOneShot/Makefile :language: c :linenos: .. _p1_timerOneShot_2: .. tab-set:: .. tab-item:: Compile and Load * Run make to compile the kernel module. .. code-block:: shell $ make make -C /lib/modules/5.4.0-150-generic/build M=$HOME/kernel_timer modules make[1]: Entering directory '/usr/src/linux-headers-5.4.0-150-generic' CC [M] $HOME/kernel_timer/timer.o Building modules, stage 2. MODPOST 1 modules CC [M] $HOME/kernel_timer/timer.mod.o LD [M] $HOME/kernel_timer/timer.ko make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-150-generic' * Check if timer.ko is generated or not using ls command. .. code-block:: shell $ ls -l total 36 -rw-rw-r-- 1 test test 154 Feb 26 13:18 Makefile -rw-rw-r-- 1 test test 47 Feb 26 13:18 modules.order -rw-rw-r-- 1 test test 0 Feb 26 13:18 Module.symvers -rw-rw-r-- 1 test test 820 Feb 26 13:18 timer.c -rw-rw-r-- 1 test test 5880 Feb 26 13:18 timer.ko -rw-rw-r-- 1 test test 47 Feb 26 13:18 timer.mod -rw-rw-r-- 1 test test 919 Feb 26 13:18 timer.mod.c -rw-rw-r-- 1 test test 3448 Feb 26 13:18 timer.mod.o -rw-rw-r-- 1 test test 3320 Feb 26 13:18 timer.o * Run insmod command to load the module in kernel. .. code-block:: shell $ sudo insmod ./timer.ko * Check kernel messages to see if the module is loaded or not. .. code-block:: shell $ dmesg [ 1173.096954] Hello world [ 1178.154636] Inside Timer Callback function * Run rmmod to unload the kernel module. .. code-block:: shell $ sudo rmmod timer * Check the kernel messages to see if the module is unloaded or not. .. code-block:: shell $ dmesg [ 1173.096954] Hello world [ 1178.154636] Inside Timer Callback function [ 1184.041014] Good bye, world .. _p1_timerOneShot_3: .. tab-set:: .. tab-item:: Summary of timer APIs =============================== =========================== API Learning =============================== =========================== timer_setup To initialize the timer mod_timer To modify a timer's timeout del_timer To stop a running timer msecs_to_jiffies To convert milliseconds =============================== ===========================