-2

It is very clear that a segmentation fault occurs when we access the memory that we are not supposed to access — i.e. we do not have the permission to access. Okay, I assume the OS manages it.

My question is: how does the operating system decide that a block of memory is allocated to a process? And the process does not have access to the memory?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 4
    That's great you are asking it! Grab a book named something like "Operation Systems" and look for a chapter named something like "Memory Management". – Eugene Sh. Oct 31 '17 at 14:36
  • 1
    https://stackoverflow.com/questions/13936534/how-does-a-segmentation-fault-work-internally-kernel-hardware – msc Oct 31 '17 at 14:37
  • 1
    https://stackoverflow.com/questions/13225936/what-exactly-happens-inside-the-os-to-cause-the-segmentation-fault – msc Oct 31 '17 at 14:37
  • 1
    read about virtual memory in any course of operating systems – alinsoar Oct 31 '17 at 14:44
  • 1
    One of the main tasks of the operating system is to control the access to memory. It allocates memory to processes, and ensures that the processes do not access memory they're not supposed to access. This is done by controlling the memory management hardware. Programs deal with virtual memory. The O/S maps virtual memory to physical memory, and rejects attempts to abuse memory. – Jonathan Leffler Oct 31 '17 at 14:58

2 Answers2

2

In most modern operating systems(OS), physical memory is not allocated directly by individual processes. Instead, a virtual memory layer is placed between the process and the physical memory, and is maintained by the memory manager of the OS. That virtual memory layer represents a mapping from a virtual address space to physical memory, and it is used to provide some security, along with the abstraction that each process has the memory to itself. (unless some multiprocessing scheme is utilized)

When checking whether a process can access a certain part of its virtual memory, the OS basically finds out if the address provided by the process is deemed accessible in the virtual memory space of that process. (Note that the address process has does not even have to be the real address on the physical memory) If the address is not deemed accessible within the virtual memory of that process, then a segment violation(SIGSEGV) is thrown.

That being said, do keep in mind that memory management in operating systems is a long conversation with many details and OS-specific implementation differences. My answer was only a generic one that is correct in most cases, and for further, more detailed information you should look into one of many books available studying the topic of operating systems.

ilim
  • 4,477
  • 7
  • 27
  • 46
0

My question is how operating system decide that a block of memory is allocated to a process? And the process does not have access to the memory?

Most processors these days use page tables to translate logical addresses in a process to physical page frames. Logical addresses are divided into fields that specify page tables, page table entries, and offsets.

If a logical address does not have a corresponding page table or page table entry, the address is invalid. If it has a page table entry, that entry might be marked as invalid. If the entry is valid, the page may be only accessible from a more secure mode.

That's how the processor knows. It's all in the page tables.

user3344003
  • 20,574
  • 3
  • 26
  • 62