为什么我们需要文件->私人数据?

发布于 2025-01-20 11:14:18 字数 773 浏览 2 评论 0原文

我目前正在学习linux设备驱动程序,一时无法理解基本的字符设备驱动程序代码。

struct scull_pipe {
       ...
};

static struct scull_pipe *scull_devices;
static int scull_open(struct inode *inode, struct file *filp)
{
    struct scull_pipe *dev;

    dev = container_of(inode->i_cdev, struct scull_pipe, cdev);
    filp->private_data = dev;
    ...
}
static ssize_t scull_write(struct file *filp, const char __user *buf, size_t count,
                loff_t *f_pos)
{
    struct scull_pipe *dev = filp->private_data;
    ...
    if (copy_from_user(dev->wp, buf, count)) {
        ...
    }
}

我的问题是为什么我们需要使用 filp->private_data 中的 scull_devices 而不是直接引用 scull_devices 对象?

I'm currently learning linux device drivers and can't understand one moment from basic character device driver code.

struct scull_pipe {
       ...
};

static struct scull_pipe *scull_devices;
static int scull_open(struct inode *inode, struct file *filp)
{
    struct scull_pipe *dev;

    dev = container_of(inode->i_cdev, struct scull_pipe, cdev);
    filp->private_data = dev;
    ...
}
static ssize_t scull_write(struct file *filp, const char __user *buf, size_t count,
                loff_t *f_pos)
{
    struct scull_pipe *dev = filp->private_data;
    ...
    if (copy_from_user(dev->wp, buf, count)) {
        ...
    }
}

My question is why do we need to use scull_devices from filp->private_data instead of directly referencing scull_devices object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文