|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
vim除非你打算真正的学好linux,或者说打算长久时间学习他,而且肯花大量时间vim,否则,最好别碰
总的来讲,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>
#defineSECOND_MAJOR0
staticintsecond_major=SECOND_MAJOR;
structsecond_dev
{
structcdevcdev;
atomic_tcounter;
structtimer_lists_timer;
};
structsecond_dev*second_devp;
staticvoidsecond_timer_handle(unsignedlongarg)
{
mod_timer(&second_devp->s_timer,jiffies+HZ);
atomic_inc(&second_devp->counter);
printk(KERN_ERR"currentjiffiesis%ld
",jiffies);
}
intsecond_open(structinode*inode,structfile*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);
return0;
}
intsecond_release(structinode*inode,structfile*filp)
{
del_timer(&second_devp->s_timer);
return0;
}
statiCSSize_tsecond_read(structfile*filp,char__user*buf,size_tcount,
loff_t*ppos)
{
intcounter;
counter=atomic_read(&second_devp->counter);
if(put_user(counter,(int*)buf))
{
return-EFAULT;
}else
{
returnsizeof(unsignedint);
}
}
staticconststructfile_operationssecond_fops=
{
.owner=THIS_MODULE,
.open=second_open,
.release=second_release,
.read=second_read,
};
staticvoidsecond_setup_cdev(structsecond_dev*dev,intindex)
{
interr,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%daddsecond%d",err,index);
}
}
intsecond_init(void)
{
intret;
dev_tdevno=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)
{
returnret;
}
second_devp=kmalloc(sizeof(structsecond_dev),GFP_KERNEL);
if(!second_devp)
{
ret=-ENOMEM;
gotofail_malloc;
}
memset(second_devp,0,sizeof(structsecond_dev));
second_setup_cdev(second_devp,0);
return0;
fail_malloc:
unregister_chrdev_region(devno,1);
}
voidsecond_exit(void)
{
cdev_del(&second_devp->cdev);
kfree(second_devp);
unregister_chrdev_region(MKDEV(second_major,0),1);
}
MODULE_AUTHOR("SongBaohua");
MODULE_LICENSE("DualBSD/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>
intmain(void)
{
intfd,i;
intdata;
fd=open("/dev/second",O_RDONLY);
if(fd<0)
{
printf("open/dev/seconderror
");
}
for(i=0;i<20;i++)
{
read(fd,&data,sizeof(data));
printf("read/dev/secondis%d
",data);
sleep(1);
}
close(fd);
}
</p>
在linux中学习命令的最好办法是学习Shell脚本编程,Shell脚本比起其他语言来学习简单,但是功能却十分强大.通过学习Shell编程,能让你掌握大量的linux命令。 |
|