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.
If CPU is executing the user applications then CPU will be in the user mode. The User applications are games, media players, text editor, MS Office etc.
Executing code in user mode has no ability to directly access some resources like Hard Disk, memory, Printer and other I/O devices. Because, 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 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 generate, CPU switch to kernel mode from user mode. System call is executed in kernel mode. After execution of system call it returns back to user mode.
Note: kernel mode also known as system, privileged (private) or supervisory mode, protected mode because user can’t access this area directly
System call is a way through which we access kernel mode. There are different types of system call to perform different tasks. When System Call is generated then CPU switch form user mode to kernel mode and performs that particular task defined by system call.
Explanation of System Call
As in above diagram, User Applications run in user mode, and operating system run in kernel mode. As OS control all the hardware’s so User cannot access hardware without kernel mode. To access kernel mode user application generate a system call because 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 system is Turn ON then CPU is in kernel mode, by default, because it loading OS into main memory and OS exist in Kernel mode. After log IN, CPU switch to user mode to run user applications.
1. Watching Movie in windows OS,
If we want To play movie then we have to double click on that particular video. As, that video exist in main memory and main memory is hardware.So, CPU cannot access the hardware in user mode. System have to request through system call to open the file in main memory. So, OPEN () system call is generated and CPU switch to Kernel mode. Now OS in kernel, open that particular file from main memory. After opening the file CPU switch back to user mode. Now we are watching the movie in user mode. When user want to close the file then CLOSE system call again generated.
2. Using MS WORD
To open MS WORD we have to double click on that particular file. As, that file exist in main memory and main memory is a hardware. So, User cannot access the hardware in user mode. User have to request through system call to open the file in main memory. So, OPEN () system call is generated and CPU switch to Kernel mode. Now OS is in kernel mode, it open that particular file from main memory. After opening the file, CPU switch back to user mode. Now we are writing some text in user mode. When user want to save the file then WRITE () system call is again generated. CPU switches to kernel mode to save the file in 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 monitor, then CPU switch to Kernel mode to print on Monitor because monitor is a hardware.
Hence it proved: CPU will shift to Kernel mode Each time when CPU wants to access hardware.
Note: When CPU switch to kernel mode from user mode then a Trap generated which change the mode bit from 1 to 0. When kernel execution is complete then trap 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 for back to withdraw money. Then the staff of bank is kernel and our command in user mode is withdraw the money. This request is executed by bank staff to withdraw the money. So, without kernel, 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 kernel mode and decision is system call
Now lets look at comparisons of Trap and Interrupt
Trap VS Interrupt
|it is generated by an instruction in the user program so they called software interrupts.||it is generated by hardware device so they are called hardware interrupt.|
|Trap can arrive after the execution of any instruction||It can occur at the execution time of any instruction|
|It raised from user program and indicates the OS to perform some functionality immediately.||A signal to processor emitted by hardware indicating an event that needs immediate attention.|
|It transfers the Control to trap handler.||Control transfer to corresponding interrupt handler routine.|