Difference between a program that crashes and program that hangs
What is the difference (or causes) between a program that crashes and a program that hangs (becomes unresponsive) in C++开发者_运维百科?
For sure, accessing invalid memory causes a program to crash. Deadlock in threads may cause a program to hang. What are the other causes?
Does exhausting all memory causes a program to hang? or crash? I'm a bit confused between the differences and their causes.
Crashing is normally caused by an illegal instruction, e.g. accessing invalid memory, dividing by zero, etc. Usually this manifests itself as a well-known exception which is handled by the operating system.
Hanging can be broken up into 2 fairly high level categories:
- Deadlock, usually caused by 2 threads competing for a resource, each requiring a resource held by the other thread to be released. A common cause of this is acquiring multiple locks in inconsistent orders within multiple threads, leading to the common ABBA deadlock pattern (and no this has nothing to do with Swedish pop music).
- Livelock, which means that the code is still actively running, but you have reached a state that you cannot leave. For example:
- The state of 2 processes/threads keep changing, never reaching an end condition
- A while loop where the exit condition will never be satisfied, or an indefinite loop (although this is stretching the definition of "livelock").
Update based on question comment
@Pop, Kristo: Am actually checking on a code that hangs but I see some problems on memory leak. But I'm not really sure if memory leak causes a program to hang. – jasonline
A memory leak can cause a program to crash, but this depends on various factors:
- Size of leak
- Frequency of leak
- Lifetime of application
Memory leaks may result in 2 bad things - a continual increase in memory usage by the process, and memory fragmentation. Both of these can result in failure to allocate memory down the line, if the OS cannot provide a contiguous block of memory.
In C++, if the new
operator fails to allocate memory, a std::bad_alloc
exception will be thrown. This will most likely be caught by the OS, resulting in a crash (unless you have written a specific handler in your application for this exception, and are able to handle it more gracefully).
Hangs can also be caused by waiting for external resources, mostly networking. Though that usually times out after a while. A hang may also be caused by the termination of a thread that handles something related to processing. For example, if a UI thread dispatched a worker thread to do some work and the worker thread died, the program would appear to be hung.
A lot of the times Windows apps hang because something happens to their message loop processing. Since all of the program events come trough the message loop once that is compromised, the program becomes unresponsive.
You can read more about how message loop works here:
http://www.winprog.org/tutorial/message_loop.html
An important factor between hanging and crashing is the underlying operating system in relation to support provided for software and hardware exception handling and/or protection mechanism. For example, in the old DOS world, you could hang the entire system with an invalid memory access when operating in real mode (since 386 > DOS-extenders crashed with exceptions, at least the popular ones such as DOS4GW).
Most exceptional and wrong behavior of user-mode applications can be stopped with proper support from OS and runtime libraries, with the exception of deadlocks.
In kernel-mode code, the possibility of hanging is much higher, of course.
It looks like you've mostly answered your own question.
A few additional reasons I can think of for a crash:
- Illegal instruction. For example, if you run SSE code on an old CPU.
- Bad memory access is pretty vague. There are sub-cases of this, such as un-aligned access of words on some CPUs, stack overflow, access to unmapped pages, writing to read-only pages, executing non-executable pages, concurrent access leading to broken invariants, to name a few...
- On Linux your process can be killed (seemingly randomly) if the system is very low on memory. (see this search for "OOM killer")
In addition to deadlocks, you have:
- "Livelocks".
- UI threads being blocked by some other action (some examples: I/O, pages being evicted to disk in low memory circumstances)
- Other programmer errors, such as loops that never terminate in some condition that hasn't been tested.
From your comment, it's possible that your memory leak is related to your hang, but not the cause of it. That is, you may have something as simple as an infinite loop somewhere that grabs a bit more memory in each iteration.
精彩评论