Re: Simple question about mmap() system call



On Fri, 23 Jun 2006 17:47:57 +0300 (EEST)
Dmitry Pryanishnikov <dmitry@xxxxxxxxxxxxxx> wrote:

Hello!

I'm writing an utility that should examine some bytes of a large file
and modify them - that't all. I've decided to mmap() the file:

void *diskp;

if ((fd=open(argv[1], O_RDWR)) == -1)
err(EX_NOINPUT, "Can't open %s for readind and writing", argv[1]);

if ((diskp=mmap(NULL, 512,
PROT_READ | PROT_WRITE, 0, fd, 0)) == MAP_FAILED)
err(EX_IOERR, "Can't mmap() file");

printf("<%c>\n",* (char *)diskp);
* (char *)diskp = '!';

if (msync(diskp, 0, MS_SYNC) || close(fd))
err(EX_IOERR, "Error closing file");

All proceeds w/o errors with the sample (2 bytes long) file, printf()
shows actual first byte of my file. But modification doesn't get written back
to the disk, file contents are unchanged after execution of my code. I'm sure
I'm overlooking something very basic and stupid, but can't find what exactly.


munmap(2)?

--
Stanislav Sedov MBSD labs, Inc. <ssedov@xxxxxxxxxxx>
Россия, Москва http://mbsd.msk.ru

--------------------------------------------------------------------
If the facts don't fit the theory, change the facts. -- A. Einstein
--------------------------------------------------------------------
PGP fingerprint: F21E D6CC 5626 9609 6CE2 A385 2BF5 5993 EB26 9581

Attachment: pgptDc9hgpRWB.pgp
Description: PGP signature



Relevant Pages

  • Simple question about mmap() system call
    ... I'm writing an utility that should examine some bytes of a large file ... and modify them - that't all. ... I've decided to mmap() the file: ... void *diskp; ...
    (freebsd-hackers)
  • Is it possible to find the computer name of a logged user?
    ... I'm writing a trigger that will modify some fields in a record to ... reflect some facts like the last user that modified the record and the ...
    (microsoft.public.sqlserver.programming)
  • Re: Curiosity - how fast do professional writers write?
    ... >>I mean writing about a topic that has personal and emotional import. ... >>The facts have no meaning without ... >Emotion doesn't play into that. ... It's what I say to blog writers. ...
    (misc.writing)
  • Re: The Pescadero Award
    ... > street and beaten by an angry mob for writing.. ... >:Bill Lang wrote: ... >:> It's time to face the facts my friends. ...
    (rec.sport.football.college)
  • Re: Thomas Covenant series
    ... My apologies. ... separate set of assumed facts -- in quotes no less. ... of writing as well. ...
    (rec.arts.sf.composition)