Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1729941
  • 博文数量: 782
  • 博客积分: 2455
  • 博客等级: 大尉
  • 技术积分: 4140
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-06 21:37
个人简介

Linux ,c/c++, web,前端,php,js

文章分类

全部博文(782)

文章存档

2015年(8)

2014年(28)

2013年(110)

2012年(307)

2011年(329)

分类:

2012-02-10 16:45:58

下面这个程序和上几篇的一样,不过是内核中实现的。可以动态的分配大小的链表结构体,就是加载后会死
#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");
阅读(337) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~