Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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.
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 ) ;
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.