title: Controlling thread order using semaphores in C
body: <p>I'm new to C and I'm just trying to run this piece of code of Pacheco's book "<em>An Introduction to Parallel Programming</em>". And I just can't figure out how I should implement the semaphores initialization in <code>main()</code>. Are <code>&amp;semaphores[dest]</code> and <code>&amp;semaphores[my_rank]</code> arrays?</p>
  
  <p>C code:</p>
  
  <pre><code>/* messages is allocated and initialized to NULL in main */
  /* semaphores is allocated and initialized to 0 (locked) in main */
  void *Send_msg(void* rank) {
      long my_rank = (long) rank;
      long dest = (my_rank + 1) % thread_count;
      char* my_msg = (char*)malloc(MSG_MAX*sizeof(char));
  
      sprintf(my_msg, "Hello to %ld from %ld", dest, my_rank);
      messages[dest] = my_msg;
      sem_post(&amp;semaphores[dest])
      /* ‘‘Unlock’’ the semaphore of dest */
  
      sem_wait(&amp;semaphores[my_rank]);/* Wait for our semaphore to be unlocked */
      printf("Thread %ld &gt; %s\n", my_rank, messages[my_rank]);
  
      return NULL;
  } /* Send_msg */
  </code></pre>
  
  <p>I want to implement this type of order mechanism as I am trying to write output array to a file. Here is my code:</p>
  
  <pre><code>int k = 0;
  long dest = (my_rank + 1) % THREAD_COUNT;
  
  if(genCount%50==0){
  //incrementing file name
  
  //semapthore lock
  sem_post(&amp;semaphores[dest]);
  
  sem_wait(&amp;semaphores[my_rank]);
  
  snprintf(buffer, sizeof(char) * 32, "file%i.dat", k);
  
  //open the file
  fp = fopen(buffer, "r+");
  if (fp == NULL) {
    printf("I couldn't open file for writing.\n");
    exit(0);
  }
  
  int loop1, loop2;
  
  //outputting the array into the file
  for (loop1 = my_first_i; loop1 &lt; my_last_i; loop1++){
    for(loop2 = my_first_i; loop2 &lt; my_last_i; loop2++){
  fprintf(fp,"%d\t", map[loop1][loop2]);
    }
  }
  
  //close the file
  fclose(fp);
  
  k++;
  </code></pre>
  
  <p>}</p>
  
  <p>The initialization of semaphors in the <code>main()</code>:</p>
  
  <pre><code>  int semCount;
    for(semCount = 0; semCount &lt; THREAD_COUNT; semCount++){
     sem_init(&amp;semaphores[semCount], 0, 1);
    }
  </code></pre>
  
  <p>I also have it as a global variable:</p>
  
  <pre><code>#define THREAD_COUNT 4
  sem_t semaphores[THREAD_COUNT];
  </code></pre>
  
  <p>The program compiles, but it gives me this error:</p>
  
  <pre><code>      *** Error in `./ebola_serial': double free or corruption (out): 0x00007f49700008c0 ***
    *** Error in `======= Backtrace: =========
    /lib64/libc.so.6(+0x7238e)[0x7f49868f038e]
    ./ebola_serial/lib64/libc.so.6(+0x7a0c8)[0x7f49868f80c8]
    ': /lib64/libc.so.6(cfree+0x48)[0x7f49868fb798]
    double free or corruption (out)/lib64/libc.so.6(fclose+0x113)[0x7f49868e6473]
    ./ebola_serial[0x401717]
    /lib64/libpthread.so.0(+0x75bd)[0x7f4986c395bd]
    : 0x/lib64/libc.so.6(clone+0x6d)[0x7f498697562d]
    ======= Memory map: ========
    00007f49700008c000400000-00402000 r-xp 00000000 08:01 802698                             /home/name/Desktop/try ca/ebola_serial
    00601000-00602000 r--p 00001000 08:01 802698                             /home/name/Desktop/try ca/ebola_serial
    00602000-00603000 rw-p 00002000 08:01 802698                             /home/name/Desktop/try ca/ebola_serial
    00603000-009d3000 rw-p 00000000 00:00 0 
    01b96000-01bb7000 rw-p 00000000 00:00 0                                  [heap]
    7f4970000000-7f4970021000 rw-p 00000000 00:00 0 
    7f4970021000-7f4974000000 ---p 00000000 00:00 0 
    7f4978000000-7f4978021000 rw-p 00000000 00:00 0 
    7f4978021000-7f497c000000 ---p 00000000 00:00 0 
    7f497c000000-7f497c021000 rw-p 00000000 00:00 0 
    7f497c021000-7f4980000000 ---p 00000000 00:00 0 
    7f4980000000-7f4980021000 rw-p 00000000 00:00 0 
    7f4980021000-7f4984000000 ---p 00000000 00:00 0 
    7f4984663000-7f4984679000 r-xp 00000000 08:01 131531                     /usr/lib64/libgcc_s-4.9.2.so.1
    7f4984679000-7f4984878000 ---p 00016000 08:01 131531                     /usr/lib64/libgcc_s-4.9.2.so.1
    7f4984878000-7f4984879000 r--p 00015000 08:01 131531                     /usr/lib64/libgcc_s-4.9.2.so.1
    7f4984879000-7f498487a000 rw-p 00016000 08:01 131531                     /usr/lib64/libgcc_s-4.9.2.so.1
    7f498487a000-7f498487b000 ---p 00000000 00:00 0 
    7f498487b000-7f498507b000 rw-p 00000000 00:00 0                          [stack:7376]
    7f498507b000-7f498507c000 ---p 00000000 00:00 0 
    7f498507c000-7f498587c000 rw-p 00000000 00:00 0 
    7f498587c000-7f498587d000 ---p 00000000 00:00 0 
    7f498587d000-7f498607d000 rw-p 00000000 00:00 0                          [stack:7374]
    7f498607d000-7f498607e000 ---p 00000000 00:00 0 
    7f498607e000-7f498687e000 rw-p 00000000 00:00 0                          [stack:7373]
    7f498687e000-7f4986a28000 r-xp 00000000 08:01 130947                     /usr/lib64/libc-2.20.so
    7f4986a28000-7f4986c28000 ---p 001aa000 08:01 130947                     /usr/lib64/libc-2.20.so
    7f4986c28000-7f4986c2c000 r--p 001aa000 08:01 130947                     /usr/lib64/libc-2.20.so
    7f4986c2c000-7f4986c2e000 rw-p 001ae000 08:01 130947                     /usr/lib64/libc-2.20.so
    7f4986c2e000-7f4986c32000 rw-p 00000000 00:00 0 
    7f4986c32000-7f4986c49000 r-xp 00000000 08:01 130973                     /usr/lib64/libpthread-2.20.so
    7f4986c49000-7f4986e48000 ---p 00017000 08:01 130973                     /usr/lib64/libpthread-2.20.so
    7f4986e48000-7f4986e49000 r--p 00016000 08:01 130973                     /usr/lib64/libpthread-2.20.so
    7f4986e49000-7f4986e4a000 rw-p 00017000 08:01 130973                     /usr/lib64/libpthread-2.20.so
    7f4986e4a000-7f4986e4e000 rw-p 00000000 00:00 0 
    7f4986e4e000-7f4986f53000 r-xp 00000000 08:01 130955                     /usr/lib64/libm-2.20.so
    7f4986f53000-7f4987152000 ---p 00105000 08:01 130955                     /usr/lib64/libm-2.20.so
    7f4987152000-7f4987153000 r--p 00104000 08:01 130955                     /usr/lib64/libm-2.20.so
    7f4987153000-7f4987154000 rw-p 00105000 08:01 130955                     /usr/lib64/libm-2.20.so
    7f4987154000-7f4987174000 r-xp 00000000 08:01 130940                     /usr/lib64/ld-2.20.so
    7f4987353000-7f4987356000 rw-p 00000000 00:00 0 
    7f498736d000-7f4987373000 rw-p 00000000 00:00 0 
    7f4987373000-7f4987374000 r--p 0001f000 08:01 130940                     /usr/lib64/ld-2.20.so
    7f4987374000-7f4987376000 rw-p 00020000 08:01 130940                     /usr/lib64/ld-2.20.so
    7ffce31e3000-7ffce3205000 rw-p 00000000 00:00 0                          [stack]
    7ffce320d000-7ffce320f000 r--p 00000000 00:00 0                          [vvar]
    7ffce320f000-7ffce3211000 r-xp 00000000 00:00 0                          [vdso]
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
    Aborted
  </code></pre>
  
  <p>Any help would be appreciated.</p>