深入理解Linux内核3

Unix内核概述

Unix内核提供了应用程序可以运行的环境,因此,内核必须实现一组服务及相应的接口。应用程序使用这些接口而不会跟硬件资源直接交互。

- 进程/内核模式

  • 用户态和内核态
  • 进程是动态的实体,在系统内通常只有有限的生存期
  • 内核本身不是一个进程,而是进程的管理者
    -请求内核服务的进程使用系统调用
  • 除了用户进程,还有些称为内核线程的特权进程
    • 以内核态运行在内核地址空间
    • 不与用户交互,不需要终端设备
    • 通常系统创建时启动,然后一直处于活跃状态
  • 激活内核例程的几种方式
    • 系统调用
    • 正在执行进程的CPU发出一个异常信号
    • 外围设备向CPU发出一个中断信号
    • 内核线程被执行

进程实现

  • 每个进程有一个进程描述符(包含有关进程的当前状态的信息)
  • 当内核暂停一个进程执行时,把几个相关处理器寄存器保存在进程描述符中

    • 程序计数器PC,栈指针sp寄存器
    • 通用寄存器
    • 浮点寄存器
    • 包含CPU状态信息的处理器控制寄存器(处理器状态字)
    • 跟踪进程对RAM访问的内存管理寄存器
  • 内核恢复一个进程执行时,用进程描述符中的合适字段装载CPU寄存器

  • 等待状态可能会有很多,有进程描述符队列实现、

可重入内核

  • 所有Unix内核都是可重入的(若干个进程可以同时在内核态下执行)
  • 可重入的一种方式是编写函数,至修改局部变量
  • 内核也可以包含非重入函数,利用锁机制来保证有且只有一个进程执行非重入函数
  • 内核控制路径(指令序列)
  • 内核控制路径交错执行

进程地址空间

  • 每个进程运行在自己的私有地址空间
  • 用户态下涉及私有栈、数据区和代码区
  • 内核态下,进程访问内核的数据区和代码区,但使用另外的私有栈
  • 每个内核路径都引用自己的私有内核栈
  • 有时候也会共享部分地址空间

    同步和临界区

  • 一致性状态

  • 抢占式和非抢占式内核
  • 禁止中断
  • 信号量

    • 组成:一个整数变量、一个等待进程的链表和两个原子方法(down()和up())。
  • 自旋锁(检查信号量耗时多,对于时间较短的操作较低效)

    • 跟信号量很相似
    • 但是没有进程链表
    • 但处理器环境下无效
  • 避免死锁

信号和进程间通信

  • 信号量
  • 消息队列
  • 共享内存
  • IPC资源是持久不变的

进程管理

  • fork()和_exit()系统调用分别创建一个新进程和终止一个进程
  • exec()类系统调用装入一个新程序
  • fork()的进程是父进程,而新进程是它的子进程。
  • 父子进程可以互相找到对方
  • 每个进程的数据结构中有两个指针,一个指向父,一个指向子
  • 写时复制技术

僵死进程

  • 父进程等待子进程结束,wait4()系统调用
  • init的特殊系统进程,系统初始化时创建
  • 当一个进程终止时,内核改变它的所有子进程的进程描述符指针,成为init的孩子

进程组和登录会话

  • 进程组(作业的抽象)
  • 登录会话通常是shell进程为用户创建的第一条命令
  • 一个登录会话可以让几个进程组同时处于活动状态
  • bg和fg命令的使用

内存管理

重点
- 虚拟内存
- 随机访问存储器RAM的使用
- 内核内存分配器KMA
- 进程虚拟地址空间处理,调页分配策略
- 高速缓存
- 设备驱动程序

Preface The Audience for This Book Organization of the Material Level of Description Overview of the Book Background Information Conventions in This Book How to Contact Us Safari? Enabled Acknowledgments Chapter 1. Introduction Section 1.1. Linux Versus Other Unix-Like Kernels Section 1.2. Hardware Dependency Section 1.3. Linux Versions Section 1.4. Basic Operating System Concepts Section 1.5. An Overview of the Unix Filesystem Section 1.6. An Overview of Unix Kernels Chapter 2. Memory Addressing Section 2.1. Memory Addresses Section 2.2. Segmentation in Hardware Section 2.3. Segmentation in Linux Section 2.4. Paging in Hardware Section 2.5. Paging in Linux Chapter 3. Processes Section 3.1. Processes, Lightweight Processes, and Threads Section 3.2. Process Descriptor Section 3.3. Process Switch Section 3.4. Creating Processes Section 3.5. Destroying Processes Chapter 4. Interrupts and Exceptions Section 4.1. The Role of Interrupt Signals Section 4.2. Interrupts and Exceptions Section 4.3. Nested Execution of Exception and Interrupt Handlers Section 4.4. Initializing the Interrupt Descriptor Table Section 4.5. Exception Handling Section 4.6. Interrupt Handling Section 4.7. Softirqs and Tasklets Section 4.8. Work Queues Section 4.9. Returning from Interrupts and Exceptions Chapter 5. Kernel Synchronization Section 5.1. How the Kernel Services Requests Section 5.2. Synchronization Primitives Section 5.3. Synchronizing Accesses to Kernel Data Structures Section 5.4. Examples of Race Condition Prevention Chapter 6. Timing Measurements Section 6.1. Clock and Timer Circuits Section 6.2. The Linux Timekeeping Architecture Section 6.3. Updating the Time and Date Section 6.4. Updating System Statistics Section 6.5. Software Timers and Delay Functions Section 6.6. System Calls Related to Timing Measurements Chapter 7. Process Scheduling Section 7.1. Scheduling Policy Section 7.2. The Scheduling Algorithm Section 7.3. Data Structures Used by the Scheduler Section 7.4. Functions Used by the Scheduler Section 7.5. Runqueue Balancing in Multiprocessor Systems Section 7.6. System Calls Related to Scheduling Chapter 8. Memory Management Section 8.1. Page Frame Management Section 8.2. Memory Area Management Section 8.3. Noncontiguous Memory Area Management Chapter 9. Process Address Space Section 9.1. The Processs Address Space Section 9.2. The Memory Descriptor Section 9.3. Memory Regions Section 9.4. Page Fault Exception Handler Section 9.5. Creating and Deleting a Process Address Space Section 9.6. Managing the Heap Chapter 10. System Calls Section 10.1. POSIX APIs and System Calls Section 10.2. System Call Handler and Service Routines Section 10.3. Entering and Exiting a System Call Section 10.4. Parameter Passing Section 10.5. Kernel Wrapper Routines Chapter 11. Signals Section 11.1. The Role of Signals Section 11.2. Generating a Signal Section 11.3. Delivering a Signal Section 11.4. System Calls Related to Signal Handling Chapter 12. The Virtual Filesystem Section 12.1. The Role of the Virtual Filesystem (VFS) Section 12.2. VFS Data Structures Section 12.3. Filesystem Types Section 12.4. Filesystem Handling Section 12.5. Pathname Lookup Section 12.6. Implementations of VFS System Calls Section 12.7. File Locking Chapter 13. I/O Architecture and Device Drivers Section 13.1. I/O Architecture Section 13.2. The Device Driver Model Section 13.3. Device Files Section 13.4. Device Drivers Section 13.5. Character Device Drivers Chapter 14. Block Device Drivers Section 14.1. Block Devices Handling Section 14.2. The Generic Block Layer Section 14.3. The I/O Scheduler Section 14.4. Block Device Drivers Section 14.5. Opening a Block Device File Chapter 15. The Page Cache Section 15.1. The Page Cache Section 15.2. Storing Blocks in the Page Cache Section 15.3. Writing Dirty Pages to Disk Section 15.4. The sync( ), fsync( ), and fdatasync( ) System Calls Chapter 16. Accessing Files Section 16.1. Reading and Writing a File Section 16.2. Memory Mapping Section 16.3. Direct I/O Transfers Section 16.4. Asynchronous I/O Chapter 17. Page Frame Reclaiming Section 17.1. The Page Frame Reclaiming Algorithm Section 17.2. Reverse Mapping Section 17.3. Implementing the PFRA Section 17.4. Swapping Chapter 18. The Ext2 and Ext3 Filesystems Section 18.1. General Characteristics of Ext2 Section 18.2. Ext2 Disk Data Structures Section 18.3. Ext2 Memory Data Structures Section 18.4. Creating the Ext2 Filesystem Section 18.5. Ext2 Methods Section 18.6. Managing Ext2 Disk Space Section 18.7. The Ext3 Filesystem Chapter 19. Process Communication Section 19.1. Pipes Section 19.2. FIFOs Section 19.3. System V IPC Section 19.4. POSIX Message Queues Chapter 20. Program ExZecution Section 20.1. Executable Files Section 20.2. Executable Formats Section 20.3. Execution Domains Section 20.4. The exec Functions Appendix A. System Startup Section A.1. Prehistoric Age: the BIOS Section A.2. Ancient Age: the Boot Loader Section A.3. Middle Ages: the setup( ) Function Section A.4. Renaissance: the startup_32( ) Functions Section A.5. Modern Age: the start_kernel( ) Function Appendix B. Modules Section B.1. To Be (a Module) or Not to Be? Section B.2. Module Implementation Section B.3. Linking and Unlinking Modules Section B.4. Linking Modules on Demand Bibliography Books on Unix Kernels Books on the Linux Kernel Books on PC Architecture and Technical Manuals on Intel Microprocessors Other Online Documentation Sources Research Papers Related to Linux Development About the Authors Colophon Index
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值