6

We know all that runs on the computer is a huge program collaborated by many.
So, when a computer hangs and we aren't able to do anything, what happens then? Also, is this scenario where everything freezes also something implemented in the program? or is it like the Program Counter is stuck and cannot increment and so is some problem in the processor?

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
Anurag
  • 651
  • 4
  • 18

1 Answers1

3

The computer may freeze under different circumstances. These are the ones I can think of now:

  • x86 CLI and HLT instructions. CLI disables interrupts, so no asynchronous event (like a timer interrupt or pressing a key) can move the CS:EIP instruction pointer to another instruction and HLT literally halts the processor.
    The instruction is seldomly used and only allowed to by the kernel if some initialization routine in boot code fails for example. Although restarting is a better option here.
    Note that HLT only halts the core it is run on, not all cores.

  • A window is not responding (commonly found on Windows). This differs from application to application. More information here.

  • A resource is attempted to be acquired but is protected by a lock and has already been acquired. The process waits (actually busy-loops or yields another process) until it finally can acquire the resource. This is only a temporary state, though, as opposed to...

  • A deadlock. Multiple circumstances under which it can occur but a common one is two processes attempting to acquire resources they provide to each other at the same time. None can handle the acquisition request because both are waiting for the other processes, so both processes end up uninterruptible. This is the reason for uninterruptible processes on Linux, which cannot be killed despite being dispatched the signal to.

  • Multitasking on a slow processor or a processor with few threads. A bad scheduling algorithm makes the situation even worse.
    Since one process occupies at least one thread, the number of processes effectively running concurrently is very low. This could be stabilized by a very fast processor, though.
    This results in a long response time to events like mouse clicks.

For x86 systems, HLT is the only instruction really hindering the instruction pointer from advancing.
All other cases are just (potentially infinite) loops or program/operating system bugs.

Community
  • 1
  • 1
cadaniluk
  • 15,027
  • 2
  • 39
  • 67
  • since you put "multitasking on a slow processor" I think you should also add "high swap traffic on a system with little RAM". They are in the same ballpark: limited hardware resources. – bolov Dec 02 '15 at 09:22
  • 1
    An interesting question is: can a bug in a user process solely freeze the entire system, or there also has to be a bug in the OS or system drivers in order for that to happen? – bolov Dec 02 '15 at 09:25
  • @bolov It is indeed but rather a philosophical one. The good old fork bomb is a user program and can easily crash the system. The fact that OSes don't prohibit it: is that a bug or not? "Philosophical" may not be the right word here but I hope you get what I mean anyway. **Note:** I'll adjust my answer later according to your first comment but am currently unable to do so. – cadaniluk Dec 02 '15 at 09:28
  • 1
    @cad So the way I see this now is, since HLT is an initialization routine scenario, while your system has booted what can halt/hang/freeze it is CLI, i wonder why and how would a CLI be run and the code wont renable CLI. The major deal however seems to be priority inversion and deadlocks, since an OS is being developed for such long times, why cant deadlocks be avoided? wont thread safety rules help prevent – Anurag Dec 02 '15 at 21:52
  • @Anurag `CLI` and `HLT` are two instructions. Interrupts do not have to be reenabled because the system is gonna halt forever. Only a restart helps here. And deadlocks can surely be avoided but programmers also make mistakes. Yes they will **help** but not make the issue disappear completely. – cadaniluk Dec 03 '15 at 06:28