View previous topic :: View next topic |
Author |
Message |
darkpenguin007 n00b
Joined: 21 Jan 2010 Posts: 22
|
Posted: Wed Nov 27, 2013 3:32 am Post subject: IPC - Shared Memory |
|
|
I've been experimenting with using shared memory, and have gotten it to work testing with an int, but I can not get it to work trying to share a custom class. Here are code segments from both processes.
Process 1:
Code: |
STATUS* c_Status_One;
STATUS c_Status_Two;
i_shmid = shmget((key_t) 1235, sizeof(STATUS), 0666 | IPC_CREAT);
v_shared_status = shmat(i_shmid, (void *)0, 0);
memcpy(v_shared_status, &c_Status_Two, sizeof(STATUS));
c_Status_One = static_cast<STATUS*>(v_shared_status);
c_Status_One->set_s_error("First");
|
Process Two:
Code: |
STATUS* c_Shared_Status;
int i_shmid;
void *v_shared_status = (void *)0;
i_shmid = shmget((key_t) 1235, sizeof(STATUS), 0666 | IPC_CREAT);
v_shared_status = shmat(i_shmid, (void *)0, 0); // Assign the shared memory to the null pointer
c_Shared_Status = static_cast<STATUS*>(v_shared_status); // Assign the null pointer to our shared class
c_Shared_Status->set_s_error("Second");
|
Granted, these are partial selections of code. set_s_error just sets a string member in my class. In these examples, I am setting s_error to a value and attempting to set it to a different value in the second process. In process_one I test the value before spawning the second process and after. In this example the string is unchanged.
I've used the same sections of code, just replacing the instances of my STATUS class with an int and it works. Anybody have a suggestion? Be gentle, just trying to learn to use shared memory. |
|
Back to top |
|
|
schorsch_76 Guru
Joined: 19 Jun 2012 Posts: 450
|
|
Back to top |
|
|
aCOSwt Bodhisattva
Joined: 19 Oct 2007 Posts: 2537 Location: Hilbert space
|
Posted: Wed Nov 27, 2013 8:00 am Post subject: |
|
|
1/ Why do both processes IPC_CREAT ?
2/ You actually should consider testing the shmget return value before shmatting.
3/ I would encourage you not to hardcode the key value. _________________
|
|
Back to top |
|
|
darkpenguin007 n00b
Joined: 21 Jan 2010 Posts: 22
|
Posted: Wed Nov 27, 2013 2:55 pm Post subject: |
|
|
Thanks guys for the reply. I'll definitely take a look at the boost libraries. To be honest, and this might be an incorrect assumption, I've just been using the standard libraries to learn with. It should work. I'll definitely take a look at those in the future.
As to why both processes use IPC_CREAT, that is the way the examples I have been using have it. I can see why the 2nd wouldn't need that. Once it is working I will definitely experiment some more.
When I began testing with this, I was checking the return value of shmid and shmat with something similar to this:
Code: |
if((i_shmid = shmget((key_t) 7000, sizeof(STATUS), 0666 | IPC_CREAT)) == -1)
{
fprintf(stderr, "shget failed\n");
exit(1);
}
|
I stripped those out, along with manually setting the key value, in an attempt to simplify the code to get it working. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|