Home > Archive > Unix Programming > March 2007 > Shared Memory........Questions........
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Shared Memory........Questions........
|
|
| Solomon_Man 2007-03-21, 5:41 am |
| All,
I having trouble getting a simple example of a shared memory example
to work;
I have never worked with shared memory and any suggestions/code hints/
examples would be greatly appreciated.
>From my reading you have to use shmget to specify that you need a
specific size of memory to be shared and then you use shmat to attach
it to a specific process.
My thinking, which is flawed, is that the child adding the 1099 to
what I believe the shared integer variable would then be "shared" to
the parent's final output of the procee id and the shared variable
( cout<<"Parent "<<getpid()<<ptheSharedIndex<<endl; ). The value
when ran for the parents final output is not 1099 but is the value
99.
int main (void)
{
int indexId = 0;
if((indexId = shmget(IPC_PRIVATE,sizeof(int),SHM_R|SHM
_W))<0)
{
cout<<"ERROR: indexId ="<<indexId<<endl;
cout<<strerror(errno)<<endl;
exit (1);
}
int ptheSharedIndex;
ptheSharedIndex = (int)shmat(indexId,0,0); //attach to the
current
process
ptheSharedIndex = 99;
int k = 0;
while (k<NUMBER_OF_PROCESSES) //Fire Off Processes
{
pid = 0;
k=k+1;
pid = fork();
if (pid == 0)
{break;}
}
if (pid == 0) /* child process */
{
int indexIdChild = 0;
if((indexIdChild = shmget(key,sizeof(int),SHM_R|
SHM_W))<0)
{
cout<<" CHILD ERROR: indexIdChild
"<<endl;
}
int ctheSharedIndex;
ctheSharedIndex = (int)shmat(indexIdChild,0,0);
*/
/* int ctheSharedIndex;
ctheSharedIndex = (int)shmat(indexId,0,0);*/
cout<<getpid()<<" "<<ptheSharedIndex<<endl;
ptheSharedIndex = 1099;
}
else /* parent */
{
sleep(8);
cout<<"Parent "<<getpid()<<ptheSharedIndex<<endl;
}
exit(0); //End Process (Main)
}
Thanks for the help,
Chris
| |
| Barry Margolin 2007-03-21, 5:41 am |
| In article <1174442950.441346.154420@e65g2000hsc.googlegroups.com>,
"Solomon_Man" <cmgray74@gmail.com> wrote:
> All,
> I having trouble getting a simple example of a shared memory example
> to work;
>
> I have never worked with shared memory and any suggestions/code hints/
> examples would be greatly appreciated.
>
> specific size of memory to be shared and then you use shmat to attach
> it to a specific process.
>
> My thinking, which is flawed, is that the child adding the 1099 to
> what I believe the shared integer variable would then be "shared" to
> the parent's final output of the procee id and the shared variable
> ( cout<<"Parent "<<getpid()<<ptheSharedIndex<<endl; ). The value
> when ran for the parents final output is not 1099 but is the value
> 99.
shmat() returns a pointer. To read and write in the shared memory, you
have to indirect through the pointer.
>
> int main (void)
> {
> int indexId = 0;
>
> if((indexId = shmget(IPC_PRIVATE,sizeof(int),SHM_R|SHM
_W))<0)
> {
> cout<<"ERROR: indexId ="<<indexId<<endl;
> cout<<strerror(errno)<<endl;
> exit (1);
> }
>
> int ptheSharedIndex;
> ptheSharedIndex = (int)shmat(indexId,0,0); //attach to the
> current
> process
int *ptheSharedIndex = shmat(indexId, 0, 0);
> ptheSharedIndex = 99;
*ptheSharedIndex = 99;
>
> int k = 0;
> while (k<NUMBER_OF_PROCESSES) //Fire Off Processes
> {
> pid = 0;
> k=k+1;
> pid = fork();
> if (pid == 0)
> {break;}
> }
>
> if (pid == 0) /* child process */
> {
> int indexIdChild = 0;
> if((indexIdChild = shmget(key,sizeof(int),SHM_R|
> SHM_W))<0)
> {
> cout<<" CHILD ERROR: indexIdChild
> "<<endl;
> }
> int ctheSharedIndex;
> ctheSharedIndex = (int)shmat(indexIdChild,0,0);
> */
> /* int ctheSharedIndex;
> ctheSharedIndex = (int)shmat(indexId,0,0);*/
> cout<<getpid()<<" "<<ptheSharedIndex<<endl;
cout<<getpid()<<" "<<*ptheSharedIndex<<endl;
> ptheSharedIndex = 1099;
*ptheSharedIndex = 1099;
> }
>
> else /* parent */
> {
> sleep(8);
> cout<<"Parent "<<getpid()<<ptheSharedIndex<<endl;
> }
>
> exit(0); //End Process (Main)
> }
>
> Thanks for the help,
> Chris
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Solomon_Man 2007-03-21, 7:05 pm |
| On Mar 21, 12:23 am, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article <1174442950.441346.154...@e65g2000hsc.googlegroups.com>,
>
>
>
>
>
> "Solomon_Man" <cmgra...@gmail.com> wrote:
>
>
>
>
> shmat() returns a pointer. To read and write in the shared memory, you
> have to indirect through the pointer.
>
>
>
>
>
>
>
>
>
>
> int *ptheSharedIndex = shmat(indexId, 0, 0);
>
>
> *ptheSharedIndex = 99;
>
>
>
>
>
>
>
>
>
> cout<<getpid()<<" "<<*ptheSharedIndex<<endl;
>
>
> *ptheSharedIndex = 1099;
>
>
>
>
>
> --
> Barry Margolin, bar...@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
Barry,
Thanks 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 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. There was also a key issue as well as I tried different things
online after seeing a few examples, I gave up on that and went to
IPC_PRIVATE instead of the key.
Again thanks for the help and I will repost with what I come up with
later tonight.
Thanks,
Chris
| |
| Martin Vuille 2007-03-21, 7:05 pm |
| "Solomon_Man" <cmgray74@gmail.com> wrote in
news:1174485085.253200.62810@l75g2000hse.googlegroups.com:
>
> Again thanks for the help and I will repost with what I come up
> with later tonight.
>
BTW, if you are going to post the exact same question
to several groups (which may or may not be a good idea,
depending on the groups) you might want to cross-post the
message as opposed to posting it several times (i.e.,
specify several newsgroup names at once when posting the
message.)
Besides being less work for you, everyone gets to see
everyone else's responses so we don't repeat ourselves, and
some newsreaders may skip over the copies so that people
who follow more than one of the groups don't see the
message multiple times.
MV
--
I do not want replies; please follow-up to the group.
|
|
|
|
|