Understanding Operating System (Part 3)

Cyberic Technology
CodeX
Published in
6 min readNov 24, 2022

Welcome back again from, our Understanding Operating System series. In the last article, we put in detail how a computer system is structured and layered as well as a brief introduction to Linux operating systems. In today’s episode, I will write about concepts of computer systems’ physical state and kernels, how they work and why they are a crucial part of an operating system.

image bypixabay

Overview

  • Multiprogramming & Hardware Requirements
  • CPU Privilege Modes
  • X86 Protection Rings
  • Mode Switch
  • Interrupts

Multiprogramming and Hardware Requirements

image by pixabay

Multiprogramming is running multiple processes at the same time. Multiprogramming works by CPU switching across the processes quickly enough to enable all processes to run simultaneously. Technically CPUs in a system could only run 1 program(process) at one time. In the past, the CPU used to be able to handle one process but technological advancements enable the CPU to become extremely faster at processing, it creates an illusion that all of the processes are running at the same time. However, there are also modern CPUs programmed with limited purposes to run only one process at a time.

Fully functional Operating Systems also have Hardware Requirements to run properly. An operating system needs certain Hardware Requirements inside CPUs like Interrupt Mechanism, Clock and CPU Privilege Modes.

Interrupt Mechanism

An interrupt Mechanism is required in a CPU to enable the preemption of running processes whenever an event occurs.

Clock

A Clock is used to determine how long a program has been running.

CPU Privilege Modes

CPU also has barriers to separating the Kernel and User, so non-privileged users cannot access the Operating System’s kernel. There are two modes inside this module: Supervisor Mode and User Mode.

Supervisor Mode (Kernel Mode)

Supervisor Mode is also called Kernel Mode because of its rights to access directly to the kernel to modify, edit, delete and install various functions and processes. In this mode, the CPU enables all the available instructions and gives the OS kernel the ability to access all memory on the system. By giving more instructions and certain privileges to the OS kernel, the kernel now has control over the entire system’s memory.

User Mode

User Mode is heavily limited as in privilege over a computer system. User mode is the opposite of the supervisor mode and User mode is proposed for non-admin users or guests who are going to just temporarily access the computer. Unlike Supervisor mode, the CPU disables all privileged instructions and most operations which access the system memory directly are restricted. The program which is being used by a user must perform a System Call(SCI) to the Operating System to request memory and other required system resources. This feature inside a CPU can effectively isolate the User Processes from interfering with the kernel.

X86 Protection Rings

image by Wikipedia under CC license

X86 Protection Rings are also another set of protection layers enforced by the operating system to restrict access inside to the kernel. There are a total of 4 rings starting from ring 0 to ring 3. As you can see from the image, ring 0 has the greatest number of privileges and technically it is lying side the Kernel level. Ring 0 can access all the memory and execute all the CPU instructions. Ring 3 is the userspace layer and it has the fewest privileges in these rings with restricted access. In practice, most operating systems use only ring 0 and ring 3. But there is an exception with the OS/2 operating system.

Hypervisor Mode

image by author

Hypervisor Mode is being used more than in the past with the growing popularity of Virtual Machines. Hypervisor Mode is an extra privilege level below ring 0 and Hypervisor Mode is also known as ring -1. Hypervisor Mode enables instructions that allow multiple operating systems to share the same processor. This brings us the ability to use more than one operating system while a computer is running. A Hypervisor Software is required to enable Hypervisor Mode. Some popular Hypervisor software instances are Vmware and Oracle VirtualBox.

Mode Switch

CPU switches user, kernel or hypervisor mode according to the user who is controlling the computer. If the CPU gets a signal from the kernel that a mode switch is required to perform a certain process, it will perform a mode switch but the Mode Switch function can be a slow operation compared to other CPU functions but this also depends on the power of your hardware. Another case of a mode switch occurring in a system also be the result of the Interrupts being performed. Interrupts can be categorized into two groups:

Involuntary

Involuntary Interrupts are occurred by external processes outside of the Operating System. There are two Involuntary Interrupts: I/O interrupts and Clock Interrupts. I/O interrupts are the interrupt signals from Input/ Output devices like a keyboard or a mouse. I/O interrupt could occur if a User moves/presses his mouse or type a word on keyboard while a process is running. Clock Interrupts are the time-based interrupt signals from the timers present on your motherboard. Clock Interrupts can occur if a process is programmed to run X operation is a specific time.

Voluntary

Voluntary Interrupts only exist inside the softwares(processes) and unlike Involuntary Interrupts it is not coming from hardwares. Two types of Voluntary Interrupts includes System Calls and Exceptions. System calls are also known as System Call Interface(SCI), a protocol where the Userspace communicates with Kernel. In this case, System calls being picked on as Interrupts. Exceptions are also Interrupts which is generated by running processes. Exceptions can be triggered if there is segfault which is a segmentation fault, a protective function which will occur if a software tries to access the Kernel by implementing codes which can be the results of a bug inside a process or malicious activities within the program itself. Another incident in Exceptions can also occur if a program is trying to do perform an illegal operation in this case is Divide by Zero error. Divide by Zero error can be occurred as an Interrupt by hardware conflicting with computer memory or if a user tried to execute illegal operations like dividing a number with 0.

Divide By Zero Error In Microsoft Calculator
image by author

These are the everything about Interrupts’ relationship with the Computer System. A CPU provides hardware mechanisms and logical supports for detecting interrupt requests and handling the interrupts.

This is everything about our Understanding Operating System series. I hope our serialized articles explaining Operating Systems really helped you understand how an Operating System works in general. This is our final article of this series. Thanks for reading out!

Reference:

--

--