[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs

Creating a Copy-On-Write Mapped Data File with the shmat Subroutine

Prerequisite Condition

The file to be mapped is a regular file.

Procedure

  1. Open (or create) the file and save the file descriptor:
    if( ( fildes = open( filename , 2 ) ) < 0 )
    {
            printf( "cannot open file\n" );
            exit(1);
    }
  2. Map the file to a segment as copy-on-write, with the shmat subroutine:
    file_ptr = shmat( fildes, 0, SHM_COPY );
    The SHM_COPY constant is defined in the /usr/include/sys/shm.h file. This constant indicates that the file is a copy-on-write mapped file. Include this header file and other shared memory header files in a program with the following directives:
    #include <sys/shm.h>
  3. Use file_ptr as a pointer to the start of the data file, and access the data as if it were in memory.
    while ( file_ptr < eof)
    {
          .
          .
          .
          (references to file using file_ptr)
    }
  4. Use the fsync subroutine to write changes to the copy of the file on disk to save the changes:
    fsync( fildes );
  5. Close the file when the program is finished working with it:
    close( fildes );

Related Information

Mapping Files with the shmat Subroutine.

Creating a Mapped Data File with the shmat Subroutine.

The shmat subroutine, shmctl subroutine, shmdt subroutine, shmget subroutine.


[ Previous | Next | Contents | Glossary | Home | Search ]