0
0
Embedded Cprogramming~20 mins

Polling vs interrupt-driven execution in Embedded C - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Polling vs Interrupt Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A
Waiting...
Flag set!
Waiting...
Flag set!
Waiting...
B
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
C
Waiting...
Waiting...
Waiting...
Waiting...
Flag set!
D
Flag set!
Flag set!
Flag set!
Flag set!
Flag set!
Attempts:
2 left
💡 Hint
The flag variable is never set to true in this code, so the polling loop never detects it as true.
Predict Output
intermediate
2: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;
}
A
No interrupt
Interrupt detected!
Interrupt detected!
B
No interrupt
No interrupt
Interrupt detected!
C
No interrupt
No interrupt
No interrupt
D
Interrupt detected!
Interrupt detected!
Interrupt detected!
Attempts:
2 left
💡 Hint
The interrupt_handler sets the flag only after the first loop iteration.
🧠 Conceptual
advanced
2: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?
AInterrupt-driven execution requires the CPU to check flags continuously, increasing CPU load.
BPolling uses less power because it constantly checks the flag, making it more efficient.
CInterrupt-driven execution allows the CPU to perform other tasks until an event occurs, reducing wasted cycles.
DPolling allows immediate response to events without delay, making it more efficient.
Attempts:
2 left
💡 Hint
Think about what the CPU does while waiting for an event in both methods.
🔧 Debug
advanced
2: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;
}
AThe count variable is not incremented inside the if statement.
BThe interrupt_handler function is missing a return type.
CThe main function should call interrupt_handler() inside the loop.
DThe flag variable should be declared volatile to prevent compiler optimization issues.
Attempts:
2 left
💡 Hint
Consider how the compiler might optimize access to the flag variable without volatile.
🚀 Application
expert
3: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?
AUse interrupt-driven execution for the button press to ensure fast response, and polling for background processing to simplify design.
BUse polling for the button press to guarantee immediate detection, and interrupts for background processing to save power.
CUse polling for both button press and background processing to keep the system simple and predictable.
DUse interrupt-driven execution for background processing and polling for the button press to prioritize tasks.
Attempts:
2 left
💡 Hint
Consider which tasks require immediate response and which can be done in the background.