Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 957
vy32 Avatar asked Jul 22 '11 14:07

vy32


People also ask

What is the purpose of memory-mapped file?

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.

What happens in a memory-mapped input output?

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.

What is memory-mapped I 0?

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.


2 Answers

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.

like image 74
Oleg Andriyanov Avatar answered Oct 21 '22 11:10

Oleg Andriyanov


Okay, it looks like SIGSEGV or SIGBUS is generated when there is an attempt made to access mapped memory that is not available.

like image 1
vy32 Avatar answered Oct 21 '22 09:10

vy32