Method for detecting whether a USB disk is connected under Linux system

  
                  

The file system of Linux is asynchronous, that is to say, writing a file is not immediately saved to the medium (hard disk, U disk, etc.), but stored in the buffer, and then accumulated to a certain extent and then saved to the medium together. . If there is no umount, the U disk is illegally pulled out, the program does not know, fopen, fwrite and other functions are still returned correctly, knowing that the operating system should write the media, it will prompt I /O error. However, a lot of data will be lost due to this untimely error report.

In fact, the USB driver modified the system configuration file when the USB disk was inserted and removed.

For example, the U disk driver will insert Attached: Yes or No in the /proc/scsi/usb-storage-0/0 when inserting or unplugging. By viewing this file, It is not difficult to detect if the USB flash drive is inserted or removed.

Note: Whether the U disk is inserted or not is not related to mount.

Copyright © Windows knowledge All Rights Reserved