Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 17 of 17
Originally Posted by bkeller The sysconf() function is used to see values for certain system configuration variables. For most systems, including linux, you can get the page size by calling ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Just Joined!
    Join Date
    Sep 2008
    Posts
    8

    Quote Originally Posted by bkeller View Post
    The sysconf() function is used to see values for certain system configuration variables. For most systems, including linux, you can get the page size by calling getpagesize(). I used sysconf() to get page size only for HPUX because that system did not support the getpagesize() function (this was early 1990's). Note that memory mapping is always in integer multiples of page size.
    will u please go through following code?
    It is nothing but code in man page of mmap.If i kept a.txt blank, it gives error "offset is past end of file" .If file is not blank then after munmap() or msync() whole string is not sync with the file a.txt. Plaese help me


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

    #define handle_error(msg) \
    do { perror(msg); exit(EXIT_FAILURE); } while (0)

    int main()
    {
    char *addr,*str="abcdefgh";
    int fd;
    struct stat sb;
    off_t offset, pa_offset;
    size_t length,count,ln=strlen (str);
    ssize_t s;


    fd = open("a.txt", O_RDWR);
    if (fd == -1)
    handle_error("open");

    if (fstat(fd, &sb) == -1) /* To obtain file size */
    handle_error("fstat");

    offset = 0; ///from start of file
    pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);
    /* offset for mmap() must be page aligned */

    if (offset >= sb.st_size) { fprintf(stderr, "offset is past end of file\n");
    exit(EXIT_FAILURE);
    }


    length = 1000;
    count = 0 - pa_offset;
    printf("\nstr =>&#37;s\tln => %d , %d\n",str,ln,length - pa_offset);

    addr = mmap(NULL, length - pa_offset, PROT_WRITE,MAP_SHARED, fd, pa_offset);
    if (addr == MAP_FAILED)
    handle_error("mmap");
    printf("\ncontents are\n");
    s = write(STDOUT_FILENO, addr + offset - pa_offset, length);

    while(count != ln)
    {
    *(addr + count)= *(str + count);
    printf(" addr[%d] => %c ",count,*(addr+count));
    count++;
    }

    if(munmap(addr,count)==0)
    //msync(addr,count,MS_SYNC);
    printf("\ncount => %d\n",count);
    //else
    // printf("\nERROR\n");



    exit(EXIT_SUCCESS);
    } /* main */

  2. #12
    Just Joined!
    Join Date
    Jun 2008
    Location
    North East U.S.
    Posts
    30
    You are getting into usage that I'm not familiar with. Be sure you get a zero return from msync() and check to see if munmap() instead of msync() shows all your changes. It looks like msync() allows lengths that are not multiples of the page size. This may be better for updating existing data in a file rather than adding to the end of a file. I just don't have experience doing that.

  3. #13
    Just Joined!
    Join Date
    Jun 2008
    Location
    North East U.S.
    Posts
    30
    My last post was in reference to your previous post. I can't tell how large your file is to begin with, but you need to ensure the length you specify (1000) is less than or equal to the size of your file. You are not allowed to grow the size of a file via mmap - you need to map only pages that already exist in the file. You can make the file large enough by writing one byte at length-1 offset into the file before you map it. Note also that mmap will always map an integral number of pages - which may go beyond the actual lingth of your file. If you refer beyond the length of the file, you will get an error of some kind (BUS or SEGMENT).

  4. $spacer_open
    $spacer_close
  5. #14
    Just Joined!
    Join Date
    Sep 2008
    Posts
    8
    I need to explain you what I want to do using mmap.What I thought that second argument to mmap() is the length of mapping so Im able to allocate mapping of given length for blank file also.I want to confirm this once again that if this is not possible then what is the purpose of providing length parameter specifically?Does mmap() calculates length of file from file descriptor,that we are providing as 5th argument?And is it necessary to call munmap() before mremep().
    Now what I want to do.....
    I want to allocate some space(not exact to file length) for a file,this file may be a blank file,I will get a starting address of that mapping,using it Im going to fill some data to it,after it I want it to write down to the disk.
    Whether this is possible or not using mmap(),munmap(),mremap(),msync()

  6. #15
    Just Joined!
    Join Date
    Jun 2008
    Location
    North East U.S.
    Posts
    30
    Chetan,

    The mmap() function allows you to use operating system page-in and page-out to access and update data stored in a file. When you refer to a location in memory on a page that is mapped to a file for the first time, it will read that page in from the file so you can modify it or simply reference it. If that page does not exist in the file, you will get an error (a BUS or SIGNAL error). If you want to update pages within a file but not extend the length of the file, you can do this with mmap(). But if you want to extend the length of the file, you will need to do that with write() (or perhaps fwrite). The length parameter is specified because many applications do not bother to map the entitre file, but just the area they need. Consider a database file that may contain many different tables of data - the mmap() could be used to map just one of the tables stored in the file into memory. It should be possible to map into memory beyond the length of the file as long as when you first want to extend the file size, you write the pages out using write() - then if you reference them by accessing memory, those pages will exist and they will be read into your addess space.

    If all you want to do is write a new file, it would be better to put the data into memory (obtained with malloc() or mmap /dev/zeros) and then write it all to the file at the end with a single write() call. I would recommend using mmap only when you expect to be (primarily) referencing or updating existing data in a file. While it is certainly possible to extend a file using write() while also having it mapped, you need to be careful about accessing memory beyond the current end of the file.

    Brion

  7. #16
    Just Joined!
    Join Date
    Sep 2008
    Posts
    8

    mmap

    Im in problem because of the shocking behaviour of mmap()
    In the following code,I have map a file named a.c(blank at the start),I lseek upto 4096 and write blank character at the end of file. Now I have map that file into memory,and fill 10 characters to mmap region.Now enen though Im not unmapping the nap region or not writting anything to file the first character gets written to the file.
    Why is so ?
    Please help.

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


    #define FILEPATH "/root/a.c"

    #define sz 4096


    int main()
    {
    int i;
    int fd;
    int result;
    char *map,*x="10,abc,5000";
    struct stat st;

    fd = open(FILEPATH, O_RDWR | O_CREAT | O_APPEND, (mode_t)0600);
    if (fd == -1)
    {
    perror("Error opening file for writing");
    exit(EXIT_FAILURE);
    }


    result = lseek(fd, sz, SEEK_SET);
    if (result == -1)
    {
    close(fd);
    printf("\nno seek\n");
    exit(EXIT_FAILURE);
    }


    result = write(fd, "", 1);
    if (result != 1)
    {
    close(fd);
    printf("\nWritting last byte of the file\n");
    exit(EXIT_FAILURE);
    }
    fstat(fd , &st);
    printf("\nfile length %d \n",st.st_size);

    map = (char *) mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (map == MAP_FAILED)
    {
    close(fd);
    perror("Error mmapping the file");
    exit(EXIT_FAILURE);
    }

    i=0;
    while(i < 10)
    {
    printf(" %d) %c",i,map[i]=x[i]);
    i++;
    }









    /*write(fd, map, strlen(map));

    if (munmap(map , st.st_size) == -1)
    {
    perror("Error un-mmapping the file");

    }
    */
    close(fd);
    return 0;
    }

  8. #17
    Just Joined!
    Join Date
    Jun 2008
    Location
    North East U.S.
    Posts
    30
    I can't tell what you saw in your output, but I would expect the first 10 bytes of x to be put into the first 10 bytes of the file and the file should be 4097 bytes long and all zeros except for the first 10 bytes. When your process ends, all mappings are unmapped by the OS and since you mapped specifying MAP_SHARED, all changes to the mapped area will be seen in the file. If you use MAP_PRIVATE, all changes will not be seen by other users of the file.

    Brion

Page 2 of 2 FirstFirst 1 2

Posting Permissions

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