在Linux设备驱动程序编程中使用struct inode和struct文件传递数
发布时间:2020-12-13 18:04:43 所属栏目:Linux 来源:网络整理
导读:我正在研究 Linux Device Drivers,3rd edition的第3.5章.本节介绍一种从open函数中的struct inode * inode中我们自己定义的自定义结构的方法: int scull_open(struct inode *inode,struct file *filp){ struct scull_dev *dev; dev = container_of(inode-i_
我正在研究
Linux Device Drivers,3rd edition的第3.5章.本节介绍一种从open函数中的struct inode * inode中我们自己定义的自定义结构的方法:
int scull_open(struct inode *inode,struct file *filp) { struct scull_dev *dev; dev = container_of(inode->i_cdev,struct scull_dev,cdev); filp->private_data = dev; /* for other methods */ } return 0; } 从我的理解,当设备被打开时,代表设备的struct inode * inode被传递给scull_open.然后,提取自定义结构开发并将其传递给filp-> private_data,以便其他方法(如scull_read)可以使用它: ssize_t scull_read(struct file *filp,char _ _user *buf,size_t count,loff_t *f_pos) { struct scull_dev *dev = filp->private_data; /* other codes that uses *dev */ } 这似乎对我来说很好,直到我意识到在scull_setup_cdev here初始化期间我们已经有一个struct scull_dev * dev. 我很困惑,因为我以为我们可以使struct scull_dev * dev成为一个全局变量,那么scull_read和其他方法最终将可以访问它,而不用遍历所有使用inode和file的传递. 我的问题是,为什么我们不把它作为一个全局变量? 任何人都可以提供一些使用这种方法传递数据的实例吗? 解决方法
线程安全!如果两个线程/进程同时使用驱动程序怎么办?
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |