I am modifying a tool that currently opens files and reads them with fread() to use memory-mapped files. 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?
A memory-mapped file contains the contents of a file in virtual memory. This mapping between a file and memory space enables an application, including multiple processes, to modify the file by reading and writing directly to the memory.
Memory-mapped I/O uses the same address space to address both main memory and I/O devices. The memory and registers of the I/O devices are mapped to (associated with) address values. So a memory address may refer to either a portion of physical RAM, or instead to memory and registers of the I/O device.
Memory mapped I/O is an interfacing technique in which memory related instructions are used for data transfer and the device is identified by a 16-bit address. In this type, the I/O devices are treated as memory locations. The control signals used are MEMR and MEMW.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With