LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   shared memory between two processes (https://www.linuxquestions.org/questions/linux-software-2/shared-memory-between-two-processes-386468/)

davidtexs3 11-25-2005 12:49 PM

shared memory between two processes
 
I want to be able to share memory between two processes. The background is that I have a framegrabber which locks a region of memory (containing multiple camera frames) and returns pointers to individual frames when a new frame is ready. These indivdual frames must be available to two seperate processes. I currently can't see any way of coercing shmget or shmat to allow me to use memory which I or the framegrabber specifies. For example, I've tried to do an shmget / shmat on a pointer to an array and the call to shmat fails. Is there any solution... I'm sure there must be some way of sharing memory between two processes where the location of the memory is known in advance?

Thanks for any suggestion,
Dave

foo_bar_foo 11-25-2005 02:25 PM

yes you can do shared memory (you have to do the syncronization yourself !)
first process allocates the memory using shget
this would create a new segment available to same user id or provide access by another process
int segment_id = shmget(shm_key, getpagesize(), IPC_CREAT | S_IRUSER | S_IWUSER);
then you attach the shared segment to a certain address space using shmat() with segment_id
returned from shmget like
char *shared_mem = (char*) shmat (segment_id, (void*) 0x5000000, 0);
detatch is
shmdt(shared_mem);
dealocate is
shmctl (segment_id, IPC_RMID, 0);

you can get info like
struct shmid_ds shmbuffer;
shmctl(segment_id, IPC_STAT, &shmbuffer);
int segment_size = shmbuffer.shm_segsz;

the first value passed to shget is the key other processes can use as an identifier to gain acess
if you need to garantee a new segment use the macro IPC_PRIVATE for the key


All times are GMT -5. The time now is 12:37 PM.