5.5.11 · D1 · HinglishEmbedded Systems & Real-Time Software

FoundationsFreeRTOS IPC — queues, semaphores, mutexes, event groups

4,704 words21 min read↑ Read in English

5.5.11 · D1 · Coding › Embedded Systems & Real-Time Software › FreeRTOS IPC — queues, semaphores, mutexes, event groups

Queues ya mutexes samajhne se pehle, tumhe unhe words mein fluent hona chahiye jo parent note assume karta hai ki tum pehle se jaante ho. Yeh page unhe ek-ek karke zero se build karta hai, ek aisi order mein jahan har idea pichle par tikta hai.


1. Kernel — wo invisible manager

Yeh topic kyun iska use karta hai: parent mein har IPC object ko kernel object describe kiya gaya hai. Kernel woh neutral referee hai jo ordering aur atomicity ko trustworthy banata hai — bina ek single manager ke, koi bhi promise enforce nahi ho sakti.


2. Task — ek program jo sochta hai ki poora CPU uska hai

Figure s01 neeche exactly yahi draw karta hai: teen task boxes, har ek ka apna stack aur priority, sab ek single CPU block se fed hain jiske dashed arrows unke beech jump karte hain — "bahut fast switch karta hai, ek time par ek." Ek dashed arrow ko ek time par trace karo taaki feel ho ki haath kabhi do jagah nahi hote.

Figure — FreeRTOS IPC — queues, semaphores, mutexes, event groups

Yeh topic kyun iska use karta hai: IPC = Inter-Process Communication. "Communication" ki problem tab tak nahi hai jab tak tumhare paas pehle do alag cheezein na hon — tasks — jo baat karni chahti hain. Full treatment ke liye dekho Producer–Consumer Pattern.


3. Scheduler aur preemption — wo haath jo chalte hain

Yeh topic kyun iska use karta hai: yeh saare danger ka source hai. Agar scheduler kabhi tumhe interrupt nahi kar sakta, toh shared data kabhi half-updated nahi pakdi ja sakti. Full detail yahan hai RTOS Scheduling — preemption & priorities.


4. Priority — ek plain number jiska matlab hai "kitna urgent"

Yeh topic kyun iska use karta hai: parent ki poori priority inversion discussion (Mars Pathfinder bug — dekho Priority Inversion & the Mars Pathfinder bug) ek story hai is baare mein ki kaunse numbered shirt ko haath milte hain. Is symbol ke bina tum usse follow nahi kar sakte.


5. Ready, Blocked, Running — task ke teen states

Figure s02 neeche woh state map hai jo tumhe is topic ke baaki hisse ke liye apne dimag mein rakhna chahiye: teen boxes (Running, Ready, Blocked) aur unke beech labelled arrows — scheduled, preempted, take/wait (sleep), aur event / timeout. Notice karo Blocked se bahar nikalne ke do raaste: ek event ya ek timeout, exactly upar wali alarm-clock picture.

Figure — FreeRTOS IPC — queues, semaphores, mutexes, event groups

Yeh topic kyun iska use karta hai: har IPC object ka asli kaam ek task ko Blocked aur Ready ke beech sahi moment par move karna hai. "Queue par wait karo", "semaphore lo", "bits ke liye wait karo" sab ka matlab hai "mujhe back room mein daal do jab tak meri condition true na ho (ya mera alarm na baje)."


6. Atomic — "all-or-nothing", taaki koi half-done cheez na dekhe

Yeh topic kyun iska use karta hai: ek plain count = count + 1 teen steps hain (read, add, write) — perfectly choppable. Parent kehta hai semaphore par Give/Take "atomic ops" hain. Woh word ek promise hai jo kernel karta hai — briefly interrupts disable karke implement kiya — ki counter kabhi mid-update nahi dekhа ja sakta.


7. Race condition — woh bimari jo saari IPC theek karti hai

Figure s03 neeche woh exact disaster ek timeline par hai: Task A 5 padhta hai, ek orange dashed preempt arrow ise freeze karta hai, Task B same 5 padhta hai aur 6 likhta hai, phir A jaagta hai aur woh bhi 6 likhta hai. Bottom line padhо — whiteboard 5 → 6 → 6 jaata hai jabki hum 7 expect karte the. Yeh ek picture IPC exist kyun karta hai iska reason hai.

Figure — FreeRTOS IPC — queues, semaphores, mutexes, event groups

Yeh topic kyun iska use karta hai: mutexes aur critical sections (Critical Sections & taskENTER_CRITICAL) aur queues woh tools hain jo aisi interleavings ko impossible banate hain.


8. FIFO — First In, First Out ordering

Yeh topic kyun iska use karta hai: FreeRTOS queue (agla section) FIFO buffer ke roop mein define ki gayi hai. Yeh word tumhe woh ordering guarantee batata hai jo tumhe free milti hai.


9. Queue — tasks ke beech safe mailbox

Yeh topic kyun iska use karta hai: queue parent ka foundation object hai, aur yeh teen ideas combine karta hai jo upar build hue hain (FIFO order, copy-by-value, Blocked/Ready). Baaki sab — semaphores bhi — ek stripped-down queue ke roop mein describe kiye gaye hain.


10. Copy by value vs. by pointer — actually kya travel karta hai

Yeh topic kyun iska use karta hai: parent ka core safety claim ("self-contained aur lifetime-safe") aur iska sabse bada trap ("copying avoid karne ke liye pointer bhejo") dono is distinction par hinge karte hain.


11. Notation 0b, |, aur 1 << n — bit-flags

Yeh topic kyun iska use karta hai: event groups (Section 14) literally inhi switches ka ek bank hain. Tumhe jaanna chahiye ki har named bit ek lit switch hai aur | unhe combine karta hai event-group API se milne se pehle.


12. ISR — Interrupt Service Routine

Yeh topic kyun iska use karta hai: parent ke ...FromISR functions, "ISR mein kabhi block mat karo" rule, aur "koi owner nahi ⇒ ISR mein koi mutex nahi" rule in dono facts se follow karte hain. Deeper treatment: Interrupt Service Routines & Deferred Processing.


13. Counter , Give aur Take

Yeh topic kyun iska use karta hai: , Give, aur Take semaphore section ke andar sirf yehi symbols hain. Saare ISR-signaling examples bas itne hain — "ISR ek token daalta hai, task ek leta hai ya soता hai."


14. Mutex aur priority inheritance — exclusive key

Yeh topic kyun iska use karta hai: parent ek poora section devote karta hai yeh batane ke liye ki mutex locking ke liye plain semaphore se better kyun hai. Ownership rule aur priority inheritance dono reasons hain — dono pictures follow karne ke liye tumhare paas honi chahiye.


15. Event group — switches ke combination par wait karna

Yeh topic kyun iska use karta hai: parent event groups introduce karta hai precisely logical combinations ("dono WiFi AND config") aur broadcasts ke liye. Switch-panel picture ke bina, xEventGroupWaitBits apne AND/OR flags ke saath unreadable hai.


Yeh topic ko kaise feed karte hain

Kernel = manager

Scheduler + preemption

Stack = private notepad

Task = worker

Producer Consumer pair

Priority number H M L

Round-robin on ties

Race condition

Atomic all-or-nothing

Timeout xTicksToWait

Blocked Ready Running states

All IPC objects

FIFO ordering

Queue = mailbox

Value vs pointer

Counter c Give Take

Semaphore

Bit flags 0b and 1 shift n

Event group

ISR no context no block

Mutex priority inheritance


Equipment checklist

Self-test: right side cover karo aur reveal karne se pehle zor se jawab do.

Kernel kya hai, ek line mein?
Woh always-present manager software (FreeRTOS) jo IPC objects ka owner hai, decide karta hai kaun run kare, aur akela code hai jo tasks ko block/wake kar sakta hai.
Stack kya hai aur har task ko apna kyun milta hai?
Local variables aur return addresses ke liye private scratch memory; alag stacks ek task ki scribblings ko doosre ki corrupt karne se rokti hain.
Task kya hai, ek line mein?
Ek independent forever-loop function apne private stack aur priority ke saath, aisa likha gaya jaise yeh poora CPU own karta ho.
Producer–Consumer Pattern kya hai?
Ek task data banata hai (producer), doosra use karta hai (consumer); unke beech ek queue unki speed mismatch buffer karta hai.
"Preemption" scheduler ko kya karne ki permission deta hai?
Ek running task ko mid-work pause karna taaki ek zyada urgent (higher-priority) task run kar sake.
Jab kai Ready tasks same priority share karti hain toh scheduler kya karta hai?
Round-robin time-slicing — har ek ko rotation mein equal ek-tick baari do taaki koi starve na kare.
FreeRTOS mein, bada priority number zyada urgent matlab hai ya kam?
Zyada urgent — sabse bada number jo ready hai woh run karta hai.
Teen task states ka naam batao aur kaun CPU free karta hai?
Running, Ready, Blocked — Blocked (sleeping) CPU free karta hai busy-wait karne ke bajaye.
Timeout / xTicksToWait kya hai?
Max ticks jitna ek task Blocked rehega kernel ke jagane se pehle; 0 = wait mat karo, portMAX_DELAY = hamesha wait karo.
"Atomic" kya promise karta hai, aur FreeRTOS ise kaise deliver karta hai?
All-or-nothing bina kisi half-state ke; briefly interrupts disable karke deliver kiya jaata hai (taskENTER_CRITICAL) ya scheduler lock karke.
Race condition ko ek sentence mein describe karo.
Ek bug jahan outcome same data ko touch karne wale tasks ke beech preemption ki unlucky timing par depend karta hai.
FIFO queue ke baare mein kya guarantee karta hai?
Pehle daali gayi item pehle nikali jaati hai — ordering preserved rehti hai, line mein cutting nahi.
Queue ek IPC object ke roop mein kya hai (FIFO ke aage bhi)?
Ek kernel-owned buffer jo same-size items task→task copy karta hai aur full sender ya empty receiver ko block karta hai.
Copy-by-value aur pointer pass karne mein kya farak hai?
Value actual bytes copy karta hai (self-contained); pointer sirf ek address copy karta hai, jo dangle kar sakta hai agar target free ho jaaye.
0b011 kya hai aur 1 << 2 kya hai?
0b011 binary mein 3 hai (switches 1 aur 0 on); 1 << 2 4 hai (switch 2 on).
Semaphore counter par do atomic moves kya hain?
Give () aur Take (, blocking jabki ).
ISR kya hai aur yeh kabhi block kyun nahi kar sakta?
Interrupt Service Routine — ek function jo hardware ek event par turant run karta hai; iska koi task identity nahi, toh ise soने nahi diya ja sakta.
Mutex plain binary semaphore se alag kya banata hai?
Iska ek owner hota hai (sirf lene wala release kar sakta hai) aur yeh priority inheritance support karta hai.
Priority inheritance kya hai aur yeh kya prevent karta hai?
Low task ko jo key hold kar raha hai temporarily waiting high task ki priority tak raise karna, unbounded priority inversion prevent karta hai.
Event group woh kya kar sakta hai jo ek single semaphore nahi kar sakta?
Ek task ko bits ke AND/OR combination par jagana, aur ek bit-set ko kai waiters tak broadcast karna.