gpt4 book ai didi

linux-kernel - Linux 设备驱动程序中的 open 方法是否应该返回文件描述符?

转载 作者:行者123 更新时间:2023-12-04 04:01:53 25 4
gpt4 key购买 nike

我正在学习 Linux 设备驱动程序编程第 3 版,我对 open 方法有一些疑问,这是该书中使用的“scull_open”方法:

int scull_open(struct inode *inode, struct file *filp){
struct scull_dev *dev; /* device information */

dev = container_of(inode->i_cdev, struct scull_dev, cdev);
filp->private_data = dev; /* for other methods */
/* now trim to 0 the length of the device if open was write-only */
if ( (filp->f_flags & O_ACCMODE) == O_WRONLY) {
if (down_interruptible(&dev->sem))
return -ERESTARTSYS;
scull_trim(dev); /* ignore errors */
up(&dev->sem);
}
return 0; /* success */
}

我的问题是:
  • 这个函数不应该向刚打开的设备返回一个文件描述符吗?
  • “*filp”不是这个函数本地的,那为什么我们把dev的内容复制到它里面呢?
  • 我们以后如何在读写方法中使用?
  • 有人可以给我写一个典型的 open 方法的“非胖”实现吗?

    ssize_t scull_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos){
    struct scull_dev *dev = filp->private_data;
    ...}
  • 最佳答案

    用户空间打开函数就是您所想到的,即返回文件描述符 int 的系统调用。很多很好的引用资料,例如 APUE 3.3.

    设备驱动程序“打开方法”是 file_operations 结构中的一个函数。它不同于用户空间“文件打开”。安装设备驱动程序后,当用户代码打开设备(例如访问/dev/scull0)时,将调用此“打开方法”。

    关于linux-kernel - Linux 设备驱动程序中的 open 方法是否应该返回文件描述符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18034508/

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