当前位置: 技术问答>linux和unix
新手问一下关于内核定时器的问题, 加载就死机
来源: 互联网 发布时间:2017-05-08
本文导语: 本帖最后由 danistmein 于 2013-12-06 11:29:25 编辑 下面是代码. 就是想做一个定时器实验... #include /**/ #include /*printk()*/ #include /*file_operations*/ #include /*cdev_init() cdev_add()*/ #include /*size_t*/ #include /*dev_t*/ #include ...
下面是代码. 就是想做一个定时器实验...
#include /**/
#include /*printk()*/
#include /*file_operations*/
#include /*cdev_init() cdev_add()*/
#include /*size_t*/
#include /*dev_t*/
#include /*timer_list and timer API*/
#include /*kmalloc() kfree()*/
#define DEV_NUM 1
#define NAME "timer"
struct timer_list *timer;
int tmajor;
int tminor;
struct cdev *dev;
int tdelay = 1000;
int count;
void timer_fn(unsigned long data)
{
count++;
printk(KERN_INFO "timer start is already %d secn", count);
timer->expires += tdelay;
add_timer(timer);
}
static int topen(struct inode *node, struct file *pfile)
{
return 0;
}
static int trelease(struct inode *node, struct file *pfile)
{
return 0;
}
static ssize_t twrite(struct file *pfile, const char __user *buff, size_t count, loff_t *f_pos)
{
return 0;
}
static ssize_t tread(struct file *pfile, char __user *buff, size_t count, loff_t *f_pos)
{
return 0;
}
static struct file_operations tops =
{
.owner = THIS_MODULE,
.open = topen,
.write = twrite,
.read = tread,
.release= trelease,
};
void timer_setup(void)
{
cdev_init(dev, &tops);
dev->owner = THIS_MODULE;
dev->ops = &tops;
cdev_add(dev, MKDEV(tmajor, tminor), DEV_NUM);
}
static int __init timer_init(void)
{
int result;
dev_t devno;
dev = kmalloc(sizeof(struct cdev), GFP_KERNEL);
result = alloc_chrdev_region(&devno, tminor, DEV_NUM, NAME);
tmajor = MAJOR(devno);
if (result expires = jiffies + tdelay;
timer->data = 0;
timer->function = timer_fn;
add_timer(timer);
return 0;
}
static void __exit timer_exit(void)
{
printk(KERN_WARNING "goodbye timer");
cdev_del(dev); /*注销设备*/
unregister_chrdev_region(MKDEV(tmajor, tminor), DEV_NUM); /*删除设备*/
kfree(dev);
del_timer(timer);
kfree(timer);
}
MODULE_AUTHOR("Sinn x.F");
MODULE_LICENSE("GPL");
module_init(timer_init);
module_exit(timer_exit);
但是加载了就会导致oops... 而且oops说的/build/buildd/linux-3.2.0/kernel/timer.c 根本就没有这个路径啊..
顺便问一下.. 当产生oops的时候 能把这个oops保存下来吗...每次得拿手机拍下来..
|
保存下来一般用kdump就好了,配置下
问题好像出在alloc_chrdev_region
问题好像出在alloc_chrdev_region