LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2003, 12:28 PM   #1
overbored
Member
 
Registered: Jun 2003
Posts: 58

Rep: Reputation: 15
Standard Way To Share Memory Among Processes? Sys-V IPC?


What's a good way of sharing memory among processes? The only thing I've been able to come up with is System V IPC. Is this "standard" for processes that want to share data with each other? (I'm not interested in piping.) Thanks in advance.
 
Old 06-21-2003, 01:33 PM   #2
mknirsch
Member
 
Registered: Feb 2003
Location: Germany
Distribution: SLES, SuSe, Ubuntu, HPUX
Posts: 44

Rep: Reputation: 21
Smile shared memory function calls

Hi,

Shared memory allows multiple processes to share a virtual memory space.
One process creates or allocates the shared memory space and defines
the size and access rights to that memory region.
The code for memory allocation consists of the following few lines...
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
...
int shmid;
...
if( ( shmid = shmget( KEY, SIZE, SHMFLAG ) ) == -1 )
{
do some error handling, errno is set
}

the first argument KEY specifies the the key to use for the memory segment. It may either be the value IPC_PRIVATE when a new segment shall be created or it is a non zero value identifying the memory segment.

SIZE is the requested size of the memory segment (1 Byte to 1 MByte).

The SHMFLAG argument is used to set the IPC_CREAT flag and the
read/write permissions. Have a look in the man pages for further details

Once the shared memory segment is created it can be controlled via the function

status = shmctl( SHMID, CMD, struct shmid_ds *BUFFER );

The first argument SHMID is a shared memory segment identifier returned by the shmget function call.
With the second argument we can define a command to be executed
The third argument finally is a reference to the shmid_ds structure
(see <sys/shm.h> for details).
Valid operations are
IPC_STAT for returning the current values of the shmid_ds structure,
IPC_SET for modifying structure members
IPC_RMID for the removal of the system data structure for the referenced shmid
SHM_LOCK for locking the memory segment
SHM_UNLOCK for unlocking sth memory segment

The shared memory segment can be mapped into the data segment of a process. This operation is performed by the
void *shmat( SHMID, void *shmaddr, int shmflag ); function call
obviously shmid is the identifier of the shared memory segment, shmaddr
is used to assign the location of the memory segment. If shmaddr is not zero then the system will take this addrss as the attachments address for the shared memory. If shmaddr = 0 the system will pick the attachment address (not really recommended).
With the third argument you can specify the the access permissions .

The status = shmdt( void *shmaddr ); function call is used to detach the calling process's data segement from the shared memory region.

It is the programmers task to pass the shmid to the processes accessing the common shared memory area. Think about shmid as an additional argument when forking new processes; other methods are lockfiles etc.

Enjoy

Martin

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Samba share problems (IPC$) cuschu Linux - Software 10 02-22-2006 04:12 AM
IPC msgrcv - writing overflows memory extra 4 bytes tara Programming 1 11-08-2005 11:40 PM
IPC Memory Share - C Program - Why not exiting for(;;) ?? brunnopessoa Programming 4 09-05-2004 09:27 PM
Mount windows share - not standard question hopefully! Flibble Linux - General 4 10-17-2002 10:22 AM
IPC Shared Memory support in kernel? stevho Linux - General 1 01-17-2002 07:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration