猫函数调用read()将无限次 [英] cat function calling read() infinite times

查看:224
本文介绍了猫函数调用read()将无限次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作简单的字符设备驱动程序。我已经实现阅读和模块中写函数,问题是,当我尝试使用读取设备文件猫的/ dev /设备文件它进入无限循环,即读重复同样的数据。有人建议我任何解决这个问题?下面是我的司机code。

 #包括LT&; Linux的/  -  module.h中GT;
#包括LT&; Linux的/ fs.h文件>
#包括LT&; Linux的/ string.h中>
#包括LT&; ASM / uaccess.h中>
#包括LT&; Linux的/ init.h中>
MODULE_LICENSE(GPL);
MODULE_DESCRIPTION(字符设备驱动程序);
MODULE_AUTHOR(SRINIVAS);静态字符味精[100] = {0};静态INT吨;静态INT dev_open(结构的inode *,结构*文件);
静态INT dev_rls(结构的inode *,结构*文件);
静态ssize_t供dev_read(结构文件*,字符*,为size_t,参数loff_t *);
静态ssize_t供dev_write(结构文件*,为const char *,为size_t,参数loff_t *);静态结构的file_operations FOPS =
{
    .read = dev_read,
    。开= dev_open,
    .WRITE = dev_write,
    .release = dev_rls,
};
静态INT himodule(无效)
{
    T = 0;
    T =函数register_chrdev(0,chardevdriver,&安培; FOPS);
    如果(叔℃,)
        printk的(KERN_ALERT设备注册失败\\ n);
    其他
        printk的(KERN_ALERT设备注册成功的\\ n);    printk的(KERN_ALERT重大数字是%D,T);
    返回0;
}
静态无效byemodule(无效)
{
    unregister_chrdev(T,chardevdriver);
    printk的(KERN_ALERT成功注销\\ n);
}静态INT dev_open(结构的inode * inod,结构file * FIL)
{
    printk的(KERN_ALERTdev的内部打开);
    返回0;
}
静态ssize_t供dev_read(结构文件*的filp,字符* BUFF,为size_t LEN,参数loff_t *关闭)
{
    总之计数= 0;
    而(MSG [计数]!= 0){
        put_user(MSG [计数],BUFF ++);
        算上++;
    }
    返回计数;
}静态ssize_t供dev_write(结构文件*的filp,为const char * BUFF,为size_t LEN,参数loff_t *关闭)
{
    总之计数= 0;
    printk的(KERN_ALERT写\\ n的内部);    memset的(味精,0,100);    printk的(KERN_ALERTLEN的大小%ZD,LEN);    而(LEN大于0){
        味精[计数] = BUFF [统计]
        len--;
        算上++;
    }
    返回计数;
}静态INT dev_rls(结构的inode * inod,结构file * FIL)
{
    printk的(KERN_ALERT设备关闭\\ n);
    返回0;
}
宏module_init(himodule);
宏module_exit(byemodule);


解决方案

.read 功能也应该正确处理它 LEN 关闭参数。实现从内存中缓冲的文件中读取的最简单方法是使用 simple_read_from_buffer 助手:

 静态ssize_t供dev_read(结构文件*的filp,字符* BUFF,为size_t LEN,参数loff_t *关闭)
{
    返回simple_read_from_buffer(BUFF,LEN,关闭,味精,100);
}

您可以检查的辅助教育目的(在 FS / libfs.c 的定义)code。

顺便说一句,你的 .WRITE 方法,你可以使用 simple_write_to_buffer 帮手。

I am working on simple character device driver. I have implemented read and write functions in the module, the problem is when I try to read the device file using cat /dev/devicefile it is going into infinite loop i.e. reading the same data repeatedly. Can someone suggest me any solution to this problem? Below is my driver code.

#include<linux/module.h>
#include<linux/fs.h>
#include<linux/string.h>
#include<asm/uaccess.h>
#include<linux/init.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("character device driver");
MODULE_AUTHOR("Srinivas");

static char msg[100]={0};

static int t;

static int dev_open(struct inode *, struct file *);
static int dev_rls(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *,size_t, loff_t *);
static ssize_t dev_write(struct file *, const char *, size_t,loff_t *);

static struct file_operations fops =
{
    .read = dev_read,
    .open = dev_open,
    .write = dev_write,
    .release = dev_rls,
};
static int himodule( void )
{
    t = 0;
    t = register_chrdev(0, "chardevdriver", &fops);
    if (t < 0)
        printk(KERN_ALERT"device registration failed\n");
    else
        printk(KERN_ALERT"device registered successfully\n");

    printk(KERN_ALERT"major number is %d", t);
    return 0;
}
static void byemodule(void)
{
    unregister_chrdev(t, "chardevdriver");
    printk(KERN_ALERT"successfully unregistered\n");
}

static int dev_open(struct inode *inod, struct file *fil)
{
    printk(KERN_ALERT"inside the dev open");
    return 0;
}
static ssize_t dev_read(struct file *filp, char *buff, size_t len, loff_t *off)
{
    short count = 0;
    while (msg[count] != 0) {
        put_user(msg[count], buff++);
        count++;
    }
    return count;
}

static ssize_t dev_write(struct file *filp, const char *buff, size_t len, loff_t *off)
{
    short count = 0;
    printk(KERN_ALERT"inside write\n");

    memset(msg,0,100);

    printk(KERN_ALERT" size of len is %zd",len);

    while (len > 0) {
        msg[count] = buff[count];
        len--;
        count++;
    }
    return count;
}

static int dev_rls(struct inode *inod,struct file *fil)
{
    printk(KERN_ALERT"device closed\n");
    return 0;
}
module_init(himodule);
module_exit(byemodule);

解决方案

.read function should also correctly process its len and off arguments. The simplest way to implement reading from memory-buffered file is to use simple_read_from_buffer helper:

static ssize_t dev_read(struct file *filp, char *buff, size_t len, loff_t *off)
{
    return simple_read_from_buffer(buff, len, off, msg, 100);
}

You can inspect code of that helper (defined in fs/libfs.c) for educational purposes.

BTW, for your .write method you could use simple_write_to_buffer helper.

这篇关于猫函数调用read()将无限次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆