Re: Shared Memory...Some Questions.....
- From: "Solomon_Man" <cmgray74@xxxxxxxxx>
- Date: 21 Mar 2007 06:47:43 -0700
On Mar 21, 7:53 am, Martin Vuille <jpm...@xxxxxxxxx> wrote:
"Solomon_Man" <cmgra...@xxxxxxxxx> wrote innews:1174442771.089368.296510@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:
I'm by no means a shared memory expert...
if (pid == 0) /* child process */
{
int indexIdChild = 0;
if((indexIdChild =
shmget(key,sizeof(int),SHM_R|SHM_W))<0)
{
cout<<" CHILD ERROR: indexIdChild "<<endl;
}
A few things here...
Where does "key" come from? I did not see it declared prior to this
point. Same with
Attached memory segments are inherited across a fork, so you
shouldn't have to repeat the shmget/shmat in the children.
You specified IPC_PRIVATE as the key when you first called shmget
before the fork. If you want to be able to shmget the same segment
in another process, you neet to use the same key in all shmgets and
that key cannot be IPC_PRIVATE.
int ctheSharedIndex;
ctheSharedIndex = (int)shmat(indexIdChild,0,0);
*/
/* int ctheSharedIndex;
ctheSharedIndex = (int)shmat(indexId,0,0);*/
cout<<getpid()<<" "<<ptheSharedIndex<<endl;
ptheSharedIndex = 1099;
As already pointed out, this is incorrect. shmat returns a pointer
to the shared segment. If you are not comfortable with pointers, I
suggest that you do some experimentation in a simple program
without shared memory first. Any decent C tutorial should explain
pointers.
shmat returns a "generic" pointer (void*) because it has no way of
knowing what you plan to use the segment for. In your case, since
you are using it to store an int, you need to store the return
value of shmat into an "int*", i.e., a pointer to an integer, so:
int* pTheSharedIndex = shmat( ... )
Then, to store a value there you would dereference the pointer:
*pTheSharedIndex = 1099;
which means "store 1099 in the integer that pTheSharedIndex point
to".
MV
--
I do not want replies; please follow-up to the group.
All thanks so much for the help. I definetly was flawed on my way of
thinking for this example. :)
I will recode the small test app and try and get it to work from all
your suggestions.
After posting this originally, I found a few examples checking the
return value of shmat ( == (void *)-1) and figured that I definetlly
had some issue with my original idea. Then I also read on a man page
at home where the child processes inherits the shared memory after a
fork. The key issue was a idea I tried after seeing a few examples
online, I gave up on that and went to IPC_PRIVATE instead of the key.
Again thank for the help and I will repost with what I come up with
later tonight.
Thanks,
Chris
.
- References:
- Shared Memory...Some Questions.....
- From: Solomon_Man
- Re: Shared Memory...Some Questions.....
- From: Martin Vuille
- Shared Memory...Some Questions.....
- Prev by Date: Re: Linux kernel, possible useless continue
- Next by Date: hi...iam a student from nit warangal india plz help me in linux encrypted file system project...
- Previous by thread: Re: Shared Memory...Some Questions.....
- Next by thread: hi...iam a student from nit warangal india plz help me in linux encrypted file system project...
- Index(es):
Relevant Pages
|