- 论坛徽章:
- 0
|
原帖由 guotie 于 2005-12-21 16:29 发表
提个问题,proc中读函数中的内存如何释放,还是一直都不会被释放?
类似于这个程序中的buffer,好像只能使用堆栈的空间吧,如果使用kmalloc,如何释放其空间?
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/sched.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("mq110");
static char *buf;
static int ps_read(char *page, char **start, off_t offset,int count, int *eof, void *data)
{
char tmp[128];
struct task_struct *p;
rwlock_t tasklist=RW_LOCK_UNLOCKED;
if(offset >0)
return 0;
memset(buf,0,sizeof(buf));
read_lock(&tasklist);
for_each_process(p)
{
sprintf(tmp,"%d\t\t%d\t\t\t%s\n",p->pid,p->parent->pid,p->comm);
strcat(buf,tmp);
memset(tmp,0,sizeof(tmp));
}
read_unlock(&tasklist);
*start=buf;
return strlen(buf);
}
static __init int ps_init(void)
{
struct proc_dir_entry *entry;
buf = (char *)kmalloc(1024*8,GFP_KERNEL);
if(buf == NULL)
goto error1;
entry = create_proc_entry("_ps", 0444, &proc_root);
if(entry == 0)
goto error2;
entry->mode = S_IFREG | 0444;
entry->size = 0;
entry->read_proc = ps_read;
return 0;
error2:
kfree(buf);
printk(KERN_ERR "create_proc_entry() failed !\n");
return -1;
error1:
printk(KERN_ERR "kmalloc() failed !\n");
return -1;
}
static __exit void ps_cleanup(void)
{
remove_proc_entry("_ps", &proc_root);
kfree(buf);
}
module_init(ps_init);
module_exit(ps_cleanup); |
|