首页 | 资讯动态 | linux基础 | 系统管理 | 网络管理 | 编程开发 | linux数据库 | 服务器技术 | linux相关 | linux认证 | 嵌入式 | 下载中心 | 专题 | linux招聘 | 镜像站
OKLinux中文技术站
·设为首页
·加入收藏
·联系我们
系统管理: 中文环境 系统管理 桌面应用 内核技术 | Linux基础: 基础入门 安装配置 常用命令 经验技巧 软件应用 | Linux数据库: Mysql Postgre Oracle DB2 Sybase other
网络管理: 网络安全 网络应用 Linux服务器 环境配置 黑客安全 | 编程开发: PHP CC++ Python Perl Shell 嵌入式开发 java jsp | PHP技术: PHP基础 PHP技巧 PHP应用 PHP文摘
Linux资讯 Linux招聘 Linux专题 Apache | Linux相关: 硬件相关 Linux解决方案 Linux认证 企业应用 其它Unix | 相关下载: 资料下载 参考手册 开发工具 服务器类 软路由 其它
 技术搜索:
会员中心 注册会员 高级搜索  
  → 当前位置:首页>系统管理>内核技术>正文

Linux系统下内核定时器的用法

http://www.oklinux.cn  2008-03-14  来源: 赛迪网 sixth  会员收藏  游客收藏  【 】 

总的来说,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 <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;

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 <stdio.h>
#include <unistd.h>
#include <fcntl.h>

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);
}


上一篇:Java基础知识:谈谈简单Hibernate入门   下一篇:如何将JBoss做成Redhat Linux的系统服务


收藏于收藏夹】 【评论】 【推荐】 【打印】 【关闭
相关文档
·学习园地:Linux系统内核中判断大小的宏
·系统编译:如何给Make命令来传递参数
·Linux 2.6内核中sysfs文件系统简单概述
·Fedora 8 Linux系统的内核配置注意事项
·升级Linux内核的一般步骤方法
·Linux发行版知识普及:三个版本的CPUID
·编译安装Virtualbox驱动模块
· Linux系统的内核解读入门
·新手学堂 Linux系统的内核解读入门
·Linux系统内核中网络参数的意义及其应用 (1)
·走向Linux系统高手之路 内核编译过程解析
·Linux系统中安装内核的方法详细介绍
·Linux内核更新:Linux Kernel 2.6.22.7
·Linux系统下 内核模块开发详细解析
·在Ubuntu 7.10下 编译使用新内核的方法
·Linux系统线程同步:互斥量(mutex)
发表评论
密码: 匿名评论
评论内容:

(不超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规)
 
  最新文档
·学习园地:Linux系统内核中判断大小的
·系统编译:如何给Make命令来传递参数
·Linux 2.6内核中sysfs文件系统简单概述
·Fedora 8 Linux系统的内核配置注意事项
·升级Linux内核的一般步骤方法
·Linux发行版知识普及:三个版本的CPUID
·编译安装Virtualbox驱动模块
· Linux系统的内核解读入门
·新手学堂 Linux系统的内核解读入门
·Linux系统内核中网络参数的意义及其应
·走向Linux系统高手之路 内核编译过程解
·Linux系统中安装内核的方法详细介绍
  阅读排行
· 深入理解LINUX内核中文版下载地址
·基于S3C44B0微处理器的uClinux内核引导
·Kernel command using Linux system ca
·Linux 2.6内核如何武装Fedora Core 2
·Process priority and control on AIX
·Linux操作系统的内核编译内幕详解
·推荐:Linux用户态与内核态的交互
·通过振动向Linux ThinkPad传输信息
·Linux操作系统源代码详细分析(二)
·Linux系统内核接收以太帧的处理程序
·Linux and symmetric multiprocessing
·主流嵌入式Linux系统下GUI解决方案
·揭秘Linux内核调试器之内幕
·用命令行加挂Linux的文件系统简介
·Linux内核和核心OS组件的测试与分析
网摘收藏: