How do memory-mapped files handle I/O errors?
I am modifying a tool that currently opens files and reads them with fread() to use memory-mapped file开发者_如何学JAVAs. This program frequently reads from devices that may have I/O errors. Currently we catch these with errors returned by fread(). How do I/O errors get reported with memory-mapped files?
The Linux man page referenced by vy32 explicitly states that SIGSEGV
is generated on write failure (e.g. no disk space), but it is unclear whether read failures generate such errors (e.g. when removable media has been physically removed). Wikipedia seems to be more specific on that:
I/O errors on the underlying file (e.g. its removable drive is unplugged or optical media is ejected, disk full when writing, etc.) while accessing its mapped memory are reported to the application as the SIGSEGV/SIGBUS signals on POSIX, and the EXECUTE_IN_PAGE_ERROR structured exception on Windows. All code accessing mapped memory must be prepared to handle these errors, which don't normally occur when accessing memory.
POSIX specification of mmap
does not require that the signal is delivered on error but leaves such possibility for implementations:
An implementation may generate SIGBUS signals when a reference would cause an error in the mapped object, such as out-of-space condition.
Okay, it looks like SIGSEGV or SIGBUS is generated when there is an attempt made to access mapped memory that is not available.
精彩评论