segmentation fault vs page fault
Asked Answered
C

3

38
  1. I was wondering what differences and relations are between segmentation fault and page fault?

  2. Does segmentation fault only belong to segmented memory model?

    Does page fault only belong to paged memory model?

    If both are yes, since most computer systems such as x86 and Linux use paged memory model instead of segmented memory model, why does GCC C compiler sometimes report segmentation fault error?

Thanks and regards!

Cacuminal answered 5/8, 2011 at 1:43 Comment(0)
D
48

These two things are very dissimilar, actually. A segmentation fault means a program tried to access an invalid or illegal memory address: for example, 0, or a value larger than any valid pointer. A page fault is when a pointer tries to access a page of address space that's currently not mapped onto physical memory, so that the MMU needs to grab it off of disk before it can be used. The former is an illegal condition and the program will generally be aborted; the latter is perfectly normal and the program won't even know about it.

"Segmentation" isn't at all related to the old "segmented memory model" used by early x86 processors; it's an earlier use which just refers to a portion or segment of memory.

Dextroamphetamine answered 5/8, 2011 at 1:53 Comment(4)
Thanks! (1) What are there relation with segmented memory model and paged memory model? (2) what does "segmentation" in segmentation faults mean? Does it mean segment in segmented memory model? If yes, why can it still happen on paged memory model besides segmented memory model?Cacuminal
Most page faults are not noticed by the program, but Wikipedia says that an "invalid" page fault (en.wikipedia.org/wiki/Page_fault#Invalid) could cause a segmentation fault (depending on which OS you're using).Leper
When you say "page fault", your description is only covering "valid" page faults. Both start with a page-fault hardware exception, and if the OS determines that the process didn't have that page mapped, then it's invalid and delivers a SIGSEGV. But if it is valid, the page-fault handler can queue up I/O (hard page fault) or do copy-on-write or whatever lazy memory allocation (soft page fault).Abbe
Also, you are only describing Major faults (where disk need be consulted). Mostly you will see minor faults which happen when you reference unmapped pages in your address space and the MMU simply needs to map those page frames to physical pages (no disk needed). Segfault is a similar process except the references pages are not valid / in your address space I believe.Coquette
M
12

Segmentation faults occur when the memory is not allowed to be accessed (does not exist, or is forbidden). Most frequently they occur when you dereference a null variable or run off the end of an array. Page faults occur when memory that is mapped but not loaded is accessed. They are not errors, and signal to the operating system that it should load the appropriate page into memory.

Mahon answered 5/8, 2011 at 1:51 Comment(2)
Thanks! (1) What are there relation with segmented memory model and paged memory model? (2) what does "segmentation" in segmentation faults mean? Does it mean segment in segmented memory model? If yes, why can it still happen on paged memory model besides segmented memory model?Cacuminal
One other cause for segmentation fault: writing to a VALID memory address that resides in a memory page with page protections set to read-only or read/exec-only. For example, writing to an address in the .text (code) section will cause a SIGSEGV (but reading that same address will be safe)Netti
F
0

For posterity, here's a video lecture from UC Berkeley's OS course discussing this. https://www.youtube.com/watch?v=IBgkKX6DUTM&t=3345s

Tl;dr above answers (basically). But worth mentioning that the term does come from older style address translation where MMU's contained segment tables instead of page tables.

Fowler answered 1/6, 2023 at 6:55 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Nuthouse
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewDockage

© 2022 - 2024 — McMap. All rights reserved.