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, waking up of processes, inter-process communication, etc.
- Storage management: Allocation and management of memory, 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, drivers, 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, and returns the result to the process, and then switches back to user 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 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:
- System Call (Trap): This is a way for user mode processes to actively switch to kernel mode. It is mainly used to perform tasks that require kernel mode, such as accessing disk resources. The mechanism of system calls is essentially based on the use of an interrupt specially opened by the operating system for users.
- Interrupt: When a peripheral device completes the requested operation of a user, it sends a corresponding interrupt signal to the CPU. At this time, the CPU suspends the execution of the next instruction to execute the interrupt handler corresponding to the interrupt signal. If the previously executed instruction is a program in user mode, then this process naturally involves 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, and so on.
- Exception: When the CPU is executing a program running in user mode, certain exceptions that were not known in advance may occur. At this time, the current running process will be switched to the kernel-related program that handles this exception, which means switching 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 handler 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, waking up of processes, inter-process communication, etc.
- Memory management: Allocation, recovery, and obtaining the size and address of occupied memory areas for jobs.
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 low-level resources such as files, devices, networks, etc., provided by the operating system.
Process of System Call#
The process of a system call can be simplified into the following steps:
- 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).
- After the interrupt occurs, the currently executing program is interrupted and jumps to the interrupt handler. The kernel program starts to execute, which means it starts to handle the system call.
- After the kernel processing is completed, the Trap is triggered again, causing another interrupt, and switching back to user mode to continue working.