title: write vs fprintf - why different and which is better?
body: <p>I have recently started learning the wonders of pthreads according to POSIX 1003.1c. 
  <p>
  PThreads may seem complex, but they are basically simple threads that we use in the class to create parallel behavior: <a href="https://computing.llnl.gov/tutorials/pthreads/">https://computing.llnl.gov/tutorials/pthreads/</a>
  <p>
  As I am still learning, my teacher gave us a C code to toy with:
  <p></p>
  
  <pre><code>/* Creates two threads, one printing 10000 "a"s, the other printing
     10000 "b"s.
     Illustrates: thread creation, thread joining. */
  
  #include &lt;stddef.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include "pthread.h"
  
  void * process(void * arg)
  {
    int i;
    fprintf(stderr, "Starting process %s\n", (char *) arg);
    for (i = 0; i &lt; 100; i++) {
         write(1, (char *) arg, 1);
  //      fprintf(stdout, (char *) arg, 1);
    }
    return NULL;
  }
  
  int main()
  {
    int retcode;
    pthread_t th_a, th_b;
    void * retval;
  
    retcode = pthread_create(&amp;th_a, NULL, process, "a");
    if (retcode != 0) fprintf(stderr, "create a failed %d\n", retcode);
  
    retcode = pthread_create(&amp;th_b, NULL, process, "b");
    if (retcode != 0) fprintf(stderr, "create b failed %d\n", retcode);
  
    retcode = pthread_join(th_a, &amp;retval);
    if (retcode != 0) fprintf(stderr, "join a failed %d\n", retcode);
  
    retcode = pthread_join(th_b, &amp;retval);
    if (retcode != 0) fprintf(stderr, "join b failed %d\n", retcode);
  
    return 0;
  }
  </code></pre>
  
  <ul>
  Instructions to run and compile (for linux):
   <li> Run command: `sudo apt-get install build-essential`</li>
   <li>Donwload this code (obviously xD)</li>
   <li>Use the following command to compile: `gcc -D_REENTRANT filenName.c -lpthread`</li>
   <li>Run the result by using the command: `./a.out`
  </ul>
  
  <p>Everything works but I don't understand why my output order is different depending on the use of <code>write</code> or <code>fprintf</code>.
  <p>
  When I use <code>write</code> I get a random output of letters like the following:</p>
  
  <pre><code>Starting process a
  aaaaaaaaaaaaaaaaaaaaaaaaaaaaStarting process b
  aaababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  </code></pre>
  
  <p>But when I use <code>fprintf</code> I always get the get an output similar to:</p>
  
  <pre><code>Starting process a
  Starting process b
  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaababbabaabaabaababbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  </code></pre>
  
  <p>In this case, the text "Starting process" always appears first and is not mixed with the rest of the output. Why is this happening? Is it because <code>write</code> is very fast and <code>fprintf</code> is slower?</p>
  
  <p>As a C programmer, which one should I use and why?</p>