Operating Systems & Programming Interfaces | 操作系统与编程接口

📚 Operating Systems & Programming Interfaces | 操作系统与编程接口

Programming at A-Level extends beyond writing algorithms; it requires understanding how software interacts with the underlying hardware through the operating system (OS). The OS provides essential abstractions like processes, memory management, and file systems that directly affect how programs execute. This revision article explores core OS concepts from a programmer’s perspective, focusing on the interfaces that allow user code to request services from the kernel.

在 A-Level 编程中,不仅要会写算法,还要理解软件如何通过操作系统与底层硬件交互。操作系统提供了进程、内存管理、文件系统等关键抽象,直接影响程序的执行方式。本文从程序员的角度复习操作系统核心概念,重点介绍让用户代码能够请求内核服务的接口。

1. What Is an Operating System? | 什么是操作系统?

An operating system is system software that manages computer hardware resources and provides common services for application programs. It acts as an intermediary between the user and the hardware, hiding complexity and offering a uniform programming environment. Key functions include process management, memory management, file system management, and device I/O control.

操作系统是管理计算机硬件资源并为应用程序提供通用服务的系统软件。它充当用户和硬件之间的中介,隐藏复杂性并提供统一的编程环境。关键功能包括进程管理、内存管理、文件系统管理以及设备 I/O 控制。

From a programmer’s viewpoint, the OS enables multiple applications to run concurrently without interference. It enforces protection mechanisms, schedules CPU time, and manages virtual memory. Understanding these services is crucial for writing efficient, secure, and portable code.

从程序员的角度看,操作系统允许多个应用程序并发运行而不相互干扰。它执行保护机制、调度 CPU 时间并管理虚拟内存。理解这些服务对于编写高效、安全和可移植的代码至关重要。


2. The Role of the OS in Program Execution | 操作系统在程序执行中的作用

When a program is executed, the OS creates a process—an instance of the program in memory. The OS allocates memory for code, data, stack, and heap segments. It loads the executable file, resolves dynamic libraries, and transfers control to the program’s entry point. The program then runs in user mode, switching to kernel mode via system calls whenever it needs privileged operations.

当程序执行时,操作系统会创建一个进程——程序在内存中的实例。OS 为代码、数据、堆栈和堆段分配内存。它加载可执行文件,解析动态链接库,并将控制权转移到程序的入口点。程序随后在用户模式下运行,每当需要特权操作时,通过系统调用切换到内核模式。

The OS also manages the process lifecycle: creation, scheduling, blocking, and termination. Programmers can influence execution by using APIs that trigger these state transitions, such as creating child processes or waiting for I/O completion.

操作系统还管理进程生命周期:创建、调度、阻塞和终止。程序员可以通过使用触发这些状态转换的 API 来影响执行,例如创建子进程或等待 I/O 完成。


3. Process Management and Scheduling | 进程管理与调度

A process consists of a program counter, registers, and memory segments. The OS maintains a process control block (PCB) for each process, storing its state, priority, and accounting information. The scheduler decides which process gets the CPU next, aiming to maximise throughput and minimise response time.

进程由程序计数器、寄存器和内存段组成。操作系统为每个进程维护一个进程控制块(PCB),存储其状态、优先级和统计信息。调度程序决定哪个进程接下来获得 CPU,旨在最大化吞吐量并最小化响应时间。

Common scheduling algorithms studied at A-Level include First Come First Served (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin. Programmers rarely implement these directly but experience their effects through process responsiveness and fairness. For example, a CPU-bound process may starve I/O-bound processes under certain policies.

A-Level 常见的调度算法包括先来先服务(FCFS)、最短作业优先(SJF)、优先级调度和轮转调度(Round Robin)。程序员很少直接实现这些算法,但会通过进程响应性和公平性感受其影响。例如,在某种策略下,CPU 密集型进程可能导致 I/O 密集型进程饥饿。


4. Memory Management for Programmers | 面向程序员的内存管理

Memory management involves allocating physical and virtual memory to processes. The OS uses techniques like paging, segmentation, and virtual memory to give each process its own address space. From a programming perspective, dynamic memory allocation (e.g., malloc in C, new in Java) relies on the OS’s heap manager.

内存管理涉及为进程分配物理和虚拟内存。操作系统使用分页、分段和虚拟内存等技术,为每个进程提供自己的地址空间。从编程的角度看,动态内存分配(如 C 语言的 malloc、Java 的 new)依赖于操作系统的堆管理器。

Understanding the memory layout—text, data, BSS, heap, and stack—helps in debugging buffer overflows or segmentation faults. Virtual memory allows processes to exceed physical RAM by swapping pages to disk, but excessive paging leads to thrashing, degrading performance. Good programming practices, such as avoiding memory leaks and using appropriate data structures, can minimise memory footprint.

理解内存布局——文本段、数据段、BSS 段、堆和栈——有助于调试缓冲区溢出或段错误。虚拟内存允许进程通过将页面交换到磁盘来超出物理 RAM,但过度的分页会导致系统颠簸(thrashing),降低性能。良好的编程实践,如避免内存泄漏和使用合适的数据结构,可以最小化内存占用。


5. File Systems and I/O Operations | 文件系统与输入/输出操作

The file system provides a logical view of persistent storage. Programmers interact with files via high-level language libraries that ultimately call OS file operations: open, read, write, seek, and close. The OS handles buffering, permission checks, and device drivers.

文件系统提供了持久存储的逻辑视图。程序员通过高级语言库与文件交互,这些库最终调用操作系统文件操作:open、read、write、seek 和 close。操作系统处理缓冲、权限检查和设备驱动程序。

In A-Level projects, you may need to read from text files, write binary data, or traverse directories. The OS abstracts differences between storage devices, so the same read() call works whether the file is on a hard disk, SSD, or network drive. Understanding file descriptors and streams is essential for I/O redirection and piping in shell programming.

在 A-Level 项目中,你可能需要从文本文件读取、写入二进制数据或遍历目录。操作系统抽象了存储设备之间的差异,因此相同的 read() 调用无论文件在硬盘、SSD 还是网络驱动器上都能工作。理解文件描述符和流对于 Shell 编程中的 I/O 重定向和管道至关重要。


6. System Calls: The Programming Interface | 系统调用:编程接口

System calls are the programming interface between user-space applications and the kernel. When a program needs a service—such as creating a process, allocating memory, or accessing hardware—it issues a software interrupt or uses a special instruction (e.g., syscall on x86-64). The CPU switches to kernel mode, executes the requested service, and returns the result.

系统调用是用户空间应用程序与内核之间的编程接口。当程序需要某项服务时——例如创建进程、分配内存或访问硬件——它会发出一个软件中断或使用特殊指令(例如 x86-64 上的 syscall)。CPU 切换到内核模式,执行所请求的服务,并返回结果。

At A-Level, you are expected to recognise common system calls and their effects. They are typically wrapped in library functions for convenience. For example, the C standard library’s printf() eventually calls the write() system call. Below is a table of important Linux system calls with their descriptions.

在 A-Level 中,你需要识别常见的系统调用及其效果。它们通常被封装在库函数中以方便使用。例如,C 标准库的 printf() 最终会调用 write() 系统调用。下表列出了重要的 Linux 系统调用及其描述。

System Call Description
fork() Creates a new child process by duplicating the calling process.
execve() Replaces the current process image with a new program.
wait() / waitpid() Makes the parent process wait for a child to change state.
open() / read() / write() / close() Manage file I/O operations.
mmap() Maps files or devices into memory.
exit() Terminates the calling process.

中文对应表:fork() 创建新子进程;execve() 用新程序替换当前进程映像;wait()/waitpid() 使父进程等待子进程状态改变;open()/read()/write()/close() 管理文件 I/O;mmap() 将文件或设备映射到内存;exit() 终止调用进程。

These system calls form the backbone of process and file management in Unix-like systems. In Edexcel exams, you

Published by TutorHao | A-Level 编程 Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading