Bird
0
0

Why does the following infinite loop script sometimes consume 100% CPU?

hard🧠 Conceptual Q10 of 15
Bash Scripting - Loops
Why does the following infinite loop script sometimes consume 100% CPU?
while true; do :; done
ABecause ':' command is invalid and causes error
BBecause 'done' is missing
CBecause 'true' is not a valid condition
DBecause it runs without any pause or sleep, using full CPU
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop behavior

    Loop runs endlessly executing ':' which does nothing but returns success immediately.
  2. Step 2: Reason CPU usage

    Since no sleep or wait, loop runs as fast as possible, consuming full CPU.
  3. Final Answer:

    Because it runs without any pause or sleep, using full CPU -> Option D
  4. Quick Check:

    Infinite loops without delay use 100% CPU [OK]
Quick Trick: Add sleep to infinite loops to reduce CPU usage [OK]
Common Mistakes:
MISTAKES
  • Thinking ':' is invalid command
  • Assuming 'true' is wrong condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes