Can two infinite loops run in parallel on a single-core machine? Explain what the OS actually does.
ReportedSource: GeeksforGeeks interview experience: Microsoft SDE-I, 1+ year
Not in parallel (one core runs one instruction stream), but concurrently yes: preemptive time slicing switches between them on timer interrupts. A cooperative scheduler would starve on a non-yielding loop.
Truly parallel: no. One core executes one instruction stream at a time (ignoring hardware threads such as hyper-threading, which present a single physical core as two logical CPUs).
Concurrently: yes. A preemptive OS gives each thread a time slice. A timer interrupt fires, the scheduler saves the running thread's registers, picks the other runnable thread and restores its context. Both loops make progress in turns, so to a user they appear to run together.
What changes in practice.
- Each loop gets roughly half the CPU; other processes still run because the scheduler keeps preempting.
- On a cooperative (non-preemptive) scheduler, an infinite loop that never yields would starve everything else. That is why modern kernels use timer-based preemption.
- If the loops are in the same process and share data without locks, interleaving at arbitrary points can still cause race conditions even on one core.
Good closing line. Parallelism needs multiple execution units; concurrency only needs interleaving.