User Mode vs Kernel Mode 

In this lecture, we will cover User Mode vs Kernel mode. And then will cover Trap vs Interrupt in the end. Let’s start with User Mode Vs Kernel Mode.

USER Mode

If the CPU is executing the user applications, then the CPU will be in the user mode. The User applications are games, media players, text editors, MS Office, etc.

Executing code in user mode means having no ability to directly access some resources like hard disk, memory, printer, and other I/O devices. To access these resources, we have to use kernel mode through System Call.

Note: User mode is also known as safe mode and Restricted mode.

Kernel Mode

Kernel mode contains OS, which has full functionality to access and maintain all the hardware components. Hardware components are RAM, HARD, CPU, printer, and other I/O devices.

Whenever the system call is generated, the CPU switches to kernel mode from user mode. A system call is executed in kernel mode. After execution of the system call, it returns back to user mode.

Note: kernel mode is also known as system, privileged (private) supervisory mode, or protected mode because the user can’t access this area directly 

System Call

A system call is a way through which we access kernel mode. There are different types of system calls to perform different tasks. When a System Call is generated, the CPU switches from user mode to kernel mode and performs that particular task defined by the system call.

Explanation of System Call

As in the above diagram, User Applications run in user mode, and the operating systems run in kernel mode. As OS controls all the hardware, the User cannot access hardware without kernel mode. To access kernel mode, the user application generates a system call because a System call is a source to go to kernel mode.

CPU switch in two modes while working

  • User mode
  • Kernel mode

Let’s explain User Mode vs Kernel Mode with examples

When the system is turned on, the CPU is in kernel mode, by default, because it loads OS into the main memory and OS exists in Kernel mode. After logging IN, the CPU switches to user mode to run user applications.

1. Watching Movies in Windows OS,

If we want To play a movie, then we have to double-click on that particular video. As that video exists in the main memory, and the main memory is hardware. So, the CPU cannot access the hardware in user mode. The system has to request through a system call to open the file in the main memory. So, an OPEN () system call is generated, and the CPU switches to Kernel mode. Now, the OS in the kernel opens that particular file from the main memory. After opening the file, the CPU switches back to user mode. Now, we are watching the movie in user mode. When the user wants to close the file, the CLOSE system call is again generated.

2. Using MS WORD

To open MS WORD, we have to double-click on that particular file. As that file exists in the main memory, the main memory is hardware. So, the User cannot access the hardware in user mode. Users have to request through a system call to open the file in the main memory. So, an OPEN () system call is generated, and the CPU switches to Kernel mode. Now that the OS is in kernel mode, it opens that particular file from main memory. After opening the file, the CPU switches back to user mode. Now, we are writing some text in user mode. When the user wants to save the file, the WRITE () system call is again generated. CPU switches to kernel mode to save the file in the main memory.

3. Using C++

When we are coding in language translator, then we are in user mode, and when we use printf () command to print something on the monitor, the CPU switches to Kernel mode to print on the Monitor because the monitor is hardware.

Hence, it proved that the CPU will shift to Kernel mode each time it wants to access hardware.

Note: When the CPU switches to kernel mode from user mode, a Trap is generated, which changes the mode bit from 1 to 0. When kernel execution is complete, the trap is again generated, and change the mode bit from 0 to 1. Explanation given under with diagram,

Note: Windows and Linux OS are used in user and kernel modes.

Real Life Example

  • If we go back to withdraw money. Then, the staff of the bank is a kernel, and our command in user mode is to withdraw the money. This request to withdraw the money is executed by bank staff. So, without a kernel, the user can’t proceed.
  • In a cricket match, all players are in user mode when they go for any decisions to umpires. Then the Umpire is in kernel mode, and the decision is the system call

Now let’s look at comparisons of Trap and Interrupt

Trap VS Interrupt

Trap

Interrupt

It is generated by an instruction in the user program, so they call it software interrupts. it is generated by hardware devices, so they are called hardware interrupts.
Trap can arrive after the execution of any instruction It can occur at the execution time of any instruction
It is raised from the user program and indicates that the OS will perform some functionality immediately. A signal to the processor emitted by hardware indicates an event that needs immediate attention.
It transfers the Control to the trap handler. Control transfer to corresponding interrupt handler routine.