title: understand process memory map in linux
body: <p>I am trying to understand the basic of linux process memory layout and i got this program:</p>
  
  <pre><code>#include &lt;stdio.h&gt; // standard io
  #include &lt;stdlib.h&gt; // C standard library
  #include &lt;pthread.h&gt; // threading
  #include &lt;unistd.h&gt; // unix standard library
  #include &lt;sys/types.h&gt; // system types for linux
  
  // getchar basically is like "read"
  // it prompts the user for input
  // in this case, the input is thrown away
  // which makes similar to a "pause" continuation primitive 
  // but a pause that is resolved through user input, which we promptly throw away!
  void * thread_func (void * arg) {
  
      printf("Before malloc in thread 1\n");
      getchar();
      char * addr = (char *) malloc(1000);
      printf("After malloc and before free in thread 1\n");
      getchar();
      free(addr);
      printf("After free in thread 1\n");
      getchar();
  
  }
  
  int main () {
  
      char * addr;
      printf("Welcome to per thread arena example::%d\n", getpid());
      printf("Before malloc in the main thread\n");
      getchar();
      addr = (char *) malloc(1000);
      printf("After malloc and before free in main thread\n");
      getchar();
      free(addr);
      printf("After free in main thread\n");
      getchar();
  
      // pointer to the thread 1
      pthread_t thread_1;
      // pthread_* functions return 0 upon succeeding, and other numbers upon failing
      int pthread_status;
  
      pthread_status = pthread_create(&amp;thread_1, NULL, thread_func, NULL);
  
      if (pthread_status != 0) {
          printf("Thread creation error\n");
          return -1;
      }
  
      // returned status code from thread_1
      void * thread_1_status;
  
      pthread_status = pthread_join(thread_1, &amp;thread_1_status);
  
      if (pthread_status != 0) {
          printf("Thread join error\n");
          return -1;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>When I started the program, the content in <code>/proc/&lt;pid&gt;/maps</code> is:</p>
  
  <pre><code>00400000-00401000 r-xp 00000000 08:01 1323314                            /home/oscp/xg/c/memory_layout/a.out
  00600000-00601000 r--p 00000000 08:01 1323314                            /home/oscp/xg/c/memory_layout/a.out
  00601000-00602000 rw-p 00001000 08:01 1323314                            /home/oscp/xg/c/memory_layout/a.out
  7fcc372d7000-7fcc37491000 r-xp 00000000 08:01 1053757                    /lib/x86_64-linux-gnu/libc-2.19.so
  7fcc37491000-7fcc37691000 ---p 001ba000 08:01 1053757                    /lib/x86_64-linux-gnu/libc-2.19.so
  7fcc37691000-7fcc37695000 r--p 001ba000 08:01 1053757                    /lib/x86_64-linux-gnu/libc-2.19.so
  7fcc37695000-7fcc37697000 rw-p 001be000 08:01 1053757                    /lib/x86_64-linux-gnu/libc-2.19.so
  7fcc37697000-7fcc3769c000 rw-p 00000000 00:00 0 
  7fcc3769c000-7fcc376b5000 r-xp 00000000 08:01 1053877                    /lib/x86_64-linux-gnu/libpthread-2.19.so
  7fcc376b5000-7fcc378b4000 ---p 00019000 08:01 1053877                    /lib/x86_64-linux-gnu/libpthread-2.19.so
  7fcc378b4000-7fcc378b5000 r--p 00018000 08:01 1053877                    /lib/x86_64-linux-gnu/libpthread-2.19.so
  7fcc378b5000-7fcc378b6000 rw-p 00019000 08:01 1053877                    /lib/x86_64-linux-gnu/libpthread-2.19.so
  7fcc378b6000-7fcc378ba000 rw-p 00000000 00:00 0 
  7fcc378ba000-7fcc378dd000 r-xp 00000000 08:01 1053733                    /lib/x86_64-linux-gnu/ld-2.19.so
  7fcc37abe000-7fcc37ac1000 rw-p 00000000 00:00 0 
  7fcc37ad8000-7fcc37adc000 rw-p 00000000 00:00 0 
  7fcc37adc000-7fcc37add000 r--p 00022000 08:01 1053733                    /lib/x86_64-linux-gnu/ld-2.19.so
  7fcc37add000-7fcc37ade000 rw-p 00023000 08:01 1053733                    /lib/x86_64-linux-gnu/ld-2.19.so
  7fcc37ade000-7fcc37adf000 rw-p 00000000 00:00 0 
  7ffdc1cff000-7ffdc1d20000 rw-p 00000000 00:00 0                          [stack]
  7ffdc1dd8000-7ffdc1ddb000 r--p 00000000 00:00 0                          [vvar]
  7ffdc1ddb000-7ffdc1ddd000 r-xp 00000000 00:00 0                          [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  </code></pre>
  
  <p>What are the purposes of these memory regions?</p>
  
  <pre><code>7fcc37491000-7fcc37691000 ---p 001ba000 08:01 1053757                    /lib/x86_64-linux-gnu/libc-2.19.so
  ...
  7fcc37abe000-7fcc37ac1000 rw-p 00000000 00:00 0 
  7fcc37ad8000-7fcc37adc000 rw-p 00000000 00:00 0 
  </code></pre>
  
  <hr>
  
  <p>Then I press enter a few times after running the program. After it prints "Before malloc in thread 1". The memory layout looks like below:</p>
  
  <pre><code>00400000-00401000 r-xp 00000000 08:01 1323314                            /home/oscp/xg/c/memory_layout/a.out
  00600000-00601000 r--p 00000000 08:01 1323314                            /home/oscp/xg/c/memory_layout/a.out
  00601000-00602000 rw-p 00001000 08:01 1323314                            /home/oscp/xg/c/memory_layout/a.out
  00632000-00653000 rw-p 00000000 00:00 0                                  [heap]
  7fcc36ad6000-7fcc36ad7000 ---p 00000000 00:00 0 
  7fcc36ad7000-7fcc372d7000 rw-p 00000000 00:00 0 
  7fcc372d7000-7fcc37491000 r-xp 00000000 08:01 1053757                    /lib/x86_64-linux-gnu/libc-2.19.so
  7fcc37491000-7fcc37691000 ---p 001ba000 08:01 1053757                    /lib/x86_64-linux-gnu/libc-2.19.so
  7fcc37691000-7fcc37695000 r--p 001ba000 08:01 1053757                    /lib/x86_64-linux-gnu/libc-2.19.so
  7fcc37695000-7fcc37697000 rw-p 001be000 08:01 1053757                    /lib/x86_64-linux-gnu/libc-2.19.so
  7fcc37697000-7fcc3769c000 rw-p 00000000 00:00 0 
  7fcc3769c000-7fcc376b5000 r-xp 00000000 08:01 1053877                    /lib/x86_64-linux-gnu/libpthread-2.19.so
  7fcc376b5000-7fcc378b4000 ---p 00019000 08:01 1053877                    /lib/x86_64-linux-gnu/libpthread-2.19.so
  7fcc378b4000-7fcc378b5000 r--p 00018000 08:01 1053877                    /lib/x86_64-linux-gnu/libpthread-2.19.so
  7fcc378b5000-7fcc378b6000 rw-p 00019000 08:01 1053877                    /lib/x86_64-linux-gnu/libpthread-2.19.so
  7fcc378b6000-7fcc378ba000 rw-p 00000000 00:00 0 
  7fcc378ba000-7fcc378dd000 r-xp 00000000 08:01 1053733                    /lib/x86_64-linux-gnu/ld-2.19.so
  7fcc37abe000-7fcc37ac1000 rw-p 00000000 00:00 0 
  7fcc37ad8000-7fcc37adc000 rw-p 00000000 00:00 0 
  7fcc37adc000-7fcc37add000 r--p 00022000 08:01 1053733                    /lib/x86_64-linux-gnu/ld-2.19.so
  7fcc37add000-7fcc37ade000 rw-p 00023000 08:01 1053733                    /lib/x86_64-linux-gnu/ld-2.19.so
  7fcc37ade000-7fcc37adf000 rw-p 00000000 00:00 0 
  7ffdc1cff000-7ffdc1d20000 rw-p 00000000 00:00 0                          [stack]
  7ffdc1dd8000-7ffdc1ddb000 r--p 00000000 00:00 0                          [vvar]
  7ffdc1ddb000-7ffdc1ddd000 r-xp 00000000 00:00 0                          [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  </code></pre>
  
  <p>What are the purposes of these two regions?</p>
  
  <pre><code>7fcc36ad6000-7fcc36ad7000 ---p 00000000 00:00 0 
  7fcc36ad7000-7fcc372d7000 rw-p 00000000 00:00 0 
  </code></pre>
  
  <p>After it prints "After malloc and before free in thread 1", it creates another two regions below:</p>
  
  <pre><code>7fcc30000000-7fcc30021000 rw-p 00000000 00:00 0 
  7fcc30021000-7fcc34000000 ---p 00000000 00:00 0 
  </code></pre>
  
  <p>What are the purposes of these two regions?</p>