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

Files at this revision

API Documentation at this revision

Comitter:
radhey04ec
Date:
Wed Jul 15 03:34:47 2020 +0000
Parent:
0:2de8a221f1d2
Commit message:
SEMAPHORE ADVANCE PART 4 FINAL COMMITED; EXAMPLE OF USING COUNTING SEMAPHORE; TARGET NUCLEO 64

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 2de8a221f1d2 -r d3e6b9a8a9c2 main.cpp
--- a/main.cpp	Sun Jul 12 11:00:46 2020 +0000
+++ b/main.cpp	Wed Jul 15 03:34:47 2020 +0000
@@ -1,24 +1,48 @@
-#include "mbed.h"
-#include "rtos.h"
- 
-Semaphore two_slots(2);
+/*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
  
-void test_thread(void const *name) {
+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) {
-        two_slots.wait();
-        printf("%s\n\r", (const char*)name);  //PRINT  STRING AFTER TYPE CONVERSION
-        Thread::wait(1000);  //THREAD SLEEP FOR 1 SEC
+        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
-        printf("AFTER SLEEP %s\n\r", (const char*)name); //AFTER SLEEP
+        //IF SEMAPHORE RELEASE --> N = N +1
+        
+        printf("\n SEMAPHORE RELEASE THREAD = %s\n\r", (const char*)name); //AFTER SLEEP
     }
 }
  
-int main (void) {
-    Thread t2;
-    Thread t3;
+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
  
-    t2.start(callback(test_thread, (void *)"Th 2"));  // CALL THE FUNCTION WITH CALLBACK COMMAND AND PASS STRING AS ARGUMENT
-    t3.start(callback(test_thread, (void *)"Th 3"));
- 
-    test_thread((void *)"Th 1");
-}
\ No newline at end of file
+    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
\ No newline at end of file