Challenge - 5 Problems
Polling vs Interrupt Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of polling loop checking a flag
What will be the output of this embedded C code simulating a polling loop checking a flag variable?
Embedded C
#include <stdio.h> #include <stdbool.h> volatile bool flag = false; int main() { int count = 0; while (count < 5) { if (flag) { printf("Flag set!\n"); flag = false; count++; } else { printf("Waiting...\n"); count++; } } return 0; }
Attempts:
2 left
💡 Hint
The flag variable is never set to true in this code, so the polling loop never detects it as true.
✗ Incorrect
Since the flag variable is never changed to true, the if condition is never met. The program prints "Waiting..." five times and then exits.
❓ Predict Output
intermediate2:00remaining
Output of interrupt-driven flag set simulation
Given this simplified embedded C code simulating an interrupt setting a flag, what is the output?
Embedded C
#include <stdio.h> #include <stdbool.h> volatile bool flag = false; void interrupt_handler() { flag = true; } int main() { int count = 0; while (count < 3) { if (flag) { printf("Interrupt detected!\n"); flag = false; count++; } else { printf("No interrupt\n"); } if (count == 1) { interrupt_handler(); } } return 0; }
Attempts:
2 left
💡 Hint
The interrupt_handler sets the flag only after the first loop iteration.
✗ Incorrect
Initially, flag is false, so "No interrupt" prints. After count == 1, interrupt_handler sets flag true. Next two loops detect the flag and print "Interrupt detected!" twice.
🧠 Conceptual
advanced2:00remaining
Why is interrupt-driven execution more efficient than polling?
Which of the following best explains why interrupt-driven execution is generally more efficient than polling in embedded systems?
Attempts:
2 left
💡 Hint
Think about what the CPU does while waiting for an event in both methods.
✗ Incorrect
Interrupt-driven execution lets the CPU do other work or sleep until an event triggers an interrupt, saving CPU cycles and power. Polling wastes CPU time checking repeatedly.
🔧 Debug
advanced2:00remaining
Identify the bug in this interrupt-driven flag example
What is the main problem with this embedded C code simulating an interrupt-driven flag?
Embedded C
#include <stdio.h> #include <stdbool.h> volatile bool flag = false; void interrupt_handler() { flag = true; } int main() { int count = 0; while (count < 3) { if (flag) { printf("Interrupt detected!\n"); flag = false; count++; } } return 0; }
Attempts:
2 left
💡 Hint
Consider how the compiler might optimize access to the flag variable without volatile.
✗ Incorrect
Without volatile, the compiler may optimize away repeated reads of flag, causing the loop to never detect changes made by the interrupt handler.
🚀 Application
expert3:00remaining
Choosing between polling and interrupt-driven execution
You are designing an embedded system that must respond to a button press within 1 millisecond and also perform heavy background data processing. Which approach is best and why?
Attempts:
2 left
💡 Hint
Consider which tasks require immediate response and which can be done in the background.
✗ Incorrect
Interrupt-driven execution allows immediate response to the button press without wasting CPU cycles, while polling can be used for background tasks that are less time-critical.