下面这个程序和上几篇的一样,不过是内核中实现的。可以动态的分配大小的链表结构体,就是加载后会死
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEBUG
#ifdef DEBUG
#define DBG(x...) printk(x)
#else
#define DBG(x...)
#endif
typedef struct kbuff_list
{
#if 1
char *Pmalloc_buff;
#else
char buff_node[20];
#endif
int flag;
struct kbuff_list *next;
}KBUFF;
KBUFF *pread_kbuff,*pirq__kbuff;
unsigned long list_cnt;
static struct task_struct *ret;
struct semaphore sem;
static void readThread(void){
KBUFF *p1;
char *p_malloc_buff;
while(1){
down(&sem);
if(pread_kbuff !=NULL){
p1=pread_kbuff;
#if 1
p_malloc_buff=p1->Pmalloc_buff;
DBG("the pread_kbuff->Pmalloc is %s,flag is %d\n",pread_kbuff->Pmalloc_buff,pread_kbuff->flag);
pread_kbuff=pread_kbuff->next;
kfree(p_malloc_buff);
kfree(p1);
#else
DBG("the pread_kbuff->buff_node is %s,flag is %d\n",pread_kbuff->buff_node,pread_kbuff->flag);
pread_kbuff=pread_kbuff->next;
kfree(p1);
#endif
}
}
}
static void init_kbuff_list(void){
list_cnt=0;
pread_kbuff=pirq__kbuff=NULL;
sema_init(&sem, 0);
}
static void __init init_funct(void)
{
KBUFF *p1,*p2;
int USER_SIZE=1;
char kbuffer[20]="zhangfei is best";
init_kbuff_list();
ret = kthread_run(readThread, NULL, "thread_func2");
if (!IS_ERR(ret))
{
printk("kthread_create done\n");
}
else
{
printk("kthread_create error\n");
}
list_cnt++;
while(1){
p2=p1=(KBUFF *)kmalloc(sizeof(KBUFF),GFP_KERNEL);
USER_SIZE+=1;
if(USER_SIZE == 19)
USER_SIZE=1;
#if 1
p1->Pmalloc_buff=(char *)kmalloc(USER_SIZE*sizeof(char),GFP_KERNEL);
memcpy(p1->Pmalloc_buff,kbuffer,USER_SIZE);
#else
memcpy(p1->buff_node,kbuffer,USER_SIZE);
#endif
p1->flag=list_cnt;
if(list_cnt==1 || pread_kbuff == NULL){
pirq__kbuff=pread_kbuff=p1;
printk("the code get %ldth here\n",list_cnt);
}
else
pirq__kbuff->next=p1;
p1->next=NULL;
pirq__kbuff=p1;
up(&sem);
list_cnt++;
if(list_cnt==65536)
list_cnt=2;
DBG("this is the %s-> %d.the flag is %ld\n",__FUNCTION__,__LINE__,list_cnt);
}
}
static void __init clean_funct(void){
KBUFF *p1;
while(pread_kbuff !=NULL){
p1=pread_kbuff;
// DBG("the pread_kbuff->buff_node is %s,flag is %d\n",pread_kbuff->buff_node,pread_kbuff->flag);
pread_kbuff=pread_kbuff->next;
kfree(p1);
}
kthread_stop(ret);
}
module_init(init_funct);
module_exit(clean_funct);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Bob Wang");
MODULE_DESCRIPTION("Dpram Driver Module for NCM Board");
阅读(342) | 评论(0) | 转发(0) |