Changed to memory-mapped region not committing

This is a discussion on Changed to memory-mapped region not committing within the Linux Programming forums, part of the Platform Specific Boards category; I have a memory-mapped file and I'm writing changes to the memory region returned, but they don't appear to take. ...

  1. #1
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115

    Changed to memory-mapped region not committing

    I have a memory-mapped file and I'm writing changes to the memory region returned, but they don't appear to take.
    Code:
    	int des = open("processed.dat", O_RDWR | O_CREAT | O_NOATIME, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
    	assert(des != -1);
    	void *buf = mmap(NULL, SIZEOFTHESUCKA, PROT_READ | PROT_WRITE, MAP_PRIVATE, des, 0);
    	assert(buf != MAP_FAILED);
    
    ...     //some code that writes to buf
    
    const unsigned char *p = (const unsigned char *)buf;
    for (int i = 0; i < SIZEOFTHESUCKA; i++)
    	fprintf(stderr, "%02X ", (int)p[i]);
    fprintf(stderr, "\n");
    	assert(munmap(buf, SIZEOFTHESUCKA) == 0);
    	close(des);
    The stderr shows the values:
    Code:
    6D 00 00 04 00 16 33 12
    But when I call:
    Code:
    hexdump processed.dat
    I only get:
    Code:
    0000000 0000 0000 0000 0000
    0000008
    I should also point out that before the file is opened it is already SIZEOFTHESUCKA bytes long, and is zeroed.
    NDEBUG is not defined, so munmap() does get called.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    If MAP_PRIVATE is specified, modifications to the mapped data by the calling process will be visible only to the calling process and will not change the underlying object. (man mmap)
    I guess changing mmap flags or forcing a synchronization might help.

  3. #3
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    And here I was thinking it just meant that this region wouldn't be shared with other processes. Yes, changing the flags fixed it. Thanks.

    This isn't the only mysteriously useless feature of mmap(). Another is PROT_NONE.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,252
    Quote Originally Posted by Angus View Post
    And here I was thinking it just meant that this region wouldn't be shared with other processes. Yes, changing the flags fixed it. Thanks.

    This isn't the only mysteriously useless feature of mmap(). Another is PROT_NONE.
    The features are not useless, just seldom needed.

    A private map can be applied to a subset of another mapping to allow hot-patching of shared libraries without affecting the entire system.

    PROT_NONE can be used to reserve a region of address space that will be used later but not at the moment. Giving access to the pages would be incorrect, since you could not detect bugs where you improperly access this region.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a memory mapped file
    By danK in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2008, 11:38 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. virtual memory program
    By funkylunch in forum Windows Programming
    Replies: 14
    Last Post: 12-04-2003, 10:15 AM

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