Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a program that crashes and program that hangs

Tags:

c++

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.

like image 708
jasonline Avatar asked Dec 16 '09 03:12

jasonline


People also ask

What is the difference between freezing and crashing?

There's really no formal definition. But broadly speaking, a crash occur when an uncontrolled error happens. Freeze is when an application stops to respond to any event (for example an infinite loop) but no actual error happens (no exception thrown).

What happens when a program crashes?

When a program crashes, something unexpected has happened which the program itself is not equipped to handle; when the operating system detects such an event, it (usually) terminates the program.

Why do programs hang?

Hangs have varied causes and symptoms, including software or hardware defects, such as an infinite loop or long-running uninterruptible computation, resource exhaustion (thrashing), under-performing hardware (throttling), external events such as a slow computer network, misconfiguration, and compatibility problems.

What is crash software?

What Does Crash Mean? A crash, in the context of computing, is an event wherein the operating system or a computer application stops functioning properly. It mostly occurs when: Hardware has failed in a non-recoverable fashion. Operating system data have become corrupted.


3 Answers

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).

like image 119
LeopardSkinPillBoxHat Avatar answered Oct 02 '22 16:10

LeopardSkinPillBoxHat


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.

like image 24
Francis Upton IV Avatar answered Oct 02 '22 16:10

Francis Upton IV


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

like image 22
Igor Zevaka Avatar answered Oct 02 '22 17:10

Igor Zevaka