对 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 开始。
关于c - linux内核编程死循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27223443/