Re: Help on mmap (with pointers)

From: Nissan 350Z (none_at_none.com)
Date: 02/25/04


Date: Wed, 25 Feb 2004 21:29:36 +0100


> If I have a data structure with pointers that want to memory map into
> a file. How exactly do I do this?

>From HP ITRC:

      Date: 3/9/95
      Document description: Example of a program using a memory mapped file
      Document id: UNX1000079

Problem Description

How is the coding of a program to use a memory mapped file?

Solution

Here's two programs that share a memory-mapped file. The file is exactly
the size of 10 integers. The programs map the file and use it as if it
were a 10-member array of integers in memory. One program writes into the
array and the other reads from the array; since the file is mapped with
the MAP_SHARED flag, the processes share the updates:

--------------
Writer process
--------------

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>

main()
{
        int *stuff;
        int myfd;

        /* create a file of the proper length, if needed */
        if (access("/tmp/tenints", F_OK) != 0)
        {
                if (creat("/tmp/tenints", 0660) == -1)
                {
                        perror("Can't create");
                        exit(1);
                }
                if (truncate("/tmp/tenints", 10*sizeof(int)) == -1)
                {
                        perror("Can't truncate");
                        exit(1);
                }
        }

        /* open the file */
        if ((myfd = open("/tmp/tenints", O_RDWR)) == -1)
        {
                perror("Can't open");
                exit(1);
        }

        /* mmap it and treat it as an array of integers */
        if ((stuff = (int *)mmap(0, 10*sizeof(int), PROT_READ|PROT_WRITE,
                        MAP_FILE|MAP_SHARED|MAP_VARIABLE, myfd, 0)) == -1)
        {
                perror("Can't mmap");
                exit(1);
        }
        srand(time(0));

        /* write in random places in the file using normal array syntax */
        for(;;)
        {
                int nextind = rand() % 10;
                int nextval = rand();
                stuff[nextind] = nextval;
                printf("Writing %d at index %d\n", nextval, nextind);
                sleep(2);
        }
}

--------------
Reader process
--------------

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>

main()
{
        int *stuff;
        int myfd;

        if ((myfd = open("/tmp/tenints", O_RDONLY)) == -1)
        {
                perror("Can't open");
                exit(1);
        }

        if ((stuff = (int *)mmap(0, 10*sizeof(int), PROT_READ,
                        MAP_FILE|MAP_SHARED|MAP_VARIABLE, myfd, 0)) == -1)
        {
                perror("Can't mmap");
                exit(1);
        }
        srand(time(0));
        for(;;)
        {
                int nextind = rand() % 10;
                int nextval = stuff[nextind];
                printf("Reading %d at index %d\n", nextval, nextind);
                sleep(2);
        }
}



Relevant Pages

  • Re: How to point to a global memory block from multiple apps?
    ... > How is MyArrlinked tothe memory mapped file? ... > memory mappoed file to my array memory, ... the mapping address may actually be different in each ...
    (microsoft.public.vb.winapi)
  • Re: share a structure array containing multidimensional char array
    ... The "writer" has to serialize the structure data and provide the size of the total number of bytes in the shared memory segment to the "reader", for instance as an int value at the start of the MM segment. ... // use the array of structs... ... > I already have C++ code for handling memory mapped file. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How: multiple program instances sharing same data
    ... It's the memory consumption. ... otherwise typeless byte array. ... memory mapped file, which in both cases means i have to read/convert ... I don't get it, if you really need to run 50 copies of the same application, just like it's done when running on Terminal Server or Citrix, then you need to keep an eye on your resource usage, that is you need to account for the necessary CPU resources and you need the Memory resources. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: memmapfile
    ... It is true that I could simply use C functionality to memory ... function and a matlab memory mapped file in matlab). ...
    (comp.soft-sys.matlab)
  • Re: memmapfile
    ... It is true that I could simply use C functionality to memory ... function and a matlab memory mapped file in matlab). ...
    (comp.soft-sys.matlab)