Dynamically calculate the time duration passed since a module is loaded, and display the value in seconds again in proc file named seconds. Along with jiffies, we'll utilize another constant variable named HZ, which represents the tick frequency, to calculate time elapsed since module was loaded.
- Finish workspace setup
- Understand workflow
- Download the folder
secondsto local workspace
Almost similar to hello & jiffies, just we calculate and output time elapsed since module loaded.
- Navigate to Project directory
cd seconds
- compile Module
sudo make
- Look for
secondsin loaded Modules
sudo lsmod | grep seconds
You will get no results
- Look for file
secondsin proc fs
cat /proc/seconds
you will see :
cat: proc/seconds: No such file or directory
- Insert Module
sudo insmod seconds.ko
- Again check for seconds in module list
sudo lsmod | grep seconds
- Check the proc file again
cat /proc/seconds
Now we can see
Module living for 14 seconds!
each time we readsecondsproc file, we will see a new value, which is dynamically calculated when read!
- Remove the Module and verify it's gone
sudo rmmod seconds
sudo lsmod | grep seconds
No output means module is removed
- Check for proc file again
cat /proc/seconds
- Additional Header(s)
#include <asm/param.h>
- Initialize a variable to store
jiffiesat module load
static unsigned long jiffies_init;
- Store jiffies into jiffies_init at module load inside
proc_init
jiffies_init = jiffies;
- Inside proc_read modified
sprintf
rv = sprintf(buffer,"Module living for %lu seconds\n" ,(jiffies-jiffies_init)/HZ);
We calculate effective duration since module was loaded using current jiffies and jiffies_init, together with HZ contant.
Note : We declare custom functions and variable as static, this is to limit their scope to current module,otherwise they would be accessible in whole of Kernel and pollute global namespace!
Next up : What's next


