---
title: "Task synchronization using busy/wait"
subject: "Embedded System"
chapter: 6
key: "semester_6/embedded_system:task-sync-busy-wait"
source: https://www.bctengineer.com/q/semester_6/embedded_system/task-sync-busy-wait
site: BCT Engineering
---

# Task synchronization using busy/wait

Embedded System · Chapter 6: RTOS

- Asked in: 2072 Magh, 4 marks
- Video: [Different techniques for task synchronisation KTU Embedded System module 3 malayalam](https://www.youtube.com/watch?v=TuwbD6iZ6gg) — KTU CSE
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/task-sync-busy-wait
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-6

## Question

What is task synchronization and explain the task synchronization using "Busy/Wait".

## Short answer

Busy-wait synchronization coordinates tasks by repeatedly testing a shared flag until a condition is met. It is simple for short waits but wastes CPU cycles, may cause priority inversion, and increases power consumption.

## Answer

**Task synchronisation** is the coordination of tasks so that they execute in the correct order and access shared resources one at a time, to avoid race conditions and inconsistent data. For example, a consumer task must wait until a producer task has produced data.

#### Busy/Wait synchronisation
In **busy waiting** (spin waiting), a task that needs a resource or event keeps looping and testing a shared flag (variable) until the condition becomes true.

```c
volatile int flag = 0;       /* shared flag */

void producer(void) { prepare_data(); flag = 1; }

void consumer(void)
{
    while (flag == 0)        /* busy wait: spin until set */
        ;
    use_data();
    flag = 0;
}
```
Mutual exclusion can be done in the same way with a lock variable: `while (lock == 1); lock = 1; /* critical section */ lock = 0;` (a test-and-set instruction is needed to make the test and the set atomic).

**Advantages:** simple to implement; no context switch when the wait is very short; fine for short waits in interrupts or multi-core systems.

**Disadvantages:** it wastes CPU cycles while spinning; with pre-emptive priority scheduling on one CPU, if a high-priority task spins, the lower-priority task that would set the flag never gets the CPU, so the system can hang; it uses more power; and it is not suitable for long waits. For these reasons, blocking primitives (semaphores, sleep/wake) are preferred.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the aim of task synchronisation?
   A. Make all tasks run simultaneously
   B. Increase task priorities
   C. Remove the need for shared data
   D. Run tasks in the correct order and access shared resources one at a time
   Answer: D. Synchronisation prevents race conditions and inconsistent data. (Key point: Task synchronisation)

2. How does busy/wait synchronisation work?
   A. The task sleeps until woken by the kernel
   B. The task disables the scheduler permanently
   C. A task loops testing a shared flag until the condition becomes true
   D. The task sends a message and exits
   Answer: C. Spin waiting means repeatedly checking a flag, e.g. while(flag == 0); (Key point: Busy waiting)

3. Which is a main drawback of busy waiting?
   A. It needs a kernel semaphore
   B. It wastes CPU cycles and power while spinning
   C. It cannot be used with flags
   D. It causes context switches on every check
   Answer: B. Spinning uses CPU without useful work; blocking primitives are preferred for long waits. (Key point: Disadvantage)

4. Why is a test-and-set instruction needed for a lock variable?
   A. To make testing and setting the lock atomic
   B. To speed up the loop
   C. To raise priority
   D. To avoid declaring volatile
   Answer: A. Otherwise two tasks could both see the lock free before either sets it. (Key point: Atomic lock)

## Related in Embedded System

- [Mutual exclusion via sleep and wake](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Amutual-exclusion-sleep-wake) — Embedded System, chapter 6

## Related in other subjects

- [Process synchronization, Peterson, TSL](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Awhy-synchronize-peterson-tsl) — Operating System, chapter 3
