io模型_阻塞io实现

应用层.c

#include
#include
#include 
#include 
#include 
#include
#include


int main(int argc, char const *argv[])
{
    int a,b;
    char buf[128]={0};
    int fd=open("/dev/mycdev",O_RDWR);
    if(fd<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while(1)
    {
        memset(buf,0,sizeof(buf));
        read(fd,buf,sizeof(buf));
        printf("buf:%s\n",buf);
        
    }
    
    close(fd);

    return 0;
}

应用层模拟硬件数据发来中断.c

#include
#include
#include 
#include 
#include 
#include
#include


int main(int argc, char const *argv[])
{
    int a,b;
    char buf[128]="hello world";
    int fd=open("/dev/mycdev",O_RDWR);
    if(fd<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    write(fd,buf,sizeof(buf));
    close(fd);

    return 0;
}

驱动文件.c

#include 
#include 
#include
#include
#include
#include

struct class *cls;
struct device *dev;

unsigned int major;//定义一个变量保存主设备号
char kbuf[128]={0};

//定义等待队列头
wait_queue_head_t wq_head;
int condition=0;//数据是否准备好的标志变量

//封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

ssize_t mycdev_read(struct file *file, char  *ubuf, size_t size, loff_t *lof)
{
     printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
      if(size>sizeof(kbuf))//用户的需求内核满足不了
     {
        size=sizeof(kbuf);
     }
     wait_event_interruptible(wq_head,condition);//将进程切换为休眠
      long ret;
     ret=copy_to_user(ubuf,kbuf,size);
     if(ret)
     {
        printk("copy_to_user filed\n");
        return -EIO;
     }
     condition=0;//表示下一次数据没有准备好
    return 0;
}
ssize_t mycdev_write(struct file *file, const char  *ubuf, size_t size, loff_t *lof)
{
     printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
     if(size>sizeof(kbuf))//用户的需求内核满足不了
     {
        size=sizeof(kbuf);
     }
      long ret;
     ret=copy_from_user(kbuf,ubuf,size);//表示模拟硬件数据就绪
     if(ret)
     {
        printk("copy_from_user filed\n");
        return -EIO;
     }
     condition=1;
     wake_up_interruptible(&wq_head);//唤醒休眠的进程
     
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
     printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
//定义一个操作方法结构体变量并且初始化
struct file_operations fops={
    .open=mycdev_open,
    .release=mycdev_close,
    .read=mycdev_read,
    .write=mycdev_write,
};
static int __init mycdev_init(void)
{
    //初始化等待队列头
    init_waitqueue_head(&wq_head);
    //注册字符设备驱动
    major=register_chrdev(0,"mychrdev",&fops);
    if(major<0)
    {
        printk("注册字符设备驱动失败\n");
        return major;
    }
    printk("注册字符设备驱动成功major=%d\n",major);
    // 向上提交目录
    cls = class_create(THIS_MODULE, "myled");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录信息成功\n");
    // 向上提交设备节点信息
   
        dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mycdev");
        if (IS_ERR(dev))
        {
            printk("向上提交设备节点信息失败\n");
            return -PTR_ERR(dev);
        }
    printk("向上提交设备节点成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{
    // 销毁节点信息
    
        device_destroy(cls, MKDEV(major, 0));
  
    // 销毁目录信息
    class_destroy(cls);
    //注销字符设备驱动
    unregister_chrdev(major,"mychrdev");

}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

 

 

你可能感兴趣的:(java,linux,算法)