About the UART’S lock

原创文章,转载请注明出处.转载自: Li Haifeng's Blog
本文链接地址: About the UART’S lock

When more than one minicom try to connect the /dev/ttyS0, an error “Device /dev/ttyS0 is locked” will be threw out.


How to implement this lock?


In fact, the UART’s lock is not managed by Kernel, and is done by user program. The analysis is shown as below.


If “minicom” uses the /dev/ttyS0, a lock of LCK..TTYS0 is created by minicom. The following is on my PC:


hfli@msh-pc1935:~/temp$ ls -l /var/lock/

total 4

drwxr-xr-x 2 www-data root     40 May  7 11:31 apache2

-rw-r–r– 1 hfli     hfli     11 May  7 14:25 LCK..ttyS0

drwxr-xr-x 2 whoopsie whoopsie 60 May  7 11:31 whoopsie


The content of lock file is defined by program. In LCK..TTYS0, the content is the process number.


hfli@msh-pc1935:~/temp$ cat /var/lock/LCK..ttyS0


hfli@msh-pc1935:~/temp$ ps aux | grep minicom

hfli     17452  0.0  0.0   4368   832 pts/4    S+   16:39   0:00 grep –color=auto minicom

hfli     18642  0.0  0.1   7256  3328 pts/0    S+   14:25   0:01 minicom -b 38400 -D /dev/ttyS0


So, if one minicom captured the /dev/ttyS0, another minicom trying to capture ttyS0 will check whether the /var/lock/LCK..ttyS0 exists.

If LCK..ttyS0 exists, an error will be threw out.


hfli@msh-pc1935:~/temp$ minicom -b 38400 -D /dev/ttyS0

Device /dev/ttyS0 is locked.


And if I deleted /var/lock/LCK..ttyS0 manually, another minicom also can captured the /dev/ttyS0. But both the input and output cann’t work correctly.




From Li Haifeng's Blog, post About the UART’S lock

Post Footer automatically generated by wp-posturl plugin for wordpress.