c - linux内核编程死循环

标签 c linux kernel

对 linux 内核编程完全陌生,想知道为什么我的设备读写会抛出一个无限循环:

echo "hi" > simpchar
cat simpchar

static ssize_t device_read(struct file *filp, char *buff,
        size_t len, loff_t * off)
{
    int bytes_read = 0;
    int idxr = 0;
    while (len && (msg[idxr] != 0)) {
        put_user(msg[idxr], buff++);
        len--;
        bytes_read++;
        idxr++;
    }   
    return bytes_read;
}

static ssize_t device_write(struct file *filp, const char *buff,
        size_t len, loff_t * off)
{
    int bytes_read = 0;
    memset(msg, 0, BUF_LEN);
    int idxr = 0;
    while (len > 0) {
        msg[idxr++] = buff[idxr++];  
        len--;
        bytes_read++;
    }   
    return bytes_read;
}

最佳答案

您在这里缺少的是对 loff_t * off 的更新。 这是文件描述符跟上您在文件中的位置的方式。

在您的代码中,每次调用 read()/write() 总是从偏移量 0 开始。

参见 how-to-test-your-own-linux-module

关于c - linux内核编程死循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27223443/

相关文章:

c - 涉及多个 .c 文件时如何处理全局变量

c - 使用多个文件引用全局变量时出错

c - 访问以字节形式存储在内存地址中的值

linux - 在 debian 上升级内核

c - 编译内核模块时如何解决函数名冲突

c - getPath() Dijkstra 算法(C 语言)

linux - 更改主机名 Vagrant

linux - 一些文本替换问题。请指教

android - 如何从命令行在 Ubuntu Linux 上打开 AVD 管理器?

Windows 内核条件断点未评估