need to be able to write in a shared mem segment.

Discussion in 'C++' started by nass, Sep 23, 2006.

  1. nass

    nass Guest

    hi all,
    i am running slackware linux and need to use some function that will
    will enable me to write and read from a shared mem segment..
    i am using open() , to open a file, and then use mmap to get a void*
    file_memory pointer. then i use fwrite(aStruct,structSize,1,(((FILE*)
    file_memory) + anOffset)) to try to write to the file but it crashes.

    could it be that fwrite does not understand the file_memory pointer?
    what else can i use to do the writing?
    thankyou for your help
    nass
     
    nass, Sep 23, 2006
    #1
    1. Advertising

  2. nass

    red floyd Guest

    red floyd, Sep 23, 2006
    #2
    1. Advertising

  3. nass

    loufoque Guest

    nass wrote :
    > hi all,
    > i am running slackware linux and need to use some function that will
    > will enable me to write and read from a shared mem segment..


    You could use boost.interprocess (used to be boost.shmem)
     
    loufoque, Sep 23, 2006
    #3
  4. nass schrieb:
    > hi all,
    > i am running slackware linux and need to use some function that will
    > will enable me to write and read from a shared mem segment..


    This is offtopic here, but...

    > i am using open() , to open a file, and then use mmap to get a void*
    > file_memory pointer. then i use fwrite(aStruct,structSize,1,(((FILE*)
    > file_memory) + anOffset)) to try to write to the file but it crashes.


    The fourth parameter of fwrite must be a _unchanged_ FILE*, that you got by
    calling fopen. You can't cast any pointer to a FILE* and write to that like
    a file.

    And you don't need to. You said that, mmap returns a memory pointer as
    void*. So simply use it, just like you would use memory from a malloc() call.

    > could it be that fwrite does not understand the file_memory pointer?


    exactly.

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
     
    Thomas J. Gritzan, Sep 23, 2006
    #4
  5. nass

    nass Guest

    Thomas J. Gritzan wrote:
    > nass schrieb:
    > > hi all,
    > > i am running slackware linux and need to use some function that will
    > > will enable me to write and read from a shared mem segment..

    >
    > This is offtopic here, but...
    >
    > > i am using open() , to open a file, and then use mmap to get a void*
    > > file_memory pointer. then i use fwrite(aStruct,structSize,1,(((FILE*)
    > > file_memory) + anOffset)) to try to write to the file but it crashes.

    >
    > The fourth parameter of fwrite must be a _unchanged_ FILE*, that you got by
    > calling fopen. You can't cast any pointer to a FILE* and write to that like
    > a file.
    >
    > And you don't need to. You said that, mmap returns a memory pointer as
    > void*. So simply use it, just like you would use memory from a malloc() call.
    >

    nope i cant do that, the compiler gives me an error saying
    error: invalid conversion from `void*' to `FILE*'
    its gcc btw.. any other posibility?


    > > could it be that fwrite does not understand the file_memory pointer?

    >
    > exactly.
    >
    > --
    > Thomas
    > http://www.netmeister.org/news/learn2quote.html



    nass
     
    nass, Sep 24, 2006
    #5
  6. nass wrote:
    >
    >
    >
    > Thomas J. Gritzan wrote:
    >> nass schrieb:
    >>> hi all,
    >>> i am running slackware linux and need to use some function that will
    >>> will enable me to write and read from a shared mem segment..

    >> This is offtopic here, but...
    >>
    >>> i am using open() , to open a file, and then use mmap to get a void*
    >>> file_memory pointer. then i use fwrite(aStruct,structSize,1,(((FILE*)
    >>> file_memory) + anOffset)) to try to write to the file but it crashes.

    >> The fourth parameter of fwrite must be a _unchanged_ FILE*, that you got by
    >> calling fopen. You can't cast any pointer to a FILE* and write to that like
    >> a file.
    >>
    >> And you don't need to. You said that, mmap returns a memory pointer as
    >> void*. So simply use it, just like you would use memory from a malloc() call.
    >>

    > nope i cant do that, the compiler gives me an error saying
    > error: invalid conversion from `void*' to `FILE*'
    > its gcc btw.. any other posibility?
    >


    mmap returns a pointer to the shared memory.
    You do not use a 'FILE *' to write to that memory.
    After the mmap you read/update it like any other memory
    buffer.

    You'll get better support in these newsgroups:

    comp.os.linux.development.apps
    comp.os.linux.development.system

    Larry

    >
    >>> could it be that fwrite does not understand the file_memory pointer?

    >> exactly.
    >>
    >> --
    >> Thomas
    >> http://www.netmeister.org/news/learn2quote.html

    >
    >
    > nass
    >
     
    Larry I Smith, Sep 25, 2006
    #6
  7. nass

    nass Guest

    for some reason i can not 'open' these 2 sites u recommended from a
    newsgroups reader..
    anyhow, igues you are refering to doing buffer read-update with
    sprintf()... i would like to use that function too but the problem is i
    have a struct that i need to save in the buffer, and i do not now what
    type to instruct the sprintf function to 'think' my struct is....
    the struct is not always the same.. but it contains in8, uint8, int32,
    floats, doubles and even strings.
    how could i use sprintf in order to save this struct?
    nass

    Larry I Smith wrote:
    > nass wrote:
    > >
    > >
    > >
    > > Thomas J. Gritzan wrote:
    > >> nass schrieb:
    > >>> hi all,
    > >>> i am running slackware linux and need to use some function that will
    > >>> will enable me to write and read from a shared mem segment..
    > >> This is offtopic here, but...
    > >>
    > >>> i am using open() , to open a file, and then use mmap to get a void*
    > >>> file_memory pointer. then i use fwrite(aStruct,structSize,1,(((FILE*)
    > >>> file_memory) + anOffset)) to try to write to the file but it crashes.
    > >> The fourth parameter of fwrite must be a _unchanged_ FILE*, that you got by
    > >> calling fopen. You can't cast any pointer to a FILE* and write to that like
    > >> a file.
    > >>
    > >> And you don't need to. You said that, mmap returns a memory pointer as
    > >> void*. So simply use it, just like you would use memory from a malloc() call.
    > >>

    > > nope i cant do that, the compiler gives me an error saying
    > > error: invalid conversion from `void*' to `FILE*'
    > > its gcc btw.. any other posibility?
    > >

    >
    > mmap returns a pointer to the shared memory.
    > You do not use a 'FILE *' to write to that memory.
    > After the mmap you read/update it like any other memory
    > buffer.
    >
    > You'll get better support in these newsgroups:
    >
    > comp.os.linux.development.apps
    > comp.os.linux.development.system
    >
    > Larry
    >
    > >
    > >>> could it be that fwrite does not understand the file_memory pointer?
    > >> exactly.
    > >>
    > >> --
    > >> Thomas
    > >> http://www.netmeister.org/news/learn2quote.html

    > >
    > >
    > > nass
    > >
     
    nass, Sep 25, 2006
    #7
  8. nass

    Larry Smith Guest

    nass wrote:
    > for some reason i can not 'open' these 2 sites u recommended from a
    > newsgroups reader..
    > anyhow, igues you are refering to doing buffer read-update with
    > sprintf()... i would like to use that function too but the problem is i
    > have a struct that i need to save in the buffer, and i do not now what
    > type to instruct the sprintf function to 'think' my struct is....
    > the struct is not always the same.. but it contains in8, uint8, int32,
    > floats, doubles and even strings.
    > how could i use sprintf in order to save this struct?
    > nass
    >
    > Larry I Smith wrote:
    >> nass wrote:
    >>>
    >>>
    >>> Thomas J. Gritzan wrote:
    >>>> nass schrieb:
    >>>>> hi all,
    >>>>> i am running slackware linux and need to use some function that will
    >>>>> will enable me to write and read from a shared mem segment..
    >>>> This is offtopic here, but...
    >>>>
    >>>>> i am using open() , to open a file, and then use mmap to get a void*
    >>>>> file_memory pointer. then i use fwrite(aStruct,structSize,1,(((FILE*)
    >>>>> file_memory) + anOffset)) to try to write to the file but it crashes.
    >>>> The fourth parameter of fwrite must be a _unchanged_ FILE*, that you got by
    >>>> calling fopen. You can't cast any pointer to a FILE* and write to that like
    >>>> a file.
    >>>>
    >>>> And you don't need to. You said that, mmap returns a memory pointer as
    >>>> void*. So simply use it, just like you would use memory from a malloc() call.
    >>>>
    >>> nope i cant do that, the compiler gives me an error saying
    >>> error: invalid conversion from `void*' to `FILE*'
    >>> its gcc btw.. any other posibility?
    >>>

    >> mmap returns a pointer to the shared memory.
    >> You do not use a 'FILE *' to write to that memory.
    >> After the mmap you read/update it like any other memory
    >> buffer.
    >>
    >> You'll get better support in these newsgroups:
    >>
    >> comp.os.linux.development.apps
    >> comp.os.linux.development.system
    >>
    >> Larry
    >>
    >>>>> could it be that fwrite does not understand the file_memory pointer?
    >>>> exactly.
    >>>>
    >>>> --
    >>>> Thomas
    >>>> http://www.netmeister.org/news/learn2quote.html
    >>>
    >>> nass
    >>>

    >


    Please do not top post.

    One more time...

    FORGET ABOUT 'FILE *'.

    Once a file is mapped into memory - it's just memory.

    The sprintf() example was just an example of one (of many)
    ways to access the memory.

    If you want to put a 'struct stuff' in the mapped memory,
    just do it:

    struct stuff * pStuff;

    /* assuming that 'pMem' is the pointer returned by
    * mmap() -AND- that the mapped memory is large
    * enough to hold a 'struct stuff', this next line
    * allows your code to treat the memory at 'pMem'
    * as a 'struct stuff' - via 'pStuff'.
    * anything put into '*pStuff' will be visible to
    * all processes that have the same memory mapped.
    */
    pStuff = (struct stuff *) pMem;

    /* now write and read the members of the
    * 'struct stuff' that resides at the beginning
    * of the mapped memory, eg:
    */
    pStuff->some_int_field = 55;


    This is NOT a C++ language issue so - Post to a
    unix/linux newsgroup in the future.

    READ THE DOCS ON THE MEMORY MAPPING FUNCTIONS.
    It's all explained therein.

    Larry
     
    Larry Smith, Sep 26, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spendius
    Replies:
    4
    Views:
    346
    Michael Mair
    Nov 18, 2004
  2. Replies:
    8
    Views:
    317
    jaysome
    Mar 1, 2007
  3. nass
    Replies:
    1
    Views:
    342
    mlimber
    Oct 5, 2006
  4. news.aon.at
    Replies:
    11
    Views:
    649
    Ian Collins
    Jan 29, 2011
  5. Tristin Davis
    Replies:
    4
    Views:
    326
    Tristin Davis
    Jul 4, 2008
Loading...

Share This Page