Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 23 Jun 2006 17:47:57 +0300 (EEST)
From:      Dmitry Pryanishnikov <dmitry@atlantis.dp.ua>
To:        freebsd-hackers@freebsd.org
Subject:   Simple question about mmap() system call
Message-ID:  <20060623173723.H39481@atlantis.atlantis.dp.ua>

next in thread | raw e-mail | index | archive | help

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.

Sincerely, Dmitry
-- 
Atlantis ISP, System Administrator
e-mail:  dmitry@atlantis.dp.ua
nic-hdl: LYNX-RIPE



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060623173723.H39481>