Embedded 版 (精华区)
发信人: Zinux (Linux技工), 信区: Embedded_system
标 题: 内核处理程序
发信站: 哈工大紫丁香 (2001年10月26日18:42:28 星期五), 站内信件
发信人: clown (梧桐叶), 信区: UKP
标 题: 内核处理程序
发信站: UNIX编程 (2001年10月26日11:30:13 星期五), 站内信件
//ker.c
#define MODULE
#define __KERNEL__
#define __NO_VERSIONS__
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/malloc.h>
#include <asm/uaccess.h>
static int driver_loaded = 0;
EXPORT_SYMBOL_NOVERS(driver_loaded);
#define MAX_SIZE 32
#define MAX_SIZE 32
struct vir_buf {
int size;
char buf[MAX_SIZE];
struct vir_buf *next;
};
//from the application layer to see the link table
//rd_head link table: kernel->application
//wr_head link table: application->kernel
struct vir_buf *rd_head, *wr_head;
EXPORT_SYMBOL_NOVERS(rd_head);
EXPORT_SYMBOL_NOVERS(wr_head);
int ker_read();
int ker_write();
EXPORT_SYMBOL_NOVERS(ker_read);
EXPORT_SYMBOL_NOVERS(ker_write);
int init_module()
{
rd_head = NULL;
wr_head = NULL;
printk("<1>ker.o loaded, insmod virtual device driver\n");
printk("<1>ker.o loaded, insmod virtual device driver\n");
return 0;
}
void cleanup_module()
{
printk("<1>Uninstall the kernel\n");
return;
}
int ker_read()
{
unsigned long flags;
struct vir_buf *p;
save_flags(flags);
cli();
if(wr_head == NULL) {
restore_flags(flags);
return -EFAULT;
}
while(wr_head != NULL) {
printk("<1>app->ker: %s\n", wr_head->buf);
printk("<1>app->ker: %s\n", wr_head->buf);
p = wr_head;
wr_head = wr_head->next;
kfree(p);
}
restore_flags(flags);
return 0;
}
int ker_write()
{
static int n = 0;
unsigned long flags;
struct vir_buf *p, *q;
if((p = kmalloc(sizeof(struct vir_buf), GFP_KERNEL)) == NULL) {
printk("<1>kmalloc error, no free memory\n");
return -ENOMEM;
}
//Notice: snprintf is not exported by kernel 2.4
sprintf(p->buf, "kernel %d", n);
p->size = strlen(p->buf);
p->next = NULL;
p->next = NULL;
n++;
save_flags(flags);
cli();
if(rd_head == NULL)
rd_head = p;
else {
q = rd_head;
while(q->next != NULL)
q = q->next;
q->next = p;
}
restore_flags(flags);
return 0;
}
--
puke!
技工而已
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.239.152]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.208毫秒