Re: mmap, why do not work?
- From: Roger Leigh <${rleigh}@invalid.whinlatter.ukfsn.org.invalid>
- Date: Tue, 28 Feb 2006 09:50:46 +0000
"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.
.
- References:
- mmap, why do not work?
- From: sr
- mmap, why do not work?
- Prev by Date: Re: Way to get a set of string from the data already existing on screen using shell script.
- Next by Date: how to pass command line arguments to makefile
- Previous by thread: Re: mmap, why do not work?
- Next by thread: Retreiving TCP long options from tcphdr
- Index(es):
Relevant Pages
|
|