gpt4 book ai didi

c - Linux 内核字符驱动程序写入调用未按预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 08:32:59 25 4
gpt4 key购买 nike

我正在尝试使用字符驱动程序实现 FIFO。但是,在写入设备时它似乎不起作用。它似乎并没有结束循环。任何帮助或链接表示赞赏。我从许多来源获得了帮助,所以当前的代码有点困惑,有很多不应该像现在这样的东西。

static ssize_t dev_write(struct file *filp, const char *buff, size_t len, loff_t *off) {
int mode;
int ind;
ssize_t count = -ENOMEM;
printk(KERN_ALERT "to be written : %s\n", buff);
mode = iminor(filp->f_dentry->d_inode);
printk(KERN_ALERT "Device minor : %d\n", mode);
if ((mode == 1) || (mode ==3))
return -EINVAL;
if (mode == 0){
count = 0;
ind = 0;
if (buff[ind] == NULL) {
return -ENOMEM;
}
printk(KERN_ALERT "Write position1 : %d\n", writePos1);
while(ind<=len) { //loop untill we have something to writer
if (down_interruptible(&buffer1_e)) { //taking flag first isn't right because that won't allow other guyto give access to our turn.
printk(KERN_ALERT "buffer1 flag didn't work\t %d", buffer1_e.count);
return -ERESTARTSYS;
}
else {
if (down_interruptible(&flag1)){
up(&buffer1_e); //must because we couldn't write it properly
return -EINVAL;
}
else {
queue1[writePos1] = buff[ind];
printk(KERN_ALERT "Write %d %c\n",ind,queue1[writePos1]);
if (writePos1 == 9){
writePos1 = 0;
}
else
writePos1++;
count++;
}
up(&flag1);
}
up(&buffer1_f);
off += count;
ind++;
}
printk(KERN_ALERT "Write position1 now: %d\t and count%d\n", writePos1,count);
return count-1;
}

最佳答案

我刚刚编写了自己的模块,我怀疑您的问题是调用 dev_write 的进程期望 dev_write 返回写入的字节数。如果您没有返回正确的数字(我看到您返回的是 count - 1),dev_write 将被一次又一次地调用。

dev_read 我发现是相似的——直到它返回 0,进程会重复调用它——期待有更多的字符被检索(这是有道理的)。

我已经编写/修改了一个更简单的模块,它说明了使用模块作为字符缓冲区(抱歉,写得太仓促了)。它应该允许您向它回显一个字符串,并在您 cat 或以其他方式读取它时返回该字符串。这在您 make run 时得到演示。我相信您将能够轻松地将其修改为 FIFO。

对于您的内核崩溃或其他问题,我不承担任何责任,无论如何您都应该使用虚拟机。

它很长,所以在我的github上:

git clone https://github.com/n-hutton/tempRepo
cd tempRepo
make
make run

关于c - Linux 内核字符驱动程序写入调用未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27434694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com