Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, I have written a simple piece of code incorporating 4 functions (A, B, C, & D). However I need to solve a race condition problem using a single semaphore. ...
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    3

    Processes, Threads & single semaphore to solve race condition

    Hi,

    I have written a simple piece of code incorporating 4 functions (A, B, C, & D). However I need to solve a race condition problem using a single semaphore. My primary code is shown below:

    I have an idea of the code I need to insert into the body of the code below as follows:

    // Declare a semaphore called mutex A.
    sem_t mutex_a;
    main()
    {
    sem_init(& mutex_A 0, 1); // Initialise the console semaphore to value of 1.
    // Use the semaphores to provide mutual exclusion to the console.

    sem_wait( & mutex_A); // do a semaphore wait on mutex_A
    printf("I have exclusive access to the console!\n")
    sem_post ( & mutex_A); // do a semaphore post on mutex_A

    exit(0);
    }

    I have some questions in relation to the above with regard to what I need to do to the code below:

    1. Is the code above correct (AND) complete for the purposes of what I need to do and if it is correct where would I need to insert the code into the code below?
    2. If the code is correct BUT not complete do I need to replicate this code for mutex_B and mutex_c etc (given I have three functions/processes in my main code)? If I do need to add code for mutex_A and mutex_B etc to the above where should the complete semaphore code be inserted below?
    3. Is there any content in the above code (e.g. the word main()) that should be deleted prior to insertion into the code below?
    4. If the code is actually incorrect for what I need to solve (the race condition) can you please suggest suitable semaphore code using mutex to solve my problem?

    Code

    #include <stdio.h>
    #include <semaphore.h>
    #include <stdlib.h>

    // Functions - separate threads will call these functions.
    void *functionA();
    void *functionB();
    void *functionC();

    main ()
    {

    pthread_t thread1, thread2, thread3; // Declare three threads

    int rc1, rc2, rc3 ;

    // Here the console screen is partitioned into four sections
    printf ("\033[3J"); // Clear the screen
    printf ("\033[%d;%dH", 0, 0); // Set cursor position ( row 0, column 0)

    printf ("THREAD ONE\n") ;
    printf ("================================================ ===========\n");

    printf ("\033[%d;%dH", 8, 0 );
    printf ("THREAD TWO \n") ;
    printf ("================================================ ===========\n");

    printf ("\033[%d;%dH", 16, 0);
    printf ("THREAD THREE \n") ;
    printf ("================================================ ===========\n");

    printf ("\033[%d;%dH", 32, 0);
    printf ("THREAD FOUR \n") ;
    printf ("================================================ ===========\n");


    // Create first thread ... thread1
    if ( (rc1=pthread_create (& thread1, NULL, &functionA, NULL )))
    {printf ("Error in creating thread %d\n", rc1);}

    // Create second thread ... thread2
    if ( (rc2=pthread_create (& thread2, NULL, &functionB, NULL )))
    {printf ("Error in creating thread %d\n", rc2);}

    // Create third thread ... thread3
    if ( (rc3=pthread_create (& thread3, NULL, &functionC, NULL )))
    {printf ("Error in creating thread %d\n", rc3);}

    // Create fourth thread ... thread4
    if ( (rc4=pthread_create (& thread4, NULL, &functionD, NULL )))
    {printf ("Error in creating thread %d\n", rc4);}

    // The 'pthread_join' function causes main() to wait until a thread is properly created

    pthread_join ( thread1, NULL) ;
    pthread_join ( thread2, NULL) ;
    pthread_join ( thread3, NULL) ;
    pthread_join ( thread4, NULL) ;

    exit (0); // exit the main function
    }


    // This is thread1, uses functionA()
    void *functionA()
    {
    while (1)
    {
    printf ("\033[%d;%dH", 3, 0); // Set cursor position ( row 3, column 0)

    printf ("I am function 111111111111111111111111111111111111111111\n");
    }
    }


    // This is thread2, uses functionB()
    void *functionB()
    {
    while (1)
    {
    printf ("\033[%d;%dH", 11, 0); // Set cursor position ( row 11, column 0)

    printf ("I am function 222222222222222222222222222222222222222222\n");
    }
    }


    // This is thread3, uses functionC()
    void *functionC()
    {
    while (1)
    {
    printf ("\033[%d;%dH", 19, 0); // Set cursor position ( row 19, column 0)

    printf ("I am function 333333333333333333333333333333333333333333\n");
    }
    }
    Thank you so much for your kind help.

  2. #2
    Just Joined!
    Join Date
    Apr 2011
    Posts
    19

    Possible fix

    The short snippet of code below may be what you are looking for. It assumes
    the screen is a resource and the 4 threads are trying to access the resource
    for a short duration. The same sceenLock variable should be used in all
    4 threads. The assumption is most of the compute time in the threads
    is doing something else. Note other solutions are better if the screeLlock
    needs to be passed around often and contention happens between the
    various threads. Performance will not be good if there is a lot of contention.

    Code:
    pthread_mutex_t screenMutex = PTHREAD_MUTEX_INITIALIZER;
    
    void *functionA()
    {
    
    while(1)
    {
    /* CPU intensive stuff */
    pthread_mutex_lock(&screenMutex);
    printf ("\033[%d;%dH", 3, 0); // Set cursor position ( row 3, column 0)
    
    printf ("I am function 111111111111111111111111111111111111111111\n");
    pthread_mutex_unlock(&screenMutex);
    /* CPU intensive stuff */
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...