Re: mmap, why do not work?



"sr" <sruohola@xxxxxxxxx> writes:

I have this kind of a code.
It is from a book Linux programming,
unleashed.
I am not able to make it work so that, when it is run in two different
xterm windows, the two different processes would affect to each others.

This example was not very nice. If you want a better example of mmap,
try the following.

This is a simple program to mmap a file into memory, and correct
corrupted line endings ("\n\n" -> "\r\n"; a Windows editor at my old
workplace had done this damage).

Since you want shared memory, use shm_open, ftruncate+memset and mmap
to map a map in a shared memory region. ftruncate sets the size,
memset zeroes it out and makes it valid (has a backing store), and you
can then safely mmap and use it. The actual mmap usage is no
different than the example below, however.


/* Copyright © 2006 Roger Leigh <rleigh@xxxxxxxxxx>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*********************************************************************/

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

bool
process_file (const char *file);

void
hardfail (const char *msg)
{
fprintf(stderr, "E: %s: %s\n", msg, strerror(errno));
exit(EXIT_FAILURE);
}

int main (int argc,
char *argv[])
{
for (int i = 1; i < argc; ++i)
if (process_file(argv[i]) == false)
break;

return EXIT_SUCCESS;
}

bool
process_file (const char *file)
{
int fd = open(file, O_RDWR);
if (fd < 0)
hardfail(file);

struct stat statbuf;
if (fstat(fd, &statbuf) < 0)
hardfail(file);

char *map = mmap(NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
hardfail("mmap failed");

for (size_t i = 0; i < statbuf.st_size - 1; ++i)
if (map[i] == 0xA && map[i+1] == 0xA)
{
fprintf(stdout, "C: %s byte %lu\n", file, i);
map[i] = 0xD;
}

if (msync(map, statbuf.st_size, MS_SYNC) < 0)
hardfail("msync failed");

if (munmap(map, statbuf.st_size) < 0)
hardfail("munmap failed");

if (close(fd) < 0)
hardfail(file);

return true;
}



--
Roger Leigh
Printing on GNU/Linux? http://gutenprint.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
.



Relevant Pages

  • Re: 2.6.23-mm1
    ... ET_DYN binaries onto a random address (in cases in which mmap() is allowed ... * GNU General Public License for more details. ... +static inline unsigned long mmap_base ... +static inline int mmap_is_32 ...
    (Linux-Kernel)
  • Re: 2.6.23-rc3-mm1 - memory layout change? - lost support for MAP_32BIT? - mono
    ... Mono claims to mmap with the MAP_32BIT option. ... int retval, i, size; ... SuSE Labs. ... * GNU General Public License for more details. ...
    (Linux-Kernel)
  • [PATCH 01/14] spufs: cell spu problem state mapping updates
    ... it removes mmap ability of individual files when using 64k ... entire 64k page holding both registers. ... It goes along a separate patch to libspe implementing usage of that new ... unsigned long address, int *type) ...
    (Linux-Kernel)
  • Re: Marking a Page of Memory Executable
    ... What I want to do is get an executable and writable page of memory, ... instruction pointer on x86) to that page so that it will execute that ... mmap, munmap - map or unmap files or devices into memory ... void * mmap(void *start, size_t length, int prot, int ...
    (comp.lang.c)
  • Re: [PATCH][RESEND] PIE randomization
    ... The fix to this is trivial - just fix the prototype in ia32 compat version ... This patch is using mmap's randomization functionality in such a way ... ET_DYN binaries onto a random address (in cases in which mmap() is allowed ... static int set_brk ...
    (Linux-Kernel)