views:

58

answers:

3

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?

+1  A: 

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.

WhirlWind
+1  A: 

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.

shodanex
A: 

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)

MarkR