Thread: Posix threads synchronisation with semaphores

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    72

    Posix threads synchronisation with semaphores

    Hi all,

    can someone explain for me the output of this program :
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pthread.h>
    #include <semaphore.h>
    
    void *thread_function(void *arg);
    sem_t bin_sem;
    
    #define WORK_SIZE 1024
    char work_area[WORK_SIZE];
    
    int main() {
       int res;
       pthread_t a_thread;
       void *thread_result;
    
       res = sem_init(&bin_sem, 0, 0);
       if (res != 0) {
           perror("Semaphore initialization failed");
           exit(EXIT_FAILURE);
       }
       res = pthread_create(&a_thread, NULL, thread_function, NULL);
       if (res != 0) {
           perror("Thread creation failed");
           exit(EXIT_FAILURE);
       }
    
       printf("Input some text. Enter 'end' to finish\n");
       while(strncmp("end", work_area, 3) != 0) {
         if (strncmp(work_area, "FAST", 4) == 0) {
           sem_post(&bin_sem);
           strcpy(work_area, "Wheeee...");
         } else {
           fgets(work_area, WORK_SIZE, stdin);
         }
         sem_post(&bin_sem);
       }
    
       printf("\nWaiting for thread to finish...\n");
       res = pthread_join(a_thread, &thread_result);
       if (res != 0) {
           perror("Thread join failed");
           exit(EXIT_FAILURE);
       }
       printf("Thread joined\n");
       sem_destroy(&bin_sem);
       exit(EXIT_SUCCESS);
    }
    
    void *thread_function(void *arg) {
       sem_wait(&bin_sem);
       while(strncmp("end", work_area, 3) != 0) {
           printf("You input %d characters\n", strlen(work_area) -1);
           sem_wait(&bin_sem);
       }
       pthread_exit(NULL);
    }
    when you compile :
    Code:
    gcc -Wall -D_REENTRANT thread1.c -o thread1 -lpthread
    and run it :
    Code:
    ./thread1
    Input some text. Enter 'end' to finish
    hello
    You input 5 characters
    i m a thread
    You input 12 characters
    FAST
    You input 8 characters
    You input 8 characters
    You input 8 characters
    end
    
    Waiting for thread to finish...
    Thread joined
    and this is not clear for me.

    Thanks for your help.

    Regards.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    In the future, please explain what is not clear. It's good that you showed the output of your program, but you did not explain why it is incorrect. I can only assume that you're confused about the fact that FAST results in "You printed.." being shown 3 times, but I really don't know.

    I'll have to assume that's the case. When you type FAST, you cause sem_post() to be called three times, which causes sem_wait() to wake up three times.

    a) You type in FAST: the sem_post() after your if/else block is called.
    b) Your strncmp() (which could just be a strcmp()) matches FAST, so sem_post() in that block is called.
    c) The sem_post() from (a) is called again

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Thanks cas for the reply, i'm sorry about explaining what is not clear. But i'm very confused about this output:

    Code:
    FAST
    You input 8 characters
    You input 8 characters
    You input 8 characters
    I was expecting just one time this sequence but i got it three times so it's not clear at all.

    Thanks again.

    Regards.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Officially, you have undefined behavior due to unsynchronized access to "work_area".

    >> I was expecting just one ... but i got it three times ...
    You see the message 3 times because main() called sem_post() 3 times. cas identified all 3 calls in "a) b) c)" of post #2.

    gg

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Thanks for your reply Codeplug but i don't understand the call in a) and c) from the reply of cas? When i type FAST i increment the semaphore so the thread_function executes and ouput 3!!! right?

    Thanks.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    1) "FAST" is entered, fgets() returns, POST
    2) loop to top of while loop, work_area == "FAST", POST
    3) bottom of while loop is reached, POST

    There are your 3 sem_post() calls.

    4) loop to top of while loop, call fgets() and wait for input

    In the meantime, thread_function() processes all 3 sem_post() calls, and see's "Wheeee...".

    gg

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    That's much better , thank you very much codeplug.

    Regards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. threads using semaphores not working
    By davidelias16 in forum C Programming
    Replies: 4
    Last Post: 10-18-2010, 08:35 AM
  2. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  3. POSIX threads
    By Boylett in forum Linux Programming
    Replies: 2
    Last Post: 09-10-2008, 01:33 PM
  4. threads and semaphores
    By jas_atwal in forum Linux Programming
    Replies: 1
    Last Post: 12-25-2007, 07:33 PM
  5. POSIX threads policy and priority
    By arunj in forum Linux Programming
    Replies: 2
    Last Post: 06-13-2006, 03:48 AM