Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
IPC - Shared Memory
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
darkpenguin007
n00b
n00b


Joined: 21 Jan 2010
Posts: 22

PostPosted: Wed Nov 27, 2013 3:32 am    Post subject: IPC - Shared Memory Reply with quote

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
View user's profile Send private message
schorsch_76
Guru
Guru


Joined: 19 Jun 2012
Posts: 450

PostPosted: Wed Nov 27, 2013 6:49 am    Post subject: Reply with quote

I just recommend you boost documentation. The Interprocess [1] library describes exatly this [2][3].

[1] http://www.boost.org/doc/libs/1_55_0/doc/html/interprocess.html
[2] http://www.boost.org/doc/libs/1_55_0/doc/html/interprocess/quick_guide.html
[3] http://www.boost.org/doc/libs/1_55_0/doc/html/interprocess/some_basic_explanations.html
Back to top
View user's profile Send private message
aCOSwt
Bodhisattva
Bodhisattva


Joined: 19 Oct 2007
Posts: 2537
Location: Hilbert space

PostPosted: Wed Nov 27, 2013 8:00 am    Post subject: Reply with quote

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
View user's profile Send private message
darkpenguin007
n00b
n00b


Joined: 21 Jan 2010
Posts: 22

PostPosted: Wed Nov 27, 2013 2:55 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
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