banner
ShuWa

ShuWa

是进亦忧,退亦忧。然则何时而乐耶?
twitter

Operating System Basics

Operating System#

1. What is an Operating System?#

Essentially, it is a software program that runs on a computer and is mainly used to manage computer hardware and software resources.
The kernel is the core part of the operating system, responsible for memory management, hardware device management, file system management, and application management. The kernel acts as a bridge between applications and hardware, determining the performance and stability of the system.

2. What are the functions?#

  • Process and thread management: Creation, termination, blocking, awakening of processes, inter-process communication, etc.
  • Storage management: Memory allocation and management, allocation and management of external storage (such as disks), etc.
  • File management: Reading, writing, creation, and deletion of files, etc.
  • Device management: Handling requests or releases of devices (input/output devices, external storage devices, etc.), and device startup functions.
  • Network management: The operating system is responsible for managing the use of computer networks. Networks are the means by which different computers are connected in a computer system, and the operating system needs to manage network configuration, connection, communication, and security to provide efficient and reliable network services.
  • Security management: User authentication, access control, file encryption, etc., to prevent unauthorized access and operation of system resources.

3. User Mode and Kernel Mode#

What are User Mode and Kernel Mode?#

  • User Mode: Processes running in user mode can directly access data from user programs and have lower privileges. When an application needs to perform certain operations that require special privileges, such as disk reading and writing, network communication, etc., it needs to make a system call request to the operating system to enter kernel mode.
  • Kernel Mode: Processes running in kernel mode can access almost any resource of the computer, including system memory space, devices, driver programs, etc., without restrictions, and have very high privileges. When the operating system receives a system call request from a process, it switches from user mode to kernel mode, executes the corresponding system call, returns the result to the process, and then switches back to user mode.
    User Mode and Kernel Mode

Why is there a distinction between User Mode and Kernel Mode? Can't we just use Kernel Mode?#

  • Among all the instructions in the CPU, some instructions are more dangerous, such as memory allocation, clock setting, IO processing, etc. If all programs can use these instructions, it will have a catastrophic impact on the normal operation of the system. Therefore, we need to restrict these dangerous instructions to be executed only in kernel mode. These instructions that can only be executed by the operating system in kernel mode are also called privileged instructions.
  • If there is only one kernel mode in the computer system, all programs or processes must share system resources such as memory, CPU, hard disk, etc. This will lead to competition and conflicts over system resources, affecting system performance and efficiency. Moreover, this will also reduce the security of the system, as all programs or processes have the same privilege level and access rights.

How are User Mode and Kernel Mode switched?#

There are three ways to switch from user mode to kernel mode:

  1. System Call (Trap): It is a way for a user mode process to actively request a switch to kernel mode. It is mainly used to perform tasks that require kernel mode, such as accessing disk resources. The mechanism of a system call is essentially using an interrupt specially opened by the operating system for users.
  2. Interrupt: When a peripheral device completes a user's request operation, it sends a corresponding interrupt signal to the CPU. At this time, the CPU suspends the execution of the next instruction and switches to execute the interrupt handling program corresponding to the interrupt signal. If the previously executed instruction is a program in user mode, then this process naturally undergoes a switch from user mode to kernel mode. For example, when a disk read/write operation is completed, the system switches to the interrupt handling program for disk read/write to perform subsequent operations, etc.
  3. Exception: When the CPU is executing a program running in user mode and encounters some unexpected exceptions, it triggers the corresponding kernel-related program to handle this exception, which also switches to kernel mode. For example, a page fault exception.
    In terms of system processing, interrupts and exceptions are similar. They both use an interrupt vector table to find the corresponding handling program for processing. The difference is that interrupts come from outside the processor and are not caused by any specific instruction, while exceptions are the result of executing the current instruction.

System Call#

These system calls can be roughly divided into the following categories according to their functions:

  • Device management: Handling requests or releases of devices (input/output devices, external storage devices, etc.), and device startup functions.
  • File management: Reading, writing, creation, and deletion of files, etc.
  • Process management: Creation, termination, blocking, awakening of processes, inter-process communication, etc.
  • Memory management: Memory allocation, recovery, obtaining the size and address of occupied memory areas by jobs, etc.

System calls are very similar to ordinary library function calls, except that system calls are provided by the operating system kernel and run in kernel mode, while ordinary library function calls are provided by libraries or users themselves and run in user mode.
Summary: System calls are a way for applications to interact with the operating system. Through system calls, applications can access underlying resources such as files, devices, networks, etc.

Process of System Call#

The process of a system call can be simplified into the following steps:

  1. The program in user mode initiates a system call. Because system calls involve some privileged instructions (instructions that can only be executed by the operating system kernel mode), the user mode program does not have sufficient privileges, so it is interrupted and executed, which is called a Trap (Trap is a kind of interrupt).
  2. After the interrupt occurs, the program currently being executed by the CPU is interrupted and jumps to the interrupt handling program. The kernel program starts to execute, which is the beginning of handling the system call.
  3. After the kernel processing is completed, the Trap is triggered again, causing another interrupt and switching back to user mode to continue working.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.