Find the answer to your Linux question:
Results 1 to 5 of 5
Hello I face a problem in accessing sharedmemory between 2 different process. One process creates shared memory and adds data to it. Another one that access the data. Attaching sample ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    0

    Shared Memory Access in Linux


    Hello

    I face a problem in accessing sharedmemory between 2 different process.


    One process 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.

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Quote Originally Posted by Shree03 View Post
    I assume the second process to get the same address , because it access the same shared memory ,
    AFAIK, this assumption is not correct in x86er protected mode.
    Protected mode - Wikipedia, the free encyclopedia
    Debian GNU/Linux -- You know you want it.

  3. #3
    Just Joined!
    Join Date
    May 2009
    Posts
    0

    Shared Memory Access in Linux

    Does that mean we cannot place pointers in shared memories ...

    But the same address is obtained in HP Server ? By chance , do you know why it is so ?

  4. #4
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,844
    On the Intel chipset, it is possible to provide seperate address space for each process - this is desirable because it is prevents one application reading and writing to the address space (i.e. over the data of) another application in a multi-tasking environment.

    You can share memory between such process spaces, but the pointers wont be the same. Windows gets around this by having complicated address mapping and sharing methods hidden behind the COM layer, these are quite nice, as it also handles threading and other stuff. I know it's possible to do similar in Linux, but not being a commercial software developer for this platform, I don't know how.

    This is a huge subject, though. You can probably understand a lot better by googling for help here - the explanations will be far more comprehensive and lucid that we can get by cramming info into a short message on a forum.
    Linux user #126863 - see http://linuxcounter.net/

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,379
    The OS, with CPU help, maps physical to virtual memory. Each process has its own virtual memory space on an x86 system running in protected mode (Linux, WIndows, QNX, Solaris, OSX, et al). A shared memory segment that is created and attached by one process, but attached by another, will likely not have the same virtual memory address in the two applications in Linux. However, they should be pointing to the same physical memory. That said, I think that the shmflg value you are passing to shmat() is incorrect for Linux. I need to do some more reading about the valid flags for the attachment, but they aren't the same as you would pass to shmget() to create/locate the segment.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •