Re: Shared Memory...Some Questions.....
- From: Martin Vuille <jpmv27@xxxxxxxxx>
- Date: 21 Mar 2007 11:53:51 GMT
"Solomon_Man" <cmgray74@xxxxxxxxx> wrote in
news: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.
.
- Follow-Ups:
- Re: Shared Memory...Some Questions.....
- From: Solomon_Man
- Re: Shared Memory...Some Questions.....
- References:
- Shared Memory...Some Questions.....
- From: Solomon_Man
- Shared Memory...Some Questions.....
- Prev by Date: Re: Shared Memory...Some Questions.....
- Next by Date: Re: Linux kernel, possible useless continue
- Previous by thread: Re: Shared Memory...Some Questions.....
- Next by thread: Re: Shared Memory...Some Questions.....
- Index(es):
Relevant Pages
|