LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-05-2009, 06:23 AM   #1
ShreeBand
LQ Newbie
 
Registered: May 2009
Posts: 2

Rep: Reputation: 0
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.
 
Old 05-05-2009, 07:41 AM   #2
mknirsch
Member
 
Registered: Feb 2003
Location: Germany
Distribution: SLES 10, SuSe 11.x
Posts: 31

Rep: Reputation: 16
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
 
Old 05-07-2009, 01:41 AM   #3
ShreeBand
LQ Newbie
 
Registered: May 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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.
 
  


Reply

Tags
memory, shared


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Can't access shared memory area. SHMConfig disabled? alkos333 Slackware 2 12-22-2008 07:00 PM
Linux shared memory segment access problem and x86 Virtual Memory layout. regmee Linux - Kernel 1 08-23-2008 12:11 AM
Difference between resident memory,shared memory and virtual memory in system monitor mathimca05 Linux - Newbie 1 11-11-2007 04:05 AM
Being confusing on linux shared memory nuying117 Programming 2 08-02-2007 11:39 AM
Can LINUX use shared memory? Shirley Linux - General 1 05-24-2002 02:50 AM


All times are GMT -5. The time now is 09:17 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration