The file to be mapped is a regular file.
The creation of a mapped data file is a two-step process. First, you create the mapped file. Then, because the shmat subroutine does not provide for it, you must program a method for detecting the end of the mapped file.
if( ( fildes = open( filename , 2 ) ) < 0 ) { printf( "cannot open file\n" ); exit(1); }
file_ptr=shmat (fildes, 0, SHM_MAP);
The SHM_MAP constant is defined in the /usr/include/sys/shm.h file. This constant indicates that the file is a mapped file. Include this file and the other shared memory header files in a program with the following directives:
#include <sys/shm.h>
eof = file_ptr + lseek(fildes, 0, 2);
This example sets the value of eof to an address that is 1 byte beyond the end of file. Use this value as the end-of-file marker in the program.
while ( file_ptr < eof) { . . . (references to file using file_ptr) }
Note: The read and write subroutines also work on mapped files and produce the same data as when pointers are used to access the data.
close (fildes );