In: Computer Science
Design a kernel module that creates a /proc file named /proc/jiffies that reports the current value of jiffies when the /proc/jiffies file is read, such as with the command
cat /proc/jiffies
Be sure to remove /proc/jiffies when the module is removed.
Please help with this assignment using C programming and testing in Linux Ubuntu.
#As per the question first, we have to include the head file that is <linux/jiffies.h> as we are using the jiffies from this head file.
/proc:- It is file system that only exist in the kernel
memory and used for the pre-process stats.
int proc_init(void)
{ /* creates the /proc/jiffies entry */ proc_create(PROC_NAME, 0666, NULL, &proc_ops); return 0; }
void proc_exit(void) { /* removes the /proc/jiffies entry */ remove_proc_entry(PROC_NAME, NULL); }
In this /proc/jiffies
file is removed in the module
exit point proc_exit()
using the function
remove_proc_entry()
.
size_t proc_read(struct file *file, char __user *usr_buf, size_t count, loff_t *pos) { int rv = 0; char buffer[BUFFER_SIZE]; static int completed = 0; if (completed) { completed = 0; return 0; } completed = 1; rv = sprintf(buffer, "%lu\n", jiffies); /* copies kernel space buffer to user space usr buf */ copy_to_user(usr_buf, buffer, rv); return rv; }
#In the above code we use the unassigned long in into the variable buffer which exists in the kerenel memory. As /proc/jiffies can be accesed from the user space
#Now we have to make a file and enter this
#Make file
obj-m += jiffies.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
#Output would be like this :