萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

高手進階:Linux 核心定時器 使用方式與完整字符裝置驅動範例

Linux 核心定時器使用方式簡單,主要步驟:

  1. 定義 timer_list 結構變數 timer
  2. 初始化:init_timer(&timer);
  3. 設定回呼函式與到期時間:
    timer.function = fun;
    timer.expires = jiffies + TIMER_DELAY;
  4. 註冊:add_timer(&timer);

時間到達自動執行回呼函式 fun。週期性定時在回呼內重設:

mod_timer(&timer, jiffies + TIMER_DELAY);

不再需要時清理:

del_timer(&timer);

完整範例:字符裝置模組每秒遞增計數器

核心模組程式碼

#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/timer.h>
#include <asm/atomic.h>
#define SECOND_MAJOR 0
static int second_major = SECOND_MAJOR;
struct second_dev {
    struct cdev 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 = atomic_read(&second_devp->counter);
    if (put_user(counter, (int *)buf)) return -EFAULT;
    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);
    return ret;
}
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);

使用者空間測試程式

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(void) {
    int fd, i, data;
    fd = open("/dev/second", O_RDONLY);
    if (fd < 0) { printf("open /dev/second error\n"); return -1; }
    for (i = 0; i < 20; i++) {
        read(fd, &data, sizeof(data));
        printf("read /dev/second is %d\n", data);
        sleep(1);
    }
    close(fd);
    return 0;
}

載入核心模組後,使用者程式每秒讀取裝置檔,觀察計數器每秒遞增。

copyright © 萬盛學電腦網 all rights reserved