title: pthread_create fails with ENOMEM on low free memory scenario
body: <p>I have a SH4 board, here are the specs...</p>
  
  <pre><code>uname -a
  Linux LINUX7109 2.6.23.17_stm23_A18B-HMP_7109-STSDK #1 PREEMPT Fri Aug 6 16:08:19 ART 2010
  sh4 unknown
  </code></pre>
  
  <p>and suppose I have eaten pretty much all the memory, and have only 9 MB left.</p>
  
  <pre><code>free
   total       used       free     shared    buffers     cached
  Mem:         48072      42276       5796          0        172       3264
  -/+ buffers/cache:      38840       9232
  Swap:            0          0          0
  </code></pre>
  
  <p>Now, when I try to launch a single thread with default stack size (8 MB)
  the pthread_create fails with ENOMEM. If I strace my test code, I can see that the function that is failing is mmap:</p>
  
  <pre><code>old_mmap(NULL, 8388608, PROT_READ|PROT_WRITE|PROT_EXEC,                       
  MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory)
  </code></pre>
  
  <p>However, when I set the stack size to a lower value using ulimit -s:</p>
  
  <pre><code>ulimit -s 7500
  </code></pre>
  
  <p>I can now launch 10 threads. Each thread does not allocate anything, so it 
  is only consuming the minimum overhead (aprox. 8 kb per thread, right?).</p>
  
  <p>So, my question is:</p>
  
  <p>Knowing that mmap doesnt actually consume the memory, 
  Why is pthread_create() (or mmap) failing when memory available is below 
  the thread stack size ?</p>