Linux Kernel timer usage-general Linux technology-Linux programming and kernel information. The following is a detailed description. According to the linux driver development details, I copied a second program and used timer.
In general, the usage of timer is still very simple. You need to define a timer_list variable timer and initialize timer first.
Init_timer (& timer );
Then assigns values to related timer parameters:
Timer. function = fun;
Timer. expires = jiffies + TIMER_DELAY;
Add_timer (& timer );
When the timer time is reached, fun will be executed. If you continue the timer, you can
Run
Mod_timer (& timer, jiffies + TIMER_DELAY );
Call
Del_timer (& timer );
Delete the timer.
Simple. Such a simple timer is complete.
Haha.
Appendix:
CODE: # include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Define SECOND_MAJOR 0 Static int second_major = SECOND_MAJOR; Struct second_dev { Struct cdev; Atomic_t counter; Struct timer_list s_timer; }; Struct second_dev * second_devp; Static void second_timer_handle (unsigned long arg) { Mod_timer (& second_devp-> s_timer, jiffies + HZ ); Atomic_inc (& second_devp-> counter ); Printk (KERN_ERR "current jiffies is % ld \ n", jiffies ); } Int second_open (struct inode * inode, struct file * filp) { Init_timer (& second_devp-> s_timer ); Second_devp-> s_timer.function = & second_timer_handle; Second_devp-> s_timer.expires = jiffies + HZ; Add_timer (& second_devp-> s_timer ); Atomic_set (& second_devp-> counter, 0 ); Return 0; } Int second_release (struct inode * inode, struct file * filp) { Del_timer (& second_devp-> s_timer ); Return 0; } Static ssize_t second_read (struct file * filp, char _ user * buf, size_t count, Loff_t * ppos) { Int counter; Counter = atomic_read (& second_devp-> counter ); If (put_user (counter, (int *) buf )) { Return-EFAULT; } Else { Return sizeof (unsigned int ); } } Static const struct file_operations second_fops = { . Owner = THIS_MODULE, . Open = second_open, . Release = second_release, . Read = second_read, }; Static void second_setup_cdev (struct second_dev * dev, int index) { Int err, devno = MKDEV (second_major, index ); Cdev_init (& dev-> cdev, & second_fops ); Dev-> cdev. owner = THIS_MODULE; Dev-> cdev. ops = & second_fops; Err = cdev_add (& dev-> cdev, devno, 1 ); If (err) { Printk (KERN_NOTICE "Error % d add second % d", err, index ); } } Int second_init (void) { Int ret; Dev_t devno = MKDEV (second_major, 0 ); If (second_major) { Ret = register_chrdev_region (devno, 1, "second "); } Else { Ret = alloc_chrdev_region (& devno, 0, 1, "second "); Second_major = MAJOR (devno ); } If (ret <0) { Return ret; } Second_devp = kmalloc (sizeof (struct second_dev), GFP_KERNEL ); If (! Second_devp) { Ret =-ENOMEM; Goto fail_malloc; } Memset (second_devp, 0, sizeof (struct second_dev )); Second_setup_cdev (second_devp, 0 ); Return 0; Fail_malloc: Unregister_chrdev_region (devno, 1 ); } Void second_exit (void) { Cdev_del (& second_devp-> cdev ); Kfree (second_devp ); Unregister_chrdev_region (MKDEV (second_major, 0), 1 ); } MODULE_AUTHOR ("Song Baohua "); MODULE_LICENSE ("Dual BSD/GPL "); Module_param (second_major, int, S_IRUGO ); Module_init (second_init ); Module_exit (second_exit ); Attach the client test program: # Include # Include # Include Int main (void) { Int fd, I; Int data; Fd = open ("/dev/second", O_RDONLY ); If (fd <0) { Printf ("open/dev/second error \ n "); } For (I = 0; I <20; I ++) { Read (fd, & data, sizeof (data )); Printf ("read/dev/second is % d \ n", data ); Sleep (1 ); } Close (fd ); } |