How does Windows know when a program is not responding?

56 views
0

How does Windows know when a program is not responding?

In: 1

Applications and the OS need to communicate over the use of system resources. If the OS sends a message to the application and the application does not send a reply, the application can be said to be ‘not responding’.

Windows “talks” to each program that is running on your computer. When a program doesn’t “talk” back to windows. Windows will label that program as not responding.

Operating systems do a thing called *time slicing.* This is where the OS gives each program a few milliseconds to run before the OS moves on to the next program. This is how the CPU is shared between many programs at the “same” time.

In most modern OSes, like Windows, this is a *premptive* process. That is, the OS doesn’t give the program the option to keep running, but instead, the OS has ultimate control over the CPU and decides who runs when.

When a program doesn’t respond to a “polite request” to give up its slice, the OS takes it anyway and marks the program as ‘not responding’. If the program fails to respond a certain number of times, the OS tells you so you can choose whether to give the program more time or to go ahead and kill & restart it.

Each application has a message queue. Every time something happens (mouse moves, key pressed, window needs to be redrawn, timer expires, etc) windows adds a message to the queue for the application. The application needs to handle the message within a few seconds or Windows will think the application is not responding. Once the application starts processing messages again the error goes away.

The answer directly from Microsoft:

> The operating system detects this state by attaching a timer to pending messages in the message queue. If a message has not been retrieved within 5 seconds, the DWM declares the window to be hung.

https://docs.microsoft.com/en-us/windows/win32/win7appqual/preventing-hangs-in-windows-applications

Because the program doesn’t respond, really.

Windows talks to applications by sending them messages. They’re supposed to regularly ask Windows “what’s the next message I’ve received?”, and then act on that. Messages may be as simple as “the user pressed this key on the keyboard”, or “the mouse is now at this position”, or “the user would like to close this window”, or a number of other, more technical things.

But this means that there’s a constant stream of messages being sent to the application, and if the application never asks Windows for the next available message, Windows knows that something’s up. If the application doesn’t ask for these messages it can’t react to user input, and so it’s basically hung. So if an application hasn’t checked in and asked for messages for a couple of seconds, Windows flags it as “not responding”.