shared memory and mmap - Unix

This is a discussion on shared memory and mmap - Unix ; I am trying to keep a copy of shared memory segment in a file to enable recovery from the system crash. I could have added a write to a file statement into all routines that change the shared memory. But ...

+ Reply to Thread
Results 1 to 11 of 11

Thread: shared memory and mmap

  1. shared memory and mmap

    I am trying to keep a copy of shared memory segment in a file
    to enable recovery from the system crash.

    I could have added a write to a file statement into all routines
    that change the shared memory. But I thought I could achieve
    the same result if I use mmap mechanism and keep my shared
    memory segment synchronized to a file by the system at all times.

    I tried mmap a file and then attach a shared memory segment to it
    (please see code 1) and it did not work.

    I tried to get shared memory segment ready and then mmap it to a file
    (please see code 2) and this failed as well.

    For readabity I simplified the error checking and such.

    I suspect some lack of understanding of memory usage on my part.
    Is it doable at all?
    Could someone point me into the right direction?

    /////// CODE 1 ///////////

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    #define DUMP_MAP_FILE "mymemdump"
    static const int size = 1024;
    static char *sptr = NULL; /* SH MEM Addr */
    static char *mptr = NULL; /* MMAP Addr */

    main(int argc, char *argv[])
    {
    int key = -1;
    int sid = -1;

    mk_mmap();
    if((key = (int)ftok("/tmp", '0')) == -1)
    err_exit("FTOK");
    if((sid = shmget((key_t)key, size, 0666 | IPC_CREAT)) == -1)
    err_exit("SHMGET");
    if((sptr = shmat(sid, mptr, 0)) == (char *)-1)
    err_exit("SHMAT");

    printf("OK!\n");
    shmctl(sid, IPC_RMID, 0);
    munmap(mptr, size);
    exit(0);
    }
    mk_mmap()
    {
    int fd_dump = -1;

    const int create_flags = O_CREAT | O_TRUNC | O_RDWR;
    const int access_flags = S_IRUSR | S_IWUSR | S_IRGRP |
    S_IROTH;
    const int mem_flags = PROT_READ | PROT_WRITE;

    if((fd_dump = open(DUMP_MAP_FILE, create_flags, access_flags))
    == -1)
    err_exit("Open");
    if(ftruncate(fd_dump, size) == -1)
    err_exit("Re-Size");

    mptr = mmap(0, size, mem_flags, MAP_SHARED, fd_dump, 0);
    if(mptr == (caddr_t) -1)
    err_exit("MMAP");

    close(fd_dump);
    }
    err_exit(char *msg)
    {
    printf("FATAL ERROR: %s : %i [%s]\n", msg, errno,
    strerror(errno));
    exit(1);
    }

    result:
    FATAL ERROR: SHMAT : 22 [Invalid argument]


    /////// CODE 2 ///////////

    (for readability I show just changed parts of code)

    main(int argc, char *argv[])
    {
    int key = -1;
    int sid = -1;

    if((key = (int)ftok("/tmp", '0')) == -1)
    err_exit("FTOK");
    if((sid = shmget((key_t)key, size, 0666 | IPC_CREAT)) == -1)
    err_exit("SHMGET");
    if((sptr = shmat(sid, 0, 0)) == (char *)-1)
    err_exit("SHMAT");
    mk_fix_mmap();

    printf("OK!\n");
    shmctl(sid, IPC_RMID, 0);
    munmap(mptr, size);
    exit(0);
    }
    mk_fix_mmap()
    {
    int fd_dump = -1;
    char err_buf[256];

    const int create_flags = O_CREAT | O_TRUNC | O_RDWR;
    const int access_flags = S_IRUSR | S_IWUSR | S_IRGRP |
    S_IROTH;
    const int mem_flags = PROT_READ | PROT_WRITE;
    const int map_params = MAP_FIXED | MAP_SHARED;

    if((fd_dump = open(DUMP_MAP_FILE, create_flags, access_flags))
    < 0)
    err_exit("Open");
    if(ftruncate(fd_dump, size) == -1)
    err_exit("Re-Size");
    if(mmap(sptr, size, mem_flags, map_params, fd_dump, 0) ==
    (caddr_t)-1)
    err_exit("MMAP");
    close(fd_dump);
    }

    result:
    FATAL ERROR: MMAP : 12 [Not enough space]


    Any thoughts would be appreciated.
    Regards,
    Mi

  2. Re: shared memory and mmap

    Hi,

    It looks like you just need open(2) with O_SYNC.

    migurus д:
    > I am trying to keep a copy of shared memory segment in a file
    > to enable recovery from the system crash.
    >
    > I could have added a write to a file statement into all routines
    > that change the shared memory. But I thought I could achieve
    > the same result if I use mmap mechanism and keep my shared
    > memory segment synchronized to a file by the system at all times.
    >
    > I tried mmap a file and then attach a shared memory segment to it
    > (please see code 1) and it did not work.
    >
    > I tried to get shared memory segment ready and then mmap it to a file
    > (please see code 2) and this failed as well.
    >
    > For readabity I simplified the error checking and such.
    >
    > I suspect some lack of understanding of memory usage on my part.
    > Is it doable at all?
    > Could someone point me into the right direction?
    >
    > /////// CODE 1 ///////////
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > #define DUMP_MAP_FILE "mymemdump"
    > static const int size = 1024;
    > static char *sptr = NULL; /* SH MEM Addr */
    > static char *mptr = NULL; /* MMAP Addr */
    >
    > main(int argc, char *argv[])
    > {
    > int key = -1;
    > int sid = -1;
    >
    > mk_mmap();
    > if((key = (int)ftok("/tmp", '0')) == -1)
    > err_exit("FTOK");
    > if((sid = shmget((key_t)key, size, 0666 | IPC_CREAT)) == -1)
    > err_exit("SHMGET");
    > if((sptr = shmat(sid, mptr, 0)) == (char *)-1)
    > err_exit("SHMAT");
    >
    > printf("OK!\n");
    > shmctl(sid, IPC_RMID, 0);
    > munmap(mptr, size);
    > exit(0);
    > }
    > mk_mmap()
    > {
    > int fd_dump = -1;
    >
    > const int create_flags = O_CREAT | O_TRUNC | O_RDWR;
    > const int access_flags = S_IRUSR | S_IWUSR | S_IRGRP |
    > S_IROTH;
    > const int mem_flags = PROT_READ | PROT_WRITE;
    >
    > if((fd_dump = open(DUMP_MAP_FILE, create_flags, access_flags))
    > == -1)
    > err_exit("Open");
    > if(ftruncate(fd_dump, size) == -1)
    > err_exit("Re-Size");
    >
    > mptr = mmap(0, size, mem_flags, MAP_SHARED, fd_dump, 0);
    > if(mptr == (caddr_t) -1)
    > err_exit("MMAP");
    >
    > close(fd_dump);
    > }
    > err_exit(char *msg)
    > {
    > printf("FATAL ERROR: %s : %i [%s]\n", msg, errno,
    > strerror(errno));
    > exit(1);
    > }
    >
    > result:
    > FATAL ERROR: SHMAT : 22 [Invalid argument]
    >
    >
    > /////// CODE 2 ///////////
    >
    > (for readability I show just changed parts of code)
    >
    > main(int argc, char *argv[])
    > {
    > int key = -1;
    > int sid = -1;
    >
    > if((key = (int)ftok("/tmp", '0')) == -1)
    > err_exit("FTOK");
    > if((sid = shmget((key_t)key, size, 0666 | IPC_CREAT)) == -1)
    > err_exit("SHMGET");
    > if((sptr = shmat(sid, 0, 0)) == (char *)-1)
    > err_exit("SHMAT");
    > mk_fix_mmap();
    >
    > printf("OK!\n");
    > shmctl(sid, IPC_RMID, 0);
    > munmap(mptr, size);
    > exit(0);
    > }
    > mk_fix_mmap()
    > {
    > int fd_dump = -1;
    > char err_buf[256];
    >
    > const int create_flags = O_CREAT | O_TRUNC | O_RDWR;
    > const int access_flags = S_IRUSR | S_IWUSR | S_IRGRP |
    > S_IROTH;
    > const int mem_flags = PROT_READ | PROT_WRITE;
    > const int map_params = MAP_FIXED | MAP_SHARED;
    >
    > if((fd_dump = open(DUMP_MAP_FILE, create_flags, access_flags))
    > < 0)
    > err_exit("Open");
    > if(ftruncate(fd_dump, size) == -1)
    > err_exit("Re-Size");
    > if(mmap(sptr, size, mem_flags, map_params, fd_dump, 0) ==
    > (caddr_t)-1)
    > err_exit("MMAP");
    > close(fd_dump);
    > }
    >
    > result:
    > FATAL ERROR: MMAP : 12 [Not enough space]
    >
    >
    > Any thoughts would be appreciated.
    > Regards,
    > Mi


  3. Re: shared memory and mmap

    migurus wrote:
    > I am trying to keep a copy of shared memory segment in a file
    > to enable recovery from the system crash.
    >
    > I could have added a write to a file statement into all routines
    > that change the shared memory. But I thought I could achieve
    > the same result if I use mmap mechanism and keep my shared
    > memory segment synchronized to a file by the system at all times.
    >
    > I tried mmap a file and then attach a shared memory segment to it
    > (please see code 1) and it did not work.
    >
    > I tried to get shared memory segment ready and then mmap it to a file
    > (please see code 2) and this failed as well.


    open() the file, ftruncate() it to the desired size, then mmap() it.

    Chris

  4. Re: shared memory and mmap

    On Aug 5, 12:10*am, Chris Friesen wrote:
    > migurus wrote:
    > > I am trying to keep a copy of shared memory segment in a file
    > > to enable recovery from the system crash.

    >
    > > I could have added a write to a file statement into all routines
    > > that change the shared memory. But I thought I could achieve
    > > the same result if I use mmap mechanism and keep my shared
    > > memory segment synchronized to a file by the system at all times.

    >
    > > I tried mmap a file and then attach a shared memory segment to it
    > > (please see code 1) and it did not work.

    >
    > > I tried to get shared memory segment ready and then mmap it to a file
    > > (please see code 2) and this failed as well.

    >
    > open() the file, ftruncate() it to the desired size, then mmap() it.
    >
    > Chris


    Well, this is what I am doing. I probably did not make myself clear. I
    am trying to mmap at the address of shared memory segment, so all the
    changes to that segment are copied into the mmap'ed file by the
    system.

    In my code 1 sample I mmap first, get the address into mptr, then try
    to attach (shmat) at that address mptr.
    In my code 2 sample I try to reverse - get sh memfirst, its address
    stored in sptr, and then I mmap with MAP_FIXED to that address. Both
    attempts are failing.

    Any thoughts on that?
    Regards
    Mi

  5. Re: shared memory and mmap

    migurus wrote:
    > On Aug 5, 12:10 am, Chris Friesen wrote:


    >>open() the file, ftruncate() it to the desired size, then mmap() it.


    > Well, this is what I am doing. I probably did not make myself clear. I
    > am trying to mmap at the address of shared memory segment, so all the
    > changes to that segment are copied into the mmap'ed file by the
    > system.


    Why? Just mmap() the file and use it directly. You don't need a
    separate shared memory segment.

    Chris

  6. Re: shared memory and mmap

    migurus wrote:

    > In my code 1 sample I mmap first, get the address into mptr, then try
    > to attach (shmat) at that address mptr.


    You're confusing (POSIX) mmap() and (SYSV) shmat(). After the mmap the
    file's contents is available from [mptr .. mptr+size).

    - Huibert.

  7. Re: shared memory and mmap

    migurus writes:
    >On Aug 5, 12:10=A0am, Chris Friesen wrote:
    >> migurus wrote:
    >> > I am trying to keep a copy of shared memory segment in a file
    >> > to enable recovery from the system crash.

    >>
    >> > I could have added a write to a file statement into all routines
    >> > that change the shared memory. But I thought I could achieve
    >> > the same result if I use mmap mechanism and keep my shared
    >> > memory segment synchronized to a file by the system at all times.

    >>
    >> > I tried mmap a file and then attach a shared memory segment to it
    >> > (please see code 1) and it did not work.

    >>
    >> > I tried to get shared memory segment ready and then mmap it to a file
    >> > (please see code 2) and this failed as well.

    >>
    >> open() the file, ftruncate() it to the desired size, then mmap() it.
    >>
    >> Chris

    >
    >Well, this is what I am doing. I probably did not make myself clear. I
    >am trying to mmap at the address of shared memory segment, so all the
    >changes to that segment are copied into the mmap'ed file by the
    >system.


    You can't do that. Only one mapping (either the shmat() or mmap())
    can be present at a given virtual address. If you use MAP_SHARED,
    you don't need to use shmat at all.

    scott

  8. Re: shared memory and mmap

    Scott Lurndal wrote:
    > You can't do that. Only one mapping (either the shmat() or mmap())
    > can be present at a given virtual address. If you use MAP_SHARED,
    > you don't need to use shmat at all.


    Or to put it a little differently, mmap is really just a file-backed
    shared memory segment. So if you want a shared memory segment with file
    backup, you have it with mmap alone.

    A. Silk

  9. Re: shared memory and mmap

    On Aug 5, 10:46*am, Chris Friesen wrote:
    > migurus wrote:
    > > On Aug 5, 12:10 am, Chris Friesen wrote:
    > >>open() the file, ftruncate() it to the desired size, then mmap() it.

    > > Well, this is what I am doing. I probably did not make myself clear. I
    > > am trying to mmap at the address of shared memory segment, so all the
    > > changes to that segment are copied into the mmap'ed file by the
    > > system.

    >
    > Why? *Just mmap() the file and use it directly. *You don't need a
    > separate shared memory segment.
    >
    > Chris


    The shared memory segment is accessed by many unrelated processes,
    that is the reason for it.

  10. Re: shared memory and mmap

    On Aug 5, 11:02*am, sc...@slp53.sl.home (Scott Lurndal) wrote:
    > migurus writes:
    > >On Aug 5, 12:10=A0am, Chris Friesen wrote:
    > >> migurus wrote:
    > >> > I am trying to keep a copy of shared memory segment in a file
    > >> > to enable recovery from the system crash.

    >
    > >> > I could have added a write to a file statement into all routines
    > >> > that change the shared memory. But I thought I could achieve
    > >> > the same result if I use mmap mechanism and keep my shared
    > >> > memory segment synchronized to a file by the system at all times.

    >
    > >> > I tried mmap a file and then attach a shared memory segment to it
    > >> > (please see code 1) and it did not work.

    >
    > >> > I tried to get shared memory segment ready and then mmap it to a file
    > >> > (please see code 2) and this failed as well.

    >
    > >> open() the file, ftruncate() it to the desired size, then mmap() it.

    >
    > >> Chris

    >
    > >Well, this is what I am doing. I probably did not make myself clear. I
    > >am trying to mmap at the address of shared memory segment, so all the
    > >changes to that segment are copied into the mmap'ed file by the
    > >system.

    >
    > You can't do that. * Only one mapping (either the shmat() or mmap())
    > can be present at a given virtual address. * If you use MAP_SHARED,
    > you don't need to use shmat at all.
    >
    > scott- Hide quoted text -
    >
    > - Show quoted text -


    Thank you, so I suspected the system would not allow for that.
    I think you clarified the issue.

    Regarding SHARED, as I said, I use shared memory mechanism because
    many unrelated processes access it.

  11. Re: shared memory and mmap

    migurus wrote:
    > On Aug 5, 10:46 am, Chris Friesen wrote:


    >>Why? Just mmap() the file and use it directly. You don't need a
    >>separate shared memory segment.


    > The shared memory segment is accessed by many unrelated processes,
    > that is the reason for it.


    Apparently you need to do some more research on mmap(). If all the
    processes mmap() the same file with MAP_SHARED, they will all get the
    same memory. There's no need for shmget/shmat.

    Chris

+ Reply to Thread