0

In Linux, applications normally lock serial ports for exclusive use by creating a corresponding lock file in /var/lock/ with a name something like LCK..ttyS1.

Does Mono implement this locking of serial ports? I found this bug report which suggests it doesn't.

Does Mono provide a library for locking other Linux devices, e.g. a custom char device /dev/custom-char-device via /var/lock/LCK..custom-char-device?

(Note: now I'm not even sure what is the "right way" to lock serial ports in Linux; see my other question about that.)

Community
  • 1
  • 1
Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
  • http://stackoverflow.com/questions/5944004/c-sharp-serialport-open-does-not-throw-an-exception-if-port-is-already-open-in – Craig McQueen Jan 14 '16 at 00:06

1 Answers1

0

As far as I can tell, using flock() locking of serial ports or other devices is the way to go in Linux (following Debian's lead in Debian bug #734086).

I found an advisory locking example in Mono C#, however that uses fcntl() locking rather than flock() locking, and these two methods are different in Linux.

So I think it would be necessary to use a native helper library to provide flock(), which is called via Interop with Native Libraries. The flock() function needs to be passed a native file descriptor, which can be obtained from a FileStream via fs.SafeFileHandle.DangerousGetHandle().

Community
  • 1
  • 1
Craig McQueen
  • 41,871
  • 30
  • 130
  • 181