首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >cat函数无限次调用read()

cat函数无限次调用read()
EN

Stack Overflow用户
提问于 2015-07-22 20:26:05
回答 3查看 3K关注 0票数 1

我正在开发简单的字符设备驱动程序。我已经在模块中实现了读写功能,问题是当我尝试使用cat /dev/devicefile读取设备文件时,它会进入无限循环,即重复读取相同的数据。有人能给我一些解决这个问题的建议吗?下面是我的驱动程序代码。

代码语言:javascript
运行
复制
#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);
EN

回答 3

Stack Overflow用户

发布于 2015-07-22 21:26:10

.read函数还应正确处理其lenoff参数。实现从内存缓冲文件读取的最简单方法是使用simple_read_from_buffer帮助器:

代码语言:javascript
运行
复制
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);
}

出于教育目的,您可以检查该帮助器(在fs/libfs.c中定义)的代码。

顺便说一句,对于你的.write方法,你可以使用simple_write_to_buffer助手。

票数 5
EN

Stack Overflow用户

发布于 2015-07-22 20:37:03

您没有考虑传递给dev_read函数的缓冲区大小,因此您可能会在cat中调用未定义的行为。试试这个:

代码语言:javascript
运行
复制
static ssize_t dev_read( struct file *filp, char *buff, size_t len, loff_t  *off )
{
    size_t count = 0;
    printk( KERN_ALERT"inside read %d\n", *off );
    while( msg[count] != 0 && count < len )
    {
        put_user( msg[count], buff++ );
        count++;
    }
    return count;
}
票数 1
EN

Stack Overflow用户

发布于 2016-10-23 19:20:43

这个问题可以通过正确设置my_read()的第四个参数*off来解决。

第一次需要返回count,第二次以后返回0。

代码语言:javascript
运行
复制
if(*off == 0) {
    while (msg[count] != 0) {
        put_user(msg[count], buff++);
        count++;
        (*off)++;
    }
    return count;
}
else
return 0;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31563107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档