Table of Contents
Memory Protection in Operating System
Memory Protection is an important concept in Operating system. Memory protection in operating system is required to protect the kernel address space from user address space.
In order to understand the concept of Memory protection in contiguous memory allocation first, we need to know about the user mode and kernel mode.
We have explained the importance of memory protection as well as the memory protection method here in this tutorial.
Frequently Asked Questions
Following questions are commonly asked in exam from memory protection section.
- What is Memory ?
- What is User Space ?
- What is Kernel Space ?
- What do you understand by logical address ?
- What is physical address ?
- What is Limit Register ?
- Explain the concept of contiguous memory allocation.
Concept of Memory Protection
- At the time of working on a computer system two types of process execute. First type of process are user process and second types of process are system process.
- In Memory, each user process has its own address space. So Operating system must be protected from user processes, and each user address space should be protected from another user process.
- First, there is a need to determine the legal address that a process may access and to ensure that process may access only these legal addresses.
- Protection can be provided by using two registers known as the Base Register and Limit Register.
- The base register holds the smallest legal, physical memory address.
- Limit register specifies the size of the range.
For example, if the base register holds 300040 and the limit register is 120900, then the program can legally access all addresses from 300040 through 420939 (inclusive).
Memory Protection Hardware
- Protection of memory space is accomplished by having the CPU hardware compare every address generated by the CPU with the values of the limit register.
- Any attempt by a program executing in user mode to access operating-system Memory or other users’ Memory results in a trap to the operating system, which treats the attempt as a fatal error.
- This scheme prevents a user program from (accidentally or deliberately) modifying the code or data structures of either the operating system or other users.
- The Base Register and Limit register can be loaded only by the operating system, which uses a special privileged instruction.
- Since privileged instructions can be executed only in kernel mode, and since only the operating system executes in kernel mode, only the operating system can load the base and limit registers.
- This scheme allows the operating system to change the value of the registers but prevents user programs from changing the registers’ contents.
- The operating system, executing in kernel mode, is given unrestricted access to both operating system memory and users’ Memory.
- This provision allows the operating system to load users’ programs into users’ Memory, dump out those programs in case of errors, access and modify parameters of system calls, and so on.
- Each Logical Address should be less than the Limit Register value.
- The Memory Management Unit converts each logical address to a physical address by adding the value of the relocation register.
- When the CPU Scheduler selects a process for execution, the dispatcher loads the relocation and limit registers with the correct values as part of the context switch.
- Because every address generated by a CPU is checked against these registers, we can protect both the operating system and the other users’ programs and data from being modified by this running process.
Conclusion and Summary
Memory Protection is required in operating system to keep the Kernel space safe from the user space and also to keep safe one user space with another user space.
In this tutorial we have explained all important concepts related to memory protection.
I hope this tutorial will be beneficial for computer science students.