开发者

Issues with mmap of Complex Types

I have an issue attempting to access shared memory using mmap for complex types.

So I allocate my memory as so in my parent pr开发者_如何学编程ocess:

/* Create mmap file */
fid = open(TMP_FILE_NAME, O_RDWR | O_CREAT | O_EXCL, (mode_t) 0755);
if (fid < 0)
{
  printf("Bad Open of mmap file <%s>\n", TMP_FILE_NAME);
  die(-1);
}

/* Make mmap file Buffer Size */
status = ftruncate(fid, INPUT_BUFFER_SIZE); 
if (status)
{
    printf("Failed to ftruncate the file <%s>, status = %d\n", TMP_FILE_NAME, status);
    die(-1);
}

/* Initialize Shared Memory */
mmap_ptr = mmap((caddr_t) 0,
      INPUT_BUFFER_SIZE,        // Default Buffer Size
      PROT_WRITE | PROT_READ,   // R/W Permissions
      MAP_SHARED, // No file backing
      fid,  
      (off_t) 0);
if (mmap_ptr == MAP_FAILED)
{
    printf("Failed to perform mmap, Exiting\n");
    die(-1);
}

Now the Struct that I'm passing in memory to my child process is as follows:

/* Data structue for IPC */
typedef struct {
    int current_active_id;
    int consume_remaining;
    Queue buffer;
} input_buffer;

where Queue is a data structure class from the following: http://www.idevelopment.info/data/Programming/data_structures/c/Queue/Queue.shtml

In my child process it's okay when I do this, it returns the correct value:

printf("Got here... Shared Mem: %d\n", input_queue->consume_remaining);

but when I do something like:

IsEmpty(input_queue->buffer)

it crashes and in the code of the Queue it's only doing this:

return Q->Size == 0;

Any help would be appreciated, thanks!!


Queue is a pointer to struct QueueRecord, and should be allocated as such, presumably using the same shared memory segment. note that this should also be mapped at the same address in both parent and child, or you will not be able to dereference it.


The structure you are putting in the map contains pointers. The pointers are all relative to the address space of the process that created them.

If the other process doesn't mmap at the same address, or if it does but the allocations made for the queue aren't taken from inside that buffer, the pointers will be invalid in the other process.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜