我正在研究
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的传递.
我的问题是,为什么我们不把它作为一个全局变量?
任何人都可以提供一些使用这种方法传递数据的实例吗?
解决方法
线程安全!如果两个线程/进程同时使用驱动程序怎么办?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。