What Can Cause a C Program to Crash Operating System

核能气质少年 提交于 2021-02-19 03:47:31

问题


I recently found that a fairly large image manipulation program I'm writing in C on a Windows 8 machine has a bug when used in very particular circumstances. Unfortunately, the bug is causing my entire computer to come to a standstill so that my only option is to pull the plug on the computer (especially annoying when I'm working remotely...)

Because it's an image manipulation program, I can't just flood it with print statements to isolate the problematic section - the problem occurs somewhere in a loop that's called billions of times, so adding a printf slows it down to the point that it would take days to get to a failing iteration.

I understand, therefore, if this question is too broad, as it isn't really reasonable for me to put down all of the code that could cause my problem, I'm simply asking

What are the circumstances in which C code can, instead of seg faulting or halting the program, actually freeze the entire OS

When I search the problem, I see code golf questions like this

A C program which crashes the system(shuts down the system)

This is not what I'm asking - obviously I haven't written system("shutdown") anywhere in my loop.

Being most familiar with python and java, this problem is not what I'm used to, but in my experience,

  • Dividing by zero produces a seg fault
  • Accessing memory by accident that is slightly outside an intended array causes a seg fault (sometimes down the road a little)
  • Accessing protected memory causes the program to hang
  • Stack overflow causes a seg fault
  • Dereferencing a non-initialized pointer causes a seg fault

Is this impression false - could those cases cause the whole system to crash? What cases am I missing? Is it dependent on my version of gcc, or my permission status?

I haven't been able to try to reproduce it on a different operating system yet, as it requires a few dependencies to run the entire program.

If my only option is to sit for days waiting for the program to run with print statements, or avoid weird situations, then, of course, so be it. I'm looking for key places to look for the bug.


回答1:


On modern systems with hardware-enforced privilege separation between user-mode and kernel-mode, and an operating system that functions to correctly configure these mechanisms, you simply cannot crash the system from a user mode process.

Any of those errors are trapped by the CPU, which call exception handlers in the OS which will quickly pull the plug on your system.

If I had to guess, a piece of hardware is overheating or malfunctioning:

  • Overheating CPU due to poor thermal conductivity with heatsink
  • Failing / under-sized power supply
  • Failing DIMMs
  • Failing hard drive
  • Failing CPU
  • Failing / overheating GPU

I've seen cryptocoin-mining software bring a system to its knees because it was pushing the limits of the GPU. When the card would lock-up/reset, the driver would get confused or lock-up, and the system would end up needed rebooted.

Your system is doing next to nothing when you're just sitting there browsing the web, etc. But if your system locks up when you start running a CPU-intensive application, it can bring out problems that you didn't know where there.

While this is a little out-of-place on Stack Overflow, it falls into one of those grey areas between hardware and software. I would stress-test your system, keeping an eye on CPU/GPU/memory temperatures, and power supply voltages. Check out MemTest86, Stresslinux.




回答2:


The most trivial cause of OS freezing is "memory full". If you have processes that use a lot of memory, then your system is going to swap from main memory (typically RAM) to secondary memory (typically disk) which lead to a very huge overhead... As a user what you usually observe is a almost freezed computer, sometimes so freezed that you think it is crashed. If your OS is badly designed then it sometimes crashes!



来源:https://stackoverflow.com/questions/26479670/what-can-cause-a-c-program-to-crash-operating-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!