1

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?

  • For the corollary question, sometimes the two terms are used interchangeably, but IMO the "kernel" is only the actual kernel that sits between the hardware the the actual operating system, it handles processes and memory and device drivers. The "operating system" (or OS) is the kernel plus all tools, utilities, shells and user-interfaces that users interact with. – Some programmer dude Nov 12 '17 at 04:11
  • And for the bottom-line question, on a modern segmented and protected OS like Linux, Windows or macOS, it's the kernel that set up the segments and virtual memory tables for the processes, and access outside of a page is what could lead to a segmentation fault. You might want to do more research about *virtual memory*, *segmentation* and *paging* if you want more details. – Some programmer dude Nov 12 '17 at 04:14
  • 1
    @Someprogrammerdude Thanks, in terms of research, I'm working through - Zed A. Shaw. *Learn C the Hard Way* (really good C examples), and - Randall Hyde. *Write Great Code, Volume 2: Thinking Low-Level, Writing High-Level.* from suggestions by other stackoverflow questions and they have great explanations of the C memory layout. Hopefully those 2 resources can help others. Thanks for clarifying "kernel" vs. OS. Back to bottom-line Q, I'm thinking/guessing the virtual mem table would've setup a condition for overflow; how does it send this signal back? – ernestyalumni2014 Nov 12 '17 at 04:22
  • I don’t know how all the different cases are handled but I’m fairly sure there is a recursion depth limit that when it’s reached causes a seg fault – Ace shinigami Nov 12 '17 at 04:31

0 Answers0