Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This works for me. Note that I had to open the destination O_RDWR. I suspect the kernel attempts to map whole pages from the file into memory (reading it) because you're updating it a byte or word at a time, and that might not change the whole page.</p> <p>A couple of other points:</p> <ol> <li><p>You don't need to close and unmap stuff on error if you're just going to exit.</p></li> <li><p>Use memcpy and don't write your own byte-copying loop. Memcpy will be a lot better optimised in general. (Though it's not always the absolute best.)</p></li> <li><p>You might want to read the source code to FreeBSD's "cp" utility. Take a look here and search for the use of mmap. <a href="http://svnweb.freebsd.org/base/stable/9/bin/cp/utils.c?revision=225736&amp;view=markup" rel="nofollow">http://svnweb.freebsd.org/base/stable/9/bin/cp/utils.c?revision=225736&amp;view=markup</a></p></li> </ol> <hr> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;fcntl.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/mman.h&gt; #include &lt;string.h&gt; #include &lt;sys/stat.h&gt; int main(int argc, char *argv[]) { int s, d; struct stat st; void *sp, *dp; s = open(argv[1], O_RDONLY); if (s == -1) { perror("open source"); exit(1); } d = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0644); if (d == -1) { perror("open destintation"); exit(1); } if (fstat(s, &amp;st)) { perror("stat source"); exit(1); } if (ftruncate(d, st.st_size)) { perror("truncate destination"); exit(1); } sp = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, s, 0); if (sp == MAP_FAILED) { perror("map source"); exit(1); } dp = mmap(NULL, st.st_size, PROT_WRITE | PROT_READ, MAP_SHARED, d, 0); if (dp == MAP_FAILED) { perror("map destintation"); exit(1); } memcpy(dp, sp, st.st_size); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload