2

I'm new to C and I'm just trying to run this piece of code of Pacheco's book "An Introduction to Parallel Programming". And I just can't figure out how I should implement the semaphores initialization in main(). Are &semaphores[dest] and &semaphores[my_rank] arrays?

C 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(&semaphores[dest])
    /* ‘‘Unlock’’ the semaphore of dest */

    sem_wait(&semaphores[my_rank]);/* Wait for our semaphore to be unlocked */
    printf("Thread %ld > %s\n", my_rank, messages[my_rank]);

    return NULL;
} /* Send_msg */

I want to implement this type of order mechanism as I am trying to write output array to a file. Here is my code:

int k = 0;
long dest = (my_rank + 1) % THREAD_COUNT;

if(genCount%50==0){
//incrementing file name

//semapthore lock
sem_post(&semaphores[dest]);

sem_wait(&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 < my_last_i; loop1++){
  for(loop2 = my_first_i; loop2 < my_last_i; loop2++){
fprintf(fp,"%d\t", map[loop1][loop2]);
  }
}

//close the file
fclose(fp);

k++;

}

The initialization of semaphors in the main():

  int semCount;
  for(semCount = 0; semCount < THREAD_COUNT; semCount++){
   sem_init(&semaphores[semCount], 0, 1);
  }

I also have it as a global variable:

#define THREAD_COUNT 4
sem_t semaphores[THREAD_COUNT];

The program compiles, but it gives me this error:

      *** 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

Any help would be appreciated.

Jakub Badacz
  • 63
  • 1
  • 7
  • Read `man sem_init` and ask further. – Shiv Dec 14 '16 at 13:39
  • 2
    What kind of C is that? It seems the `//` becomes `/` and the `_` are missing... Anyway `[]` is the array subscript operator, to answer your question. – Déjà vu Dec 14 '16 at 13:39
  • 1
    It is a bad copy from some ebook or scan. Do you notice the unlockquotes. Those are printing quotes not code quotes of a monospace font. Underscores missing. The OCR has a fail implementation. – Shiv Dec 14 '16 at 13:40
  • @Shiv Yea, sorry didn't spot that. Fixed it now. I've tried `sem_init(&semaphores, 1, 0);` but that didn't work. – Jakub Badacz Dec 14 '16 at 13:47
  • `semaphores` is an array of semaphores. `dest` and `my_rank` are indexes into that array. `&semaphores[dest]` is the address of the `dest` element of array `semaphores`. – Josh Sanford Dec 14 '16 at 13:49
  • I'm sure that `return NULL;` isn't an OCR error - if that compiles without warnings, you've misconfigured your compilation... – Toby Speight Dec 14 '16 at 13:55
  • also your declaration of `Send_msg` is weird as in `Send_msg(void rank)` should be `Send_msg(void *rank)` ... It's always better to cut and paste the code and then use `{}` button in the editor to format it rather than retyping it. – Ahmed Masud Dec 14 '16 at 14:25
  • @AhmedMasud Yes, you're right. I copied it from pdf and it didn't copy, all characters. – Jakub Badacz Dec 14 '16 at 15:49
  • @TobySpeight what I'm getting is this: `error: subscripted value is neither array nor pointer nor vector` and its pointing to the `[` to this piece of code `sem_post(&semaphores[dest]);` and `sem_wait(&semaphores[my_rank]);` – Jakub Badacz Dec 14 '16 at 15:54
  • 1
    You should [edit] your question to add the additional information, rather than just using a comment. – Toby Speight Dec 14 '16 at 16:51
  • also your question is not http://stackoverflow.com/help/mvce ... I cannot copy and paste your stuff to simply compile it. – Ahmed Masud Dec 14 '16 at 19:15

1 Answers1

2

looks like semaphores defined this way

sem_t semaphores[N];

where N is number of semaphores

For initializing a semaphore you should use sem_init function which takes a pointer to desired semaphore, a flag which enables shared process usage and initial value of the semaphore.

sem_init(&semaphores[0], 0, 1);

In your case the semaphore instances stored in array called semaphores, so you have to iterate on all members of array to initialize

int i;
for(i=0;i<N;i++)
    sem_init(&semaphores[i], 0, 1);

Finlay for using semaphores you have to call sem_wait and sem_post functions this way

sem_wait(&semaphores[0 to N-1]); // Locking semaphore
sem_post(&semaphores[0 to N-1]); // Unlocking it
e.jahandar
  • 1,715
  • 12
  • 30
  • Oh okay, I get your point. So does that mean that I should have one semaphore for each thread? – Jakub Badacz Dec 14 '16 at 16:02
  • 1
    Semaphores are used to protect the data in the critical sections, for example consider producer-consumer problem, one thread is producing some data and consumer threads are consuming them, now consider two threads are accessing same variable or same section of data at the same time! it may break the consistency or troubling producer-consumer operation – e.jahandar Dec 14 '16 at 17:07
  • 1
    So you have to protect the data, for example with semaphores. you can put a semaphore lock before accessing critical section (shared data between two threads) and unlocking when you have finished working with them. when a semaphore is locked, no other lock will be placed. Actually first locker thread will continue to work and second and other locker threads will block until first locker thread finishes it works and unlocks the semaphore. Note that, two threads must lock on same semaphore. – e.jahandar Dec 14 '16 at 17:10
  • Thank you. That explains alot. I've implemented your solution. It compiles, but it gives me an error when it access the data. I've updated my question with the error. – Jakub Badacz Dec 14 '16 at 17:46
  • 1
    The errors are irrelevant to the codes, please put full code here or ask a new question. The error is about trying to free malloc'ed region which freed previously (try to free a region twice) – e.jahandar Dec 14 '16 at 18:04
  • Here is the link to new question: http://stackoverflow.com/questions/41150014/error-while-compiling-the-code-double-free-or-corruption-out-using-threads-i – Jakub Badacz Dec 14 '16 at 18:52