Table of contents
1.
Introduction
2.
Dual Mode in OS
2.1.
The life cycle of instruction
3.
Types of instructions
3.1.
What are Privileged Instructions?
3.2.
What are Non-Privileged Instructions?
3.3.
Difference between Privileged and Non-Privileged Instructions
4.
Frequently Asked Questions
4.1.
What if a user program attempts to execute a privileged instruction?
4.2.
Is a direct instruction to switch to kernel mode a privileged or non-privileged instruction?
4.3.
Is a system call instruction to switch to kernel mode a privileged or non-privileged instruction?
5.
Conclusion
5.1.
Recommended Reading
Last Updated: Mar 27, 2024

Privileged and Non-Privileged Instructions

Author Pakhi Garg
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

It is more than likely that a rookie programmer might execute some sensitive code at times and that might lead to system failure and erroneous functioning of the operating system. Thus to ensure the proper functioning of the operating system, we must be able to distinguish between the execution of the user system code and the operating system code. For this purpose, we have Dual mode in the operating system. Before moving on to the privileged and non-privileged instructions, let’s get a quick overview of dual-mode in OS.

Also See, FCFS Scheduling Algorithm, Multiprogramming vs Multitasking

Dual Mode in OS

The dual mode in the operating system consists of basically two modes of operation- User mode (also called non-privileged mode) and Kernel mode (also called supervisor modesystem mode, and privileged mode). When a user is running an application, the system is in user mode. However, when a user application requests to access a file (for reading and writing data), the system needs to switch from the user mode to the kernel mode through a system call since the files reside in the secondary memory, and the user can’t access them directly. After executing the system call, the system returns to the user mode.

Also read - fork() system call

Note: A system call allows a user to communicate with the kernel and requests operations that can be carried out by the kernel only.

We have the mode bit to find out whether a task is getting executed on behalf of the user or the operating system. A mode bit is a bit that is added to the computer hardware to indicate the current mode: 0 represents the system is in kernel mode, and 1 represents the system is in user mode.

Life cycle of an instruction

The life cycle of instruction

Since we have discussed the dual mode in the operating system, we are in a state to understand the life cycle of instruction in the operating system.

Initially, the instructions are executed in the kernel mode, with the control residing in the operating system. As soon as a user starts an application, the control is switched to the user mode. Whenever there is a system call, interrupt, or trap, the control is eventually transferred to kernel mode. The control is returned to the instruction following the system call when the system call is executed. In this way, all instructions are executed.

Now we will discuss what exactly privileged and non-privileged instructions are:

Types of instructions

An instruction is an order or a command given to the system by an application. Instructions in the Operating System are divided into two categories- privileged instructions and non-privileged instructions. First, we will discuss privileged instructions.

What are Privileged Instructions?

Privileged instructions are the instructions that are only executed in kernel mode.

  • If a privileged instruction is attempted to get executed in user mode, that instruction will get ignored and treated as an illegal instruction. It is trapped in the operating system by the hardware.
  • It is the responsibility of the operating system to ensure that the Timer is set to interrupt before transferring control to any user application. As a result, the operating system can regain control if the timer is interrupted. 
  • The operating system uses privileged instruction to ensure proper operation.


Examples of privileged instructions

Some examples of privileged instructions include-

  1. I/O instructions
  2. Context switching
  3. Clear memory
  4. Set the timer of the CPU
  5. Halt instructions
  6. Interrupt management
  7. Modify entries in the Device-status table

What are Non-Privileged Instructions?

Now we will discuss the second type of instructions, i.e., non-privileged instructions.

Non-Privileged instructions are the instructions that are only executed in user mode.

Examples of Non-privileged instructions

  1. Generate trap instruction
  2. Reading system time
  3. Reading status of processor
  4. Sending the output to the printer
  5. Performing arithmetic operations
     

Now, the following figure will show the layered based organization of the operating system.

The whole computer system is divided into two parts- hardware and software. The software part communicates with the hardware part using the instruction set.

The instruction set is divided into two categories:

  1. Privileged instruction - These instructions must be used wisely as their misuse can harm the system.
  2. Non-privileged instruction - These are normal instructions.


The software part can be divided into two categories based on the instructions:

  1. Kernel - Can execute only privileged instructions.
  2. Application - Can execute both privileged and non-privileged instructions.
Previleged and Non-Previleged Instruction

Difference between Privileged and Non-Privileged Instructions

S.No Privileged instruction Non-privileged instruction
1., These instructions are only executed in kernel mode. These instructions are only executed in user mode.
2. These instructions get executed under specific restrictions and are mostly used for sensitive operations. These instructions get executed without interfering with other tasks because they do not share any resources.
3. Examples of privileged instructions are - I/O instructions, context switching, clear memory, etc. Examples of non-privileged instructions are - generate trap instruction, reading system time, the reading status of the processor, etc.

You can also read about layered structure of operating system, Open Source Operating System.

Frequently Asked Questions

What if a user program attempts to execute a privileged instruction?

If a user program attempts to execute a privileged instruction, then that instruction is considered illegal and is not executed. The hardware traps that instruction in the operating system.

Is a direct instruction to switch to kernel mode a privileged or non-privileged instruction?

Direct instruction to switch to kernel mode is a privileged instruction since this instruction is called independently, not via a system call, an interrupt or a trap.

Is a system call instruction to switch to kernel mode a privileged or non-privileged instruction?

A system call instruction to switch to kernel mode is a privileged instruction since this instruction is called independently, not via trap, interrupt, or a system call.

Conclusion

In this article, we briefly discussed Dual Mode and explored the types of instructions - privileged and non-privileged. These instructions are executed in the kernel and user mode, respectively.

Recommended Reading

For more in-depth knowledge of Operating Systems, check out the Operating System Track and Guided Path for Operating System on Coding Ninjas Studio.

To gain an edge over the competition also check out Problems, Interview Experiences, and more guided paths on amazing topics such as Data Structures and Algorithms only on Coding Ninjas Studio.

Happy Learning!

Live masterclass