1. <tr id='2zsb'><strong id='2zsb'></strong><small id='2zsb'></small><button id='2zsb'></button><li id='2zsb'><noscript id='2zsb'><big id='2zsb'></big><dt id='2zsb'></dt></noscript></li></tr><ol id='2zsb'><table id='2zsb'><blockquote id='2zsb'><tbody id='2zsb'></tbody></blockquote></table></ol><u id='2zsb'></u><kbd id='2zsb'><kbd id='2zsb'></kbd></kbd>
      <acronym id='2zsb'><em id='2zsb'></em><td id='2zsb'><div id='2zsb'></div></td></acronym><address id='2zsb'><big id='2zsb'><big id='2zsb'></big><legend id='2zsb'></legend></big></address>

        <i id='2zsb'><div id='2zsb'><ins id='2zsb'></ins></div></i>

        <ins id='2zsb'></ins>

      1. <i id='2zsb'></i>

        <dl id='2zsb'></dl>
        <span id='2zsb'></span>
        <fieldset id='2zsb'></fieldset>

          <code id='2zsb'><strong id='2zsb'></strong></code>
        1. Linux系统下内核定时器的用法

          • 时间:
          • 浏览:8
          • 来源:124软件资讯网

            总的来说  ,timer的用法照旧很简朴的 。主要需要界说一个timer_list变量timer、先初始化timer
             
              init_timer(&timer);
             
              then 对timer的相关参数赋值:
             
              timer.function = fun;
             
              timer.expires = jiffies + TIMER_DELAY;
             
              add_timer(&timer);
             
              在准时器时间到的时间  ,会执行fun ,若是继续准时  ,可以通过
             
              在fun中执行
             
              mod_timer(&timer , jiffies + TIMER_DELAY);
             
              在不需要的时间通过挪用
             
              del_timer(&timer);
             
              删除准时器  。
             
              简朴吧  。这样一个简朴的准时器就完成了  。
             
              呵呵  。
             
              附法式
             
               #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 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);

            附上用户端的测试法式:
            #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);
            }