Am I using mmap correctly ?

This is a discussion on Am I using mmap correctly ? within the Linux Programming forums, part of the Platform Specific Boards category; Here is an example of what I'm attempting, on a larger scale. Code: #include <iostream> #include<string> #include<sstream> #include<sys/mman.h> #include<sys/types.h> #include<sys/stat.h> ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    Am I using mmap correctly ?

    Here is an example of what I'm attempting, on a larger scale.
    Code:
    #include <iostream>
    #include<string>
    #include<sstream>
    #include<sys/mman.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    #include<cstdlib>
    #include<cstring>
    
    void set_field(int n,void* m_ptr,const std::string& data, int field_width)
    {
        memcpy(static_cast<char*>(m_ptr)+n*sizeof(char)*field_width,data.c_str(),field_width);
    }
    std::string get_field(int n,void* m_ptr, int field_width)
    {
        char data[field_width];
        memcpy(data,static_cast<char*>(m_ptr)+n*sizeof(char)*field_width,field_width);
        return std::string(data);
    }
    int main()
    {
        std::string filename;
        int file_size = 256;//Kb
        int field_width = 8;//bytes
        std::cout<<"Filename : ";
        std::cin>>filename;
    
        std::ostringstream dd_command;//"dd if=/dev/zero of=$filename count=$file_size bs=1024"
        dd_command<<"dd if=/dev/zero of="<<filename<<" count="<<file_size<<" bs=1024 2>/dev/null";
        system(dd_command.str().c_str()); // Any other way to do this ?
        
        int fd = open(filename.c_str(),O_RDWR);
        auto ptr=mmap(0,file_size*1024,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    
        if(fd==-1 || ptr==nullptr)return -1;
        
        set_field(6,ptr,"Manasij",field_width);
        std::cout<<get_field(6,ptr,field_width);
    
        munmap(ptr,file_size*1024);
        close(fd);
    
        return 0;
    }
    I'm creating an empty file of a given size and mapping it, to put a specific value somewhere within it and retrieve it.
    I'll map small subsections of the file when doing this on large files, but other than that, is there anything wrong here ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'm creating an empty file of a given size and mapping it, to put a specific value somewhere within it and retrieve it.
    I'll map small subsections of the file when doing this on large files, but other than that, is there anything wrong here ?
    I think the mmap is fine, comparing it to the few times I've used it.

    Quote Originally Posted by manasij7479 View Post
    Code:
        dd_command<<"dd if=/dev/zero of="<<filename<<" count="<<file_size<<" bs=1024 2>/dev/null";
        system(dd_command.str().c_str()); // Any other way to do this ?
    How about:

    Code:
    char zeroKb[1024] = { 0 };
    for (int i = 0; i < 256; i++) write(fd, zeroKb, 1024);
    or putc(0) 256k times. "write()" is in unistd.h.
    Last edited by MK27; 03-25-2012 at 10:20 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by MK27 View Post
    Code:
    char zeroKb[1024] = { 0 };
    for (int i = 0; i < 256; i++) write(fd, zeroKb, 1024);
    That was simple. I wonder if dd does something special to do this though. (I'll check out the coreutils sources tomorrow to find out.)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by manasij7479 View Post
    That was simple. I wonder if dd does something special to do this though. (I'll check out the coreutils sources tomorrow to find out.)
    Nah. dd is exact. You could always create a file both ways and run diff or look with hexdump, but I don't see what could complicate this. A 256KB file of zeros is a 256KB file of zeros.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,650
    The quickest way to create a 0 filled file is open(), lseek() to the end, and write a single 0 byte. Descent filesystems won't have to physically fill the gap with zero's.
    https://www.kernel.org/doc/man-pages...2/lseek.2.html

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mmap
    By vispi in forum C++ Programming
    Replies: 3
    Last Post: 07-04-2009, 12:33 PM
  2. mmap program
    By karthigayan in forum C Programming
    Replies: 1
    Last Post: 04-01-2009, 04:38 AM
  3. mmap-ing files > 4GB?
    By zxcv in forum Linux Programming
    Replies: 8
    Last Post: 06-10-2008, 04:05 AM
  4. mmap
    By forumuser06 in forum C Programming
    Replies: 1
    Last Post: 12-01-2006, 07:31 AM
  5. mmap()
    By PutoAmo in forum C Programming
    Replies: 2
    Last Post: 12-27-2002, 05:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21