Bottom line question: When the stack memory is filled up (so there's stack overflow) in a C program (e.g. infinite recursion, or a function calling itself, such as main
), how does the program know to signal or output that a Segmentation Fault
has occured? Would it be the OS?
- Corollary question - what's the difference between OS and kernel (or is it different terminology for the same thing?)?
(extra) Background information
There are many cool examples of small C code snippets that'll cause a stack overflow, such as
int main() {
main();
}
I was able to use gdb
and commands run
, break
, run
, and info frame
to see, directly, the addresses for the stack fill up "downwards" ("decreasing" addresses) step-by-step.
I also saw this question, SegFault on stack overflow in which the question said the kernel sends the signal for SIGSEGV. There were answers then saying the "kernel" (but really the OS) doesn't know, it just runs the code over and over, and that it's the responsibility of "signal handlers" to send a signal for SegFault. Is this the OS setting up the "signal handlers"? Who is able to signal that the stack is overflowed and can send a signal for Segmentation Fault?