LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Linux Shared Memory Access (http://www.linuxquestions.org/questions/linux-newbie-8/linux-shared-memory-access-723831/)

ShreeBand 05-05-2009 06:23 AM

Linux Shared Memory Access
 
Hello,
I have created 2 process . One that creates shared memory and the other one that access the shared memory . The structure that I place in the shared memory is of the below type:

struct sampple
{
int i ;
int *ptr;
}

When the first process creates the sharedmemory , shared memory address is given as OxEF .

Now when the second process access the shared memory , when it uses shmget , it gets the a different address instead of 0xEF.

This seems to be a problem for our code .

However when we tried the same in HP box , the addresses were same for both the processes.

Can someone explain why the addresses are different ?

If incase thats the way Linux works , can someone tell me if thers some settings to make the address same.

mknirsch 05-05-2009 07:41 AM

Hello ShreeBand,

I assume you use unix system V shared memory API.

Includes:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int shmget( key_t key, int size, int flag);
Value of key_t:
a) positive integer: shmget tries to open a shared memory whose key matches the specified value
b) IPC_PRIVATE: shmget allocates a new shared memory that is private to the calling process
Value of Size:
Min. and max. values are defined in <sys/shm.h> SHMMIN and SHMMAX constants
The maximum number of memory regions is defined in the <sys/shm.h> SHMMNI constant.
size defines the size of the shared memory region
Value of flag:
0: shmget fails if there is no shared memory that matches the specified key; else it returns a descriptor to that memory region.
IPC_CREAT with bitwise OR of r/w access rights: shmget creates new shared memory with specified size
return value:
-1: function failed
pos. integer: descriptor to shared memory

Basically your parent process creates shared memory using the IPC_PRIVATE key; then it forks child processes. A child process opens the shared memory using the descriptor returned by a successful shmget call. The size argument can be less or equal to the size of the shared memory created by the parent process.

Please keep in mind that the shmget API requests the kernel to allocate memory. It can be confusing when switching between kernel space and thread/process space.

(Take a look at the posix shared memory API)

regards

Martin

ShreeBand 05-07-2009 01:41 AM

Hello Martin,

Thanks for the reply .

But my problem is a different case . Firstly I have 2 independent process . One that creates shared memory and adds data to it.
Another one that access the data.

Attaching sample code :
Process 1 :

int lMemoryHandle ;
lMemoryHandle = shmget
( 8888
, sizeof ( element )
, IPC_CREAT | 0666 ) ;

if ( lMemoryHandle < 0 )
{

lReturnFlag = false ;
printf ( "\nMemory Creation Failed ! " ) ;
}

printf ( "\nMemory Created Successfully ! " ) ;

void * lMsgPointer ;

lMsgPointer = shmat
( lMemoryHandle
, (void *) NULL
, IPC_CREAT | 0666 ) ;

if ( lMsgPointer == ( void * ) -1 )
{
lReturnFlag = false ;
printf("\nMemory Attach Failed ! " ) ;

}

printf("\nMemory Attached Successfully ! " ) ;

printf("\nAddress 1 : %d\n",lMsgPointer ) ;
lPtr2 = ( element * ) lMsgPointer;
lPtr2->rollno = 22 ;
lPtr2->age = &lPtr2->rollno ;
printf("\nprocess 1 : the roll no is %d\n",*(lPtr2->age) ) ;


Process 2 :

int lMemoryHandle ;
lMemoryHandle = shmget
( 8888
, sizeof ( gaurav )
, IPC_CREAT | 0666 ) ;

if ( lMemoryHandle < 0 )
{

lReturnFlag = false ;
printf ( "\nMemory Creation Failed ! " ) ;
}

printf ( "\nMemory Created Successfully ! " ) ;

void * lMsgPointer ;

lMsgPointer = shmat
( lMemoryHandle
, (void *) NULL
, IPC_CREAT | 0666 ) ;

if ( lMsgPointer == ( void * ) -1 )
{
lReturnFlag = false ;
printf("\nMemory Attach Failed ! " ) ;
}

printf("\nMemory Attached Successfully ! " ) ;

printf("\nAddress 1 : %d\n",lMsgPointer ) ; lPtr2 = ( gaurav * ) lMsgPointer;
printf("\nprocess 1 : the roll no is %d\n",*(lPtr2->age) ) ;

In the first process , address is given as say : 1720287232 ..

I assume the second process to get the same address , because it access the same shared memory ,
But the address is as : 160436224 ( a different one. )

Because of this accesssing *(lptr2->age) results in a segmentation fault .

But the same code works in HP Server , cos the address returned is the same .

Is there any way in Linux , to make the address same for the process.


All times are GMT -5. The time now is 10:25 AM.