Note that there are some explanatory texts on larger screens.

plurals
  1. POsegfault on write() with ~8MB buffer (OSX, Linux)
    text
    copied!<p>I was curious what kind of buffer sizes write() and read() could handle on Linux/OSX/FreeBSD, so I started playing around with dumb programs like the following:</p> <pre><code>#include &lt;unistd.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;sys/stat.h&gt; int main( void ) { size_t s = 8*1024*1024 - 16*1024; while( 1 ) { s += 1024; int f = open( "test.txt", O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR ); char mem[s]; size_t written = write( f, &amp;mem[0], s ); close( f ); printf( "(%ld) %lu\n", sizeof(size_t), written ); } return 0; } </code></pre> <p>This allowed me to test how close to a seeming "8MB barrier" I could get before segfaulting. Somewhere around the 8MB mark, my program dies, here's an example output:</p> <pre><code>(8) 8373248 (8) 8374272 (8) 8375296 (8) 8376320 (8) 8377344 (8) 8378368 (8) 8379392 (8) 8380416 (8) 8381440 (8) 8382464 Segmentation fault: 11 </code></pre> <p>This is the same on OSX and Linux, however my FreeBSD VM is not only much faster at running this test, it also can go on for quite a ways! I've successfully tested it up to 511MB, which is just a ridiculous amount of data to write in one call.</p> <p>What is it that makes the write() call segfault, and how can I figure out the maximum amount that I can possibly write() in a single call, without doing something ridiculous like I'm doing right now?</p> <p>(Note, all three operating systems are 64-bit, OSX 10.7.3, Ubuntu 11.10, FreeBSD 9.0)</p>
 

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