内核加载模块代码:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/errno.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>
#define PINFO(fmt,args...) printk(KERN_INFO "<kernel>[%s]" fmt,__FUNCTION__ ##args)
#ifdef DEBUG_SWITCH
#define PDEBUG(fmt,args...) printk(KERN_DEBUG fmt,##args)
#define PERR(fmt,args...) printk(KERN_ERR fmt,##args)
#else
#define PDEBUG(fmt,args...)
#define PERR(fmt,args...)
#endif
#define SIZE 100
int major=0;
int minor=0;
unsigned int devno;
struct test_driver{
char buf[SIZE];
int irq;
struct cdev my_cdev;
// wait_queue_head_t rq;
int cur_size;
struct fasync_struct *fa;
};
struct test_driver mdr;
int test_fasync(int fd, struct file *flip, int on);
int test_open (struct inode *inod, struct file *filp)
{
printk("init test driver/n");
struct test_driver *md=container_of(inod->i_cdev,struct test_driver,my_cdev);
filp->private_data=md;
return 0;
}
int test_close(struct inode *inod, struct file *filp)
{
printk("close test driver/n");
filp->f_pos=0;
// test_fasync(-1, filp, 0);
return 0;
}
ssize_t test_read (struct file *filp, char __user *buf, size_t count, loff_t *offset)
{
struct test_driver *md=filp->private_data;
int remain,ret;
// wait_event(md->rq,md->cur_size>0);//wait_event_interruptible
// if(wait_event_interruptible(md->rq,md->cur_size>0))
// return -ERESTARTSYS;
remain=copy_to_user(buf,md->buf+filp->f_pos,count);
// memcpy(buf,"abd/n",count);
ret=count-remain;
md->cur_size-=ret;
return ret;
}
ssize_t test_write (struct file *filp, const char __user *buf, size_t count, loff_t *offset)
{
struct test_driver *md=filp->private_data;
int remain,ret;
remain=copy_from_user(md->buf+filp->f_pos,buf,count);
md->cur_size+=ret;
kill_fasync(&md->fa,SIGIO,POLL_IN);
// wake_up(&md->rq);
return ret;
}
int test_fasync(int fd, struct file *flip, int on)
{
struct test_driver *md=flip->private_data;
return fasync_helper(fd,flip,on,&md->fa);
}
struct file_operations f_ops=
{
.open=test_open,
.release=test_close,
.read=test_read,
.write=test_write,
.fasync=test_fasync,
};
int init_test(void)
{
if(major){
devno=MKDEV(major,minor);
register_chrdev_region(devno,1,"new test driver");
}
else{
alloc_chrdev_region(&devno,0,1,"alloc devno");
major=MAJOR(devno);
minor=MINOR(devno);
printk("major is %d,minor is %d/n",major,minor);
}
cdev_init(&mdr.my_cdev,&f_ops);
// init_waitqueue_head(&mdr.rq);
cdev_add(&mdr.my_cdev,devno,1);
PINFO("init test driver/n");
return 0;
}
void exit_test(void)
{
cdev_del(&mdr.my_cdev);
unregister_chrdev_region(devno,1);
PINFO("exit test driver/n");
}
module_init(init_test);
module_exit(exit_test);
第一个应用程序在后台运行用于读:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
int fd;
char buf[20];
static int flag=0;
void handler(int num)
{
if(num==SIGIO)
{
int ret;
ret=read(fd,buf,5);
buf[5]='/0';
printf("app read,%d,%s/n",ret,buf);
flag=1;
}
}
int main(void)
{
int i;//在测试的 时候不知道 为什么必须在这一行加一个变量,如果不加会出现段错误
struct sigaction sa;
fd=open("/dev/test",O_RDWR);
if(fd<0)
{
perror("open");
return -1;
}
printf("fd=%d/n",fd);
sa.sa_handler=handler;
sigaction(SIGIO, &sa, NULL);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)| O_ASYNC);
while(1){
printf("continue================/n");
sleep(5);
if(flag==1)
{
printf("2222222222222223445/n");
break;
}
}
printf("asf%s/n",buf);
close(fd);
return 0;
}
以上两个程序运行好后,运行如下程序,进行写操作:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
int main(void)
{
int fd;
fd=open("/dev/test",O_RDWR);
if(fd<0){
perror("open");
return -1;
}
printf("fd=%d/n",fd);
write(fd,"writeab",10);
// close(fd);
return 0;
}