Privileged and Non-Privileged Instructions in Operating System
Last Updated : 08 Aug, 2024
In an operating system, instructions executed by the CPU can be classified into privileged and non-privileged instructions. These classifications help the operating system ensure security, stability, and efficient resource management. In this article, we will discuss Privileged and Non-Privileged Instructions in Operating Systems.
What are Privileged Instructions?
Privileged instructions are those that can only be executed by the operating system kernel or a privileged process, such as a device driver. These instructions typically perform operations that require direct access to hardware or other privileged resources, such as setting up memory mappings or accessing I/O devices. The Instructions that can run only in Kernel Mode are called Privileged Instructions. Privileged Instructions possess the following characteristics:
- If any attempt is made to execute a Privileged Instruction in User Mode, then it will not be executed and treated as an illegal instruction. The Hardware traps it in the Operating System.
- Before transferring the control to any User Program, it is the responsibility of the Operating System to ensure that the Timer is set to interrupt. Thus, if the timer interrupts then the Operating System regains control.
- Thus, any instruction which can modify the contents of the Timer is Privileged Instruction.
- Privileged Instructions are used by the Operating System to achieve correct operation.
- Various examples of Privileged Instructions include:
- I/O instructions and Halt instructions
- Turn off all Interrupts
- Set the Timer
- Context Switching
- Clear the Memory or Remove a process from the Memory
- Modify entries in the Device-status table
Role of OS in managing Privileged Instructions
- Access control: The operating system employs access control mechanisms to limit access to privileged instructions. These mechanisms are restricted to authorized processes or users with elevated privileges. This guarantees that privileged instructions can only be executed by trusted processes and thwarts unauthorized access of malicious programs to system resources.
- Memory protection: By deploying memory protection techniques, the operating system restricts processes from accessing any memory location that belongs to it or other processes. This aids in preventing tampering with the operating system or other processes by ensuring that processes cannot do so. Furthermore, it hinders malevolent programs from resulting in system crashes or risking the system's safety.
- Interrupt handling: The execution of privileged instructions like system calls or exceptions is handled by the operating system through interrupt handling to ensure safety and accuracy, so when an interrupt occurs during the execution of a process it's necessary to save its state before transferring control to a suitable handler and then restoring it on completion.
- Virtualization: Using virtualization techniques allows the operating system to create a simulated environment where processes can execute privileged instructions without having direct access to the underlying hardware thus creating a more secure and isolated execution environment for privileged instructions by limiting process access to authorized hardware resources only.

What are Non-Privileged Instructions?
Non-privileged instructions are those that can be executed by any process, including user-level processes. These instructions are typically used for performing computations, accessing user-level resources such as files and memory, and managing process control. Non-privileged instructions are executed in user mode, which provides limited access to system resources and ensures that processes cannot interfere with one another. The Instructions that can run only in User Mode are called Non-Privileged Instructions .
Various examples of Non-Privileged Instructions include:
- Reading the status of Processor
- Reading the System Time
- Generate any Trap Instruction
- Sending the final printout of Printer
Also, it is important to note that in order to change the mode from Privileged to Non-Privileged, we require a Non-privileged Instruction that does not generate any interrupt.
Differences Between Privileged and Non-Privileged Instructions
Criteria | Privileged Instructions | Non-Privileged Instructions |
---|
Access to Resources | Direct access to system resources | Limited access to system resources |
Execution Mode | Executed in kernel mode | Executed in user mode |
Execution Permissions | Require special permissions to execute | Do not require special permissions to execute |
Purpose | Used for performing low-level system operations | Used for general-purpose computing |
Risks | Higher risk of causing system crashes or security vulnerabilities | Less risky in terms of system crashes or security vulnerabilities |
Advantages of Privileged and Non-Privileged Instructions
- Security: This ensures that unauthorized persons do not access resources of their system. The operating system or other trusted processes can only execute privileged instructions while user programs can run non-privileged instructions.
- Performance: This reduces overhead and latency by allowing them to access hardware resources directly.
- Stability: Non-privileged instructions are limited in accessing system resources while privileged instructions can only be executed by trusted processes. The distinction between user programs and critical system functions keeps any harm caused by the user program at bay.
- Flexibility: Operating systems support a variety of applications and hardware devices because they have both types of instructions. Developers find it easy to create new applications and hardware through an interface defined within the operating system for compatibility purposes.
- Debugging: With a clear demarcation between trusted and untrusted processes, this makes it easier to debug the operating system itself plus the applications running on top of it. To resolve issues, developers must identify them first which is made possible through this separation.
Disadvantages of Privileged and Non-Privileged Instructions
- Overhead: Instructions that are either privileged or non-privileged can have an impact on overhead as well as performance degradation. Changes between the two modes, privileged and non-privileged ones, may take long time causing the system performance to drop.
- Complexity: Operating systems using such instructions with distinct access levels (privileged or non-privileged) become extremely complex and challenging to develop and maintain. The necessity of both types of instructions makes it even harder to design system features and ensure system stability.
- Compatibility: Privileged and non-privileged instructions may bring compatibility problems among different hardware platforms or operating systems. Diverse implementations of privileged instructions make it hard for creation of applications working on various platforms.
- Vulnerabilities: These resources are targeted by attackers who exploit security flaws in the operating system.
Conclusion
Privileged instructions are used by the operating system kernel and privileged processes to perform low-level system operations, while non-privileged instructions are used by user-level processes for general-purpose computing. The distinction between privileged and non-privileged instructions is an important mechanism for ensuring the security and stability of an operating system.