SEMAPHORE IN MBED OS

NOTEBOOK SUBJECT : SEMAPHORE FOR PROTECTING CRITICAL SECTION

As we know while dealing with critical section ,some specific care and moves require to avoid bugs and real-time problems specially in RTOS .

Here , we are going to discuss counting SEMAPHORE technique for control shared resources accessing process.

SEMAPHORE

Semaphore is simply a variable that is non-negative and shared between threads. A semaphore is a signaling mechanism, and a thread that is waiting on a semaphore can be signaled by another thread. It uses two atomic operations, 1)wait, and 2) signal for the process synchronization.

In counting semaphore, you can access the shared resources until counter value is greater than zero, and when any thread or process cross the SEMAPHORE GUARD counter decrement by one. Same way when process or thread release the SEMAPHORE at that time Counter increment by one.

For more clarification lets see below example :

PLATFORM : MBED ONLINE COMPILER HARDWARE : STM32 NUCLEO-64 BOARD

SEMAPHORE IN MBED

/*SEMAPHORE ADVANCE PART 4
THIS PROGRAM DEDICATED TO COUNTING SEMAPHORE
NO MENTIONED WHILE CREATING SEMAPHORE IS ACT LIKE COUNTER, WHILE COUNTER > 0, THREAD/PROCESS CAN ACCESS RESOURCES.
IF COUNT = 1 IT ACT AS BINARY SEMAPHORE.
*/

//OUTPUT  --> PUTTY / COOLTERM / HYPERTTERMINAL
//TARGET : SOFTWARE = MBED ONLINE , HARDWARE : NUCLEO - 64 

//SEMAPHORE - ADVANCE PART 4
//PART -4 JAYDEEP SHAH

#include "mbed.h"  //MBED LIBRARY
#include "rtos.h"  // RTOS LIBRARY
 
Semaphore two_slots(2);  //CREATE SEMAPHORE OBJECT WITH N=2 , TWO PROCESS CAN ACCESS CRITICAL SECTION
 
void test_thread(void const *name) {        //PROCESS OR FUNCTION
    while (true) {
        printf("\n BEFORE SEMAPHORE GUARD -- THREAD NAME %s\n\r", (const char*)name);
        two_slots.wait();   //SEMAPHORE GUARD --> PROCESS CAN CROSS ONLY IF N>0
        
        // N = N -1 --> COUNTER DECREMENT IF PROCESS CROSS
        printf("\n SEMAPHORE CROSS AND SLEPP THREAD =  %s\n\r", (const char*)name);  //PRINT  STRING AFTER TYPE CONVERSION
        
        Thread::wait(1000);  //THREAD SLEEP FOR 1 SEC   --> THREAD SLEEP
        
        two_slots.release();  //SEMAPHORE RELEASE
        //IF SEMAPHORE RELEASE --> N = N +1
        
        printf("\n SEMAPHORE RELEASE THREAD = %s\n\r", (const char*)name); //AFTER SLEEP
    }
}
 
int main (void) {  // MAIN THREAD CREATE AND START
    Thread t2;   // CREATEHREAD OBJECT
    Thread t3;   // CREATE THREAD OBJECT
 
    t2.start(callback(test_thread, (void *)"Th 2"));  // THREAD START - CALL THE FUNCTION WITH CALLBACK COMMAND AND PASS STRING AS ARGUMENT
    t3.start(callback(test_thread, (void *)"Th 3"));  // THREAD START    & FUNCTION CALL SAME AS ABOVE
 
    test_thread((void *)"Th 1");  // FUNCTION CALL 
    //MAIN THREAD FIRST START WITH NORMAL PRIORITY
}


// YOU CAN TEST THE PROGRAM OUTPUT ON SERIAL MONITOR WITH TIME STAMP
//FOR MORE CLARIFICATION , TRY TO CHANGE SEMAPHORE COUNTER VALUE AND CHECK OUTPUT

DOWNLOAD LINK : (IMPORT FROM HERE)

<<

Import program13) SEMAPHORE_PART4_ADVANCE

SEMAPHORE ADVANCE PART 4 EXAMPLE OF COUNTING SEMAPHORE IN MBED OS FOR PROTECTING CRITICAL SECTION OF SHARED RESOURCES TARGET BOARD NUCLEO - 64 OS PLATFORM : MBED CREATED BY : JAYDEEP SHAH -- radhey04ec@gmail.com

>>

Thanks: Created by : JAYDEEP SHAH EMAIL : radhey04ec@gmail.com


Please log in to post comments.