操作系统与系统调用再认识

操作系统是什么操作的系统?

一直觉得【操作系统】这个名字很怪异,对所谓【操作系统】有一定认识后发现,这个名字并没有深刻表达所谓【操作系统】这个东西的内涵。【操作系统】这个名字表明【操作系统】是一种【系统】,但没有指明是什么【操作】。

对【操作系统】理论有研究的同学都知道,【操作系统】在计算机使用过程中充当两个角色,第一是虚拟机,为用户提供高级的机器接口;第二是资源管理,实现计算机资源多道使用。资源的多道管理只是【操作系统】一些内部精制的算法,于系统外部是感觉不到的;【操作系统】的系统角色体现在它提供的逻辑接口,这些接口就是行话【系统调用(system call)】。系统论告诉我们,系统与操作接口是一对一的关系,只要我们对系统的操作接口集作一定归纳总结,便略知操作系统的是什么操作的系统。

查维基百科的system calls 条目得知(http://en.wikipedia.org/wiki/System_calls),Linux有319个system call,而这些接口大致可分为五类:

System calls can be roughly grouped into five major categories:

  1. Process Control.
  2. File management.
    • create file, delete file
    • open, close
    • read, write, reposition
    • get/set file attributes
  3. Device Management.
    • request device, release device
    • read, write, reposition
    • get/set device attributes
    • logically attach or detach devices
  4. Information Maintenance.
    • get/set time or date
    • get/set system data
    • get/set process, file, or device attributes
  5. Communication.
    • create, delete communication connection
    • send, receive messages
    • transfer status information
    • attach or detach remote devices

有这么多的功能的接口,我们可以初步得知,【操作系统】本身是种混杂系统,所以不便单一为其定名是什么操作的系统,因而泛泛为操作,系统。

操作系统的混杂性

操作系统的混杂性表现在两个维度,第一维度是系统的双重用户,第二维是多个子系统共存。要对操作系统有深入的了解必须对操作系统的混杂性有清晰的认识,对操作系统进行进一步的【子系统】划分最好。

双重用户系统

管理用户(root)与普通用户

多子系统

VM, PM, VFS, NET, IPC

系统调用

系统调用的意义

System calls provide a layer between the hardware and user-space processes. This layer serves three primary purposes.

First, it makes programming easier by freeing users from studying low-level programming characteristics of hardware devices. Second, it greatly increases system security, because the kernel can check the accuracy of the request at the interface level before attempting to satisfy it. Last but not least, these interfaces make programs more portable, because they can be compiled and executed correctly on every kernel that offers the same set of interfaces.

系统调用的实现

application programmer interface与system call 的关系

API只是一个逻辑定义,像POSIX,system call才真正实现软件中断,调用OS的代码

application programmer interface is a function definition that specifies how to obtain a given service, while the system call is an explicit request to the kernel made via a software interrupt.

API、C库和system call 的关系

API与system call不存在完全的一对一关系,比如有些API根本不使用system call,C库是一种包装(wrapper routines )并实现,用户进程也是通过C库间接调用system call。

Unix systems include several libraries of functions that provide APIs to programmers. Some of the APIs defined by the libc standard C library refer to wrapper routines (routines whose only purpose is to issue a system call). Usually, each system call has a corresponding wrapper routine, which defines the API that application programs should employ.

The converse is not true, by the wayan API does not necessarily correspond to a specific system call. First of all, the API could offer its services directly in User Mode. (For something abstract such as math functions, there may be no reason to make system calls.) Second, a single API function could make several system calls. Moreover, several API functions could make the same system call, but wrap extra functionality around it.

Each system call is identified by means of a symbolic constant whose platform-dependent definition is specified in <asm-arch/unistd.h>.

裸男
Nakeman.cn 2023 Build by Gatsby and Tailwind, Deploy on Netlify.