UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-11-25 12:07 AM

darthsidious
Member
Registered: 2007-11-18
Posts: 14

Re: shared memory, send struct

hello, my question is: how can i send a struct through a shared memory segment? i've two different kinds of structs to send through just one shared memory. It's driving me crazy, i can't find any real example, everything i see on the web is to send chars.

ps. if you have some website or tutorial about shared memory on linux with Semaphores, it would be fantastic.

thanks!!

Offline

#2 2007-11-25 12:38 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: shared memory, send struct

Shared memory itself isn't used to send stuff around, it's just to share data. You sure you mean shared memory and not something else, like message queues or something?

To actually send stuff around with shared memory you'd probably want to have some queue implementation on top of it I suppose, though synchronized access to a shared structure seems more fitting.

What is the problem you're trying to solve? What do you you try to achieve?

Taking a step back often makes it's much more clear what the right path forward is...

Offline

#3 2007-11-25 01:41 AM

darthsidious
Member
Registered: 2007-11-18
Posts: 14

Re: shared memory, send struct

yes, i wanted to say share data, so, how do i share a struct using shared memory? if i have a char i could use strcpy or fprintf, but i don't know hot to "share" a struct. Is it possible to use memcpy?


thanks!!!

Offline

#4 2007-11-25 04:27 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: shared memory, send struct

Sure, you can just do a memcpy(pointerToSharedMemory, &myStruct, sizeof(myStruct)) to copy the struct.  (Note that you have to be careful here, if any of the members of your struct are pointers, the data the pointers point to won't get copied, only the pointers themselves... which is probably not what you want)

darthsidious;23152 wrote:

yes, i wanted to say share data, so, how do i share a struct using shared memory? if i have a char i could use strcpy or fprintf, but i don't know hot to "share" a struct. Is it possible to use memcpy?


thanks!!!

Offline

#5 2007-11-25 10:34 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: shared memory, send struct

Or allocate the struct from the start in the shared memory segment. You need to do your own memory handling anyway when using shared memory, but you can cast the thing you get to anything you want. It's just a chunk of memory multiple processes can access, that's all. The only special thing is that you need to think about concurrency, so that's what the semaphore probably is for, to serialize access to the shared data.

Offline

#6 2010-03-19 02:31 PM

aseychell
Member
Registered: 2010-03-19
Posts: 2

Re: shared memory, send struct

thanks for the info re. memcpy i3839 ...

however, can you help please when having a struct with a pointer in it, how is it best to share it?

Basically, my problem is that i have a linked list made of structs and a struct that holds a pointer to the first and last elements of this list.  Does anyone has any suggestions please?

Thanks!!

Offline

#7 2010-03-19 08:08 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: shared memory, send struct

You really can't do it, without a redesign of your storage struct...  Pointers just aren't
sharable between processes...

You could convert the linked list into a simple array for storing in the shared memory
segment...  But, if it's the sort of thing you need to regularly modify on the fly, this
may not be ideal...  You could instead replace the pointers with index values, which
may make changing links easier to do, while still retaining a flat array for storage
purposes...  But, it could also get ugly if you need to delete nodes from the list, since
you'd need to shift all following nodes down a slot, and adjust their indices everywhere...
Or, you could just mark the deleted nodes as deleted somehow, but leave them in
the same spot in the shared memory segment; and, when you need to add a new
one, first do a scan for any deleted nodes you can reuse, and if none are present,
add a new one at the end...

Offline

#8 2010-03-21 08:52 AM

aseychell
Member
Registered: 2010-03-19
Posts: 2

Re: shared memory, send struct

Thanks RobSeace.

Seems like i have to change everything in my application since i implemented all the lists as linked lists and didn't think it is such a problem if i use them for sharing.

Offline

Board footer

Powered by FluxBB