当前位置: 技术问答>linux和unix
有关slab的简单应用,导致系统崩溃,请高手指点
来源: 互联网 发布时间:2015-10-22
本文导语: #include #include #include #include static kmem_cache_t * my_kmem_cache; struct great{ char name[40]; char sex[6]; int age; int class; }; struct great * my_great_create(void) { struct great * one; one=kmem_cache_alloc(my_kmem_cac...
#include
#include
#include
#include
static kmem_cache_t * my_kmem_cache;
struct great{
char name[40];
char sex[6];
int age;
int class;
};
struct great * my_great_create(void)
{
struct great * one;
one=kmem_cache_alloc(my_kmem_cache,GFP_KERNEL);
if(!one)
return NULL;
else{
sprintf(one->name,"hacker007");
sprintf(one->sex,"femal");
one->age=26;
one->class=703;
}
return one;
}
void my_great_read(struct great * one )
{
printk("neme=%sn",one->name);
printk("sex=%sn",one->sex);
printk("age=%dn",one->age);
printk("class=%dn",one->class);
kmem_cache_free(my_kmem_cache,one);
}
static int __init slab_init(void)
{
my_kmem_cache=kmem_cache_create("great",sizeof(struct great),0,SLAB_MUST_HWCACHE_ALIGN,NULL,NULL);
if(!my_kmem_cache)
panic("kmem_cache_create errorn");
return 0;
}
static void __exit slab_exit(void)
{
if(!kmem_cache_destroy(my_kmem_cache))
panic("kmem_cache_destroy errorn");
}
EXPORT_SYMBOL(my_great_create);
EXPORT_SYMBOL(my_great_read);
module_init(slab_init);
module_exit(slab_exit);
在2.6内核中编译,加载,然后编写如下程序test.c:
#include
#include
#include
#include
static int my_return()
{
return 0;
}
static int __init my_init(void)
{
struct great * one = my_great_create();
if(one==NULL){
printk("the pointer is NULLn");
BUG_TRAP(!my_return());
}else
my_great_read(one);
return 0;
}
static void __exit my_exit(void)
{
printk("This is inside the exit functionn");
}
module_init(my_init);
module_exit(my_exit);
并编译加载,看到了相关的输出信息,但是我在rmmod test (也就是第二个文件)之后,rmmod第一个文件出错了。导致系统崩溃。我知道应该是在退出函数里面的 panic("kmem_cache_destroy errorn");引起。具体输出报错信息为: Kernel panic - not syncing: kmem_cache_destroy err
or
然后就崩溃了。不知道为什么会导致这一步?请高手指点,谢谢。
#include
#include
#include
static kmem_cache_t * my_kmem_cache;
struct great{
char name[40];
char sex[6];
int age;
int class;
};
struct great * my_great_create(void)
{
struct great * one;
one=kmem_cache_alloc(my_kmem_cache,GFP_KERNEL);
if(!one)
return NULL;
else{
sprintf(one->name,"hacker007");
sprintf(one->sex,"femal");
one->age=26;
one->class=703;
}
return one;
}
void my_great_read(struct great * one )
{
printk("neme=%sn",one->name);
printk("sex=%sn",one->sex);
printk("age=%dn",one->age);
printk("class=%dn",one->class);
kmem_cache_free(my_kmem_cache,one);
}
static int __init slab_init(void)
{
my_kmem_cache=kmem_cache_create("great",sizeof(struct great),0,SLAB_MUST_HWCACHE_ALIGN,NULL,NULL);
if(!my_kmem_cache)
panic("kmem_cache_create errorn");
return 0;
}
static void __exit slab_exit(void)
{
if(!kmem_cache_destroy(my_kmem_cache))
panic("kmem_cache_destroy errorn");
}
EXPORT_SYMBOL(my_great_create);
EXPORT_SYMBOL(my_great_read);
module_init(slab_init);
module_exit(slab_exit);
在2.6内核中编译,加载,然后编写如下程序test.c:
#include
#include
#include
#include
static int my_return()
{
return 0;
}
static int __init my_init(void)
{
struct great * one = my_great_create();
if(one==NULL){
printk("the pointer is NULLn");
BUG_TRAP(!my_return());
}else
my_great_read(one);
return 0;
}
static void __exit my_exit(void)
{
printk("This is inside the exit functionn");
}
module_init(my_init);
module_exit(my_exit);
并编译加载,看到了相关的输出信息,但是我在rmmod test (也就是第二个文件)之后,rmmod第一个文件出错了。导致系统崩溃。我知道应该是在退出函数里面的 panic("kmem_cache_destroy errorn");引起。具体输出报错信息为: Kernel panic - not syncing: kmem_cache_destroy err
or
然后就崩溃了。不知道为什么会导致这一步?请高手指点,谢谢。
|
谁说的不能用sprintf
|
就是,完全可以调用sprintf