Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   structure with an array in shared memory (http://www.programmingforums.org/showthread.php?t=15711)

SAWG Apr 26th, 2008 8:02 PM

structure with an array in shared memory
 
Hi. I'm having difficulties with shared memory.
I have a structure which looks like this:
:

  1. struct sharemem{
  2.   int *actual
  3.   int *end;
  4.   int *sharearr;
  5. }

I use shmget (IPC_PRIVATE, sizeof(struct sharemem), IPC_CREAT | 0777) to get an ID.
Afterwards I use fork(), the child jumps into a function which attaches the shared memory ID of the structure, mallocs the *sharearr pointer(I pass on the size to the function via an argument),and fills it with integers ranging from 1 to (size+1).
The problem comes up when I try to access the array with the parent function(the parent is suspended until the child is done filling the array). The pointers seem to be actually shared, I just can't get access to any of the members of the array.
Could someone please tell me what I'm doing wrong?

The Dark Apr 28th, 2008 1:24 AM

Re: structure with an array in shared memory
 
I haven't used shmget, but I would imagine you need to allocate the sharearr array using smget as well. This means that your structure would need to contain the sharearr id rather than the pointer. Then your calling process would have to call shmat to get a pointer to the memory.


All times are GMT -5. The time now is 4:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC