title: Creating and deleting files from C/C++
body: <p>I seem to be confused about how file removal works on Linux (same behavior on Mac OS X). The following code writes a 1GB file and then immediately deletes it. It does so 1024 times. I thought that since the file is closed, it would be immediately removed (space reclaimed and maybe even the file descriptor recycled). Instead, if I look at <code>lsof</code>, the file is still claimed as owned by the process (with (deleted) appended to it). If I look at <code>df</code>, the space is still used up. Eventually, the process dies from either exhausting the number of allowed open files, a limit I can raise, or just running out of space.</p>
  
  <p>What's the right way to do this? Can I actually delete the file before the process terminates (and reclaim its space), or is this a hopeless exercise and I need another design for my code?</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cstdio&gt;
  #include &lt;fcntl.h&gt;
  
  #include &lt;iostream&gt;
  
  int main()
  {
      size_t n = 1024*1024*1024;
      void* buffer = malloc(n);
      std::cout &lt;&lt; "Buffer created" &lt;&lt; std::endl;
  
      for (unsigned i = 0; i &lt; 1024; ++i)
      {
          char filename[50] = "STORAGE.XXXXXX";
          mkstemp(filename);
          std::cout &lt;&lt; filename &lt;&lt; std::endl;
          int fh = open(filename, O_WRONLY | O_SYNC, 0600);
          write(fh, buffer, n);
          close(fh);
          remove(filename);
      }
      free(buffer);
      sleep(60);
  }
  </code></pre>
  
  <p>lsof:</p>
  
  <pre><code>test-remo 29251 ...    3u   REG   8,51 1073741824  9307914 .../tmp/STORAGE.Tyj4oG (deleted)
  test-remo 29251 ...    4u   REG   8,51 1073741824  9308254 .../tmp/STORAGE.fBkF5b (deleted)
  test-remo 29251 ...    5u   REG   8,51 1073741824  9308030 .../tmp/STORAGE.xfJ9P0 (deleted)
  test-remo 29251 ...    6u   REG   8,51 1073741824  9308538 .../tmp/STORAGE.0C6oea (deleted)
  test-remo 29251 ...    7u   REG   8,51 1073741824  9306147 .../tmp/STORAGE.w9dPzC (deleted)
  test-remo 29251 ...    8u   REG   8,51 1073741824  9308311 .../tmp/STORAGE.mIwcpq (deleted)
  test-remo 29251 ...    9u   REG   8,51 1073741824  9309004 .../tmp/STORAGE.srTZfy (deleted)
  test-remo 29251 ...   10u   REG   8,51 1073741824  9309284 .../tmp/STORAGE.xjQQ4Y (deleted)
  test-remo 29251 ...   11u   REG   8,51 1073741824  9309291 .../tmp/STORAGE.ioyPxI (deleted)
  test-remo 29251 ...   12u   REG   8,51 1073741824  9307415 .../tmp/STORAGE.saYo2L (deleted)
  test-remo 29251 ...   13u   REG   8,51 1073741824  9307810 .../tmp/STORAGE.ylW467 (deleted)
  test-remo 29251 ...   14u   REG   8,51 1073741824  9308666 .../tmp/STORAGE.lUYnWN (deleted)
  test-remo 29251 ...   15u   REG   8,51 1073741824  9309678 .../tmp/STORAGE.1rZM4M (deleted)
  test-remo 29251 ...   16u   REG   8,51 1073741824  9309702 .../tmp/STORAGE.wdggra (deleted)
  test-remo 29251 ...   17u   REG   8,51 1073741824  9309687 .../tmp/STORAGE.qczYAS (deleted)
  test-remo 29251 ...   18u   REG   8,51 1073741824  9309732 .../tmp/STORAGE.u0IxKV (deleted)
  test-remo 29251 ...   19u   REG   8,51 1073741824  9309708 .../tmp/STORAGE.6SiMpi (deleted)
  test-remo 29251 ...   20u   REG   8,51 1073741824  9308649 .../tmp/STORAGE.URky5Z (deleted)
  test-remo 29251 ...   21u   REG   8,51 1073741824  9309740 .../tmp/STORAGE.j8wEq2 (deleted)
  test-remo 29251 ...   22u   REG   8,51 1073741824  9309677 .../tmp/STORAGE.Wj5rco (deleted)
  test-remo 29251 ...   23u   REG   8,51 1073741824  9309828 .../tmp/STORAGE.I38ln3 (deleted)
  test-remo 29251 ...   24u   REG   8,51 1073741824  9309839 .../tmp/STORAGE.ent8I1 (deleted)
  test-remo 29251 ...   25u   REG   8,51 1073741824  9309843 .../tmp/STORAGE.1361nk (deleted)
  test-remo 29251 ...   26u   REG   8,51 1073741824  9309824 .../tmp/STORAGE.ue2WkW (deleted)
  test-remo 29251 ...   27u   REG   8,51 1073741824  9312037 .../tmp/STORAGE.4MdKsR (deleted)
  test-remo 29251 ...   28u   REG   8,51 1073741824  9312042 .../tmp/STORAGE.EgDft5 (deleted)
  test-remo 29251 ...   29u   REG   8,51 1073741824  9312059 .../tmp/STORAGE.NRcqlE (deleted)
  test-remo 29251 ...   30u   REG   8,51 1073741824  9309847 .../tmp/STORAGE.cF9qYw (deleted)
  test-remo 29251 ...   31u   REG   8,51 1073741824  9312103 .../tmp/STORAGE.wzfhSI (deleted)
  test-remo 29251 ...   32u   REG   8,51 1073741824  9312120 .../tmp/STORAGE.hfdv1d (deleted)
  test-remo 29251 ...   33u   REG   8,51 1073741824  9312090 .../tmp/STORAGE.kSX2D4 (deleted)
  test-remo 29251 ...   34u   REG   8,51 1073741824  9309813 .../tmp/STORAGE.FWKVGf (deleted)
  test-remo 29251 ...   35u   REG   8,51 1073741824  9312148 .../tmp/STORAGE.CYPGGL (deleted)
  test-remo 29251 ...   36u   REG   8,51 1073741824  9312171 .../tmp/STORAGE.it4bmC (deleted)
  test-remo 29251 ...   37u   REG   8,51 1073741824  9312145 .../tmp/STORAGE.IGSzVO (deleted)
  test-remo 29251 ...   38u   REG   8,51 1073741824  9312228 .../tmp/STORAGE.CMSOil
  </code></pre>