通常,可以同时从多个进程打开串行设备。
可以使用TIOCEXCL
ioctl()
获得独占访问:
if (!ioctl(fd, TIOCEXCL)) {
/* Any further open() on the tty
that is currently open on descriptor fd
will fail with errno == EBUSY
(except for the administrator, with the
CAP_SYS_ADMIN capability) */
}
,并重新允许其他open()
S,
ioctl(fd, TIOCNXCL);
你需要包括<sys/ioctl.h>
的功能,常数为<termios.h>
。
许多Linux串行库已经这样做。某些串行驱动程序可能一次只允许打开一个驱动程序,因此为了确定您所看到的行为的原因,我需要更多详细信息(如何打开串行设备以及使用的串行设备驱动程序,特定)。
个人 - 我相信这是大多数Linux的串行库不 - ,我建议你尝试的ioctl(fd, TIOCEXCL)
,并也许警告用户,如果它失败(返回0,如果成功,-1除此以外);并且在所有情况下都使用ioctl(fd, TIOCNXCL); close(fd);
关闭tty。