Search

Example of using getnstimeofday in linux kernel

The linux kernel provides a number of interfaces to manage time. getnstimeofday is one of them, which gives the time in seconds and nanoseconds . The function is implemented in "timekeeping32.h" and returns a structure of the type timespec which has two members.



To print the time, we only need to print the values of tv_sec and tv_nsec which gets filled by the call to the function getnstimeofday.

In the following example code, we have created a proc entry called gettime, which prints out the values of seconds and nanoseconds when read.

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include<linux/slab.h>
int len;
char *msg;
ssize_t size;
struct timespec ts;
ssize_t read_proc(struct file *filp,char *buf,size_t count,loff_t *offp )
{
struct timespec ts;
char *temp;
temp=kmalloc(50*sizeof(char),GFP_KERNEL);
getnstimeofday(&ts);
sprintf(temp,"%ld seconds \n%ld nanoseconds\n",ts.tv_sec, ts.tv_nsec);
len=strlen(temp);
size=sizeof(char)*len;
return simple_read_from_buffer(buf,count,offp,temp,size);
}
struct file_operations proc_fops = {
read: read_proc
};
void create_new_proc_entry(void)
{
proc_create("gettime",0,NULL,&proc_fops);
}
int proc_init (void) {
create_new_proc_entry();
return 0;
}
void proc_cleanup(void) {
remove_proc_entry("gettime",NULL);
}
MODULE_LICENSE("GPL");
module_init(proc_init);
module_exit(proc_cleanup);


Save the above code as proc_read_gettimeofday.c and compile the code using the following makefile.



Compile and insert the module using



To see the output, just read the proc entry gettime, using the cat command.




No comments:

Post a Comment