我可以在 Linux 下从用户程序同时打开我自己的设备驱动程序两次吗?
我在某处读到,两次打开同一个文件具有未定义的语义,应该避免。在我的情况下,我想多次打开我自己的设备,将多个文件描述符关联到它。我的设备的文件操作都是安全的。 Linux 在系统调用 open() 和调用注册文件操作 .open() 之间是否存在不安全的部分?
Somewhere I read that opening the same file twice has an undefined semantics and should be avoided. In my situation I would like to open my own device multiple times associating multiple file descriptors to it. The file operations of my device are all safe. Is there some part of Linux between the sys call open() and the point it calls the registered file operation .open() that is unsafe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
打开同一个设备文件两次是完全可以的,只要你的驱动程序可以接受。
如果内核中是安全的,则不存在会使其不安全的隐藏部分。
例如,某些视频应用程序使用一个进程进行显示或捕获,而另一个进程打开设备文件来处理控件。
如果驱动程序不支持多次打开,那么当第二次打开发生时它应该返回错误。
It is perfectly fine to open the same device file twice, as long as your driver is ok with that.
There is no hidden part that would make it unsafe if it is safe in the kernel.
For example, some video application use one process to do the display or capture, while another opens the device file to handle the controls.
If the driver does not support multiple open, then it should return an error when a second open happens.
如果驱动程序允许您这样做,您可以在同一进程中打开设备两次。同步是驾驶员的责任。
但是,如果您以特权用户身份打开原始磁盘设备,您将需要确保不会在进程中破坏您自己的数据。
You may open a device twice in the same process, if the driver will let you do so. Synchronization is the responsibility of the driver.
However, if you are opening, say, a raw disk device as a privileged user, you will want to make sure you don't clobber your own data in your process.
在有意义的情况下,打开同一个文件两次具有明确定义的语义。如果进程都在进行读/写操作,那么它们仍然需要某种形式的同步,否则文件最终可能会充满垃圾。
对于设备驱动程序,多次打开的语义完全取决于驱动程序 - 某些驱动程序禁止它,而在其他驱动程序中它工作正常(例如 /dev/null)。在某些驱动程序中,它具有非常特殊的含义(例如声卡可能会混合多个应用程序之间的声音输出)
Opening the same file twice has well-defined semantics in cases which make sense. Processes still need some form of synchronisation if they're all doing read/write, otherwise the file is likely to end up full of rubbish.
For a device driver, the semantics of multiple opens is entirely up to the driver - some drivers prohibit it, in others it works fine (think /dev/null for instance). In some drivers it has a very special meaning (e.g. sound cards may mix the sound output between multiple apps)