tags:

views:

178

answers:

2

The following is code I've used to create a memory mapped file:

    fid = open(filename, O_CREAT | O_RDWR, 0660);

    if ( 0 > fid )
    {
       throw error;
    }

   /* mapped offset pointer to data file */

   offset_table_p = (ubyte_2 *) shmat(fid, 0, SHM_MAP);

   /* Initialize table */

   memset(offset_table_p, 0x00, (table_size + 1) * 2);

   say table_size is around 2XXXXXXXX bytes,

During debug, I've noticed it fails while attempt to initializing the 'offset table pointer',

Can anyone provide me some inputs on why it's failing during intilalization? is there any possibilities that my memory map file was not created with required table size?

A: 

First things first:

Examine the file both before and after the open() call. If on Linux, you can use the code:

char paxbuff[1000];                            // at start of function
sprintf (paxbuff,"ls -al %s",filename);
system (paxbuff);
fid = open(filename, O_CREAT | O_RDWR, 0660);  // this line already exists.
system (paxbuff);

Then, after you call shmat(), check the return values and size thus:

offset_table_p = (ubyte_2 *) shmat(fid, 0, SHM_MAP);  // already exists.
printf ("ret = %p, errno = %d\n",offset_table_p,errno);
printf ("sz = %d\n",table_size);

That should be enough to work out the problem.

paxdiablo
+2  A: 

As far as I can tell from reading documentation, you are doing it completely wrong.

Either use open() and mmap() or use shmget() and shmat().

If you use open() you will need to make the file long enough first. Use ftruncate() for that.

Zan Lynx
Good catch, upvoted.
paxdiablo