你好,我需要一些帮助,我的代码为学校。Linux内核写入()和读取()函数
阅读功能永远不应该return 0所以老师告诉我们使用wait_event_interruptible。
我的问题是,当我想尝试使用(对于〔实施例)在控制台命令:
echo 1234 > ringdev # my character device
echo 5 > ringdev
cat ringdev
我只得到一个结果:5。
我希望得到一个结果:
1234 5。
全局变量:
static char ringdev_buf[4096];
static size_t ringdev_len;
读取功能:
static ssize_t ringdev_read(struct file *filp, char __user *buf, size_t count,
loff_t *off)
{
ssize_t ret = 0;
while(1) {
wait_event_interruptible(head,ringdev_len!=0);
mutex_lock(&ringdev_lock);
if(ringdev_len!=0) {
ret = -EFAULT;
if (copy_to_user(buf, ringdev_buf,ringdev_len)) { // I was trying everything in the ringdev_len position.
ret = ringdev_len;
goto out_unlock;
}
}
mutex_unlock(&ringdev_lock);
}
out_unlock:
mutex_unlock(&ringdev_lock);
return ret;
}
我试图把任何值计数功能copy_to_user(buf, ringdev_buf,count)但结果总是相同的。
static ssize_t ringdev_write(struct file *filp, const char __user *buf,
size_t count, loff_t *off)
{
ssize_t ret=0;
mutex_lock(&ringdev_lock);
ret=-EFAULT;
if(ringdev_len + count < sizeof(ringdev_buf)) {
if (copy_from_user(ringdev_buf+ringdev_len, buf, count)==0) {
ringdev_len=ringdev_len+count;
ret=count;
wake_up_interruptible(&head);
goto out_unlock;
}
} else {
ret=-ENOSPC;
}
out_unlock:
mutex_unlock(&ringdev_lock);
return ret;
}
2016-12-28
Akihito
+0
阅读应该复制给用户整个要读取的缓冲区,而不只是一个字符。当你等待等待后情况仍然不满意时,请小心;在这种情况下,你应该返回-EINTR。在写作方面,事情实际上看起来很好 –
+0
我们还希望1234 \ n5 \ n是echo命令后面的内容(cat会在单独的行中显示1234和5) –
+0
@PaulStelian发布一个答案:p。 –