Linux file system is asynchronous, that is, write a file is not immediately saved to the media (hard disk, USB stick, etc.), but into the buffer, and so accumulate to a certain extent and then saved to the media. If there is no umount to illegally unplug the U disk, the program is not known, fopen,fwrite and other functions are still returned to the correct, know that the operating system to write media, will prompt I/O error. However, a lot of data will be lost due to this not-so-timely error report.
In fact, the USB driver has made changes to the system configuration file when a USB drive is plugged in and unplugged.
For example, the USB drive driver will be inserted or unplugged in the/proc/scsi/usb-storage-0/0 to remember Attached:yes or No
By looking at this file it is not difficult to detect whether the USB stick is plugged in or unplugged.
Note: Whether the USB drive is plugged in is not related to mount (Mount).
Of course this is not the only way to detect the state of the U-disk. Depending on the settings of the kernel, there may be no/PROC/SCSI directory, but the/sys directory will have a USB directory, plug the U-disk, the directory will be automatically created and disappear, if the above method is not feasible, then you can detect these directories to determine the existence of the U-disk or SD card storage device status,/ sys/directory found in the relevant USB or SD card, there is more detailed device information, including the device factory ID, transmission speed, capacity and so on.
USB drive status detection under Linux