%toc

计算机是什么样子的?

最终要掌握的东西

进程管理

介绍

调度

内核同步

何时需要,何时不需要

有问题就需要,没有就不需要……

各种同步的方法

对称多处理器(SMP)

什么是SMP

什么是NUMA

SMP Scheduling

Synchronization Problem

内存管理

逻辑地址、虚拟地址、物理地址

Linux的具体情况

Methods

虚拟文件系统理论?

安卓介绍

电源管理

Project

考试情报 \(\rightarrow\) 目标:期末60分

脑子里还剩下的东西

进程管理

内核同步

对称多处理器 (SMP)

读源代码 -- SMP

基于对象的文件系统 (Object-based File System)

课程内容摘抄

整体介绍

什么是Linux(历史、家族、特点、发行版)

两种模式

什么是内核

系统源码结构

内核源码结构

System startup: BIOS / BootMonitor

Booting
一个引导进程,会在用户打开计算机时,打开操作系统
Booting Sequence
载入操作系统时,计算机做的一系列操作
1. Turn on
2. CPU jump to address of BIOS (0xFFFF0)
3. BIOS runs POST (Power-On Self Test)
4. Find bootale devices
5. Load and execute boot sector form MBR
6. Load OS
BIOS, Basic Input/Output System
BIOS是计算机刚打开时运行的一段代码
BIOS最基本的函数是一段写在芯片里的代码,可以识别和控制计算机的众多设备
MBR, Master Boot Record
OS is booted from a hard disk, where the Master Boot Record (MBR) contains the primary boot loader
The MBR is a 512-byte sector, located in the first sector on the disk (sector 1 of cylinder 0, head 0)
After the MBR is loaded into RAM, the BIOS yields control to it.
The first 446 bytes are the primary boot loader, which contains both executable code and error message text
The next 64 bytes are the partition table, which contains a record for each of four partitions
The MBR ends with two bytes that are defined as the magic number (0xAA55). The magic number serves as a validation check of the MBR
To see the contents of MBR, use this command:
# dd if=/dev/hda of=mbr.bin bs=512 count=1
# od -xa mbr.bin
The dd command, which needs to be run from root, reads the first 512 bytes from /dev/hda (the first Integrated Drive Electronics, or IDE drive) and writes them to the mbr.bin file.
The od command prints the binary file in hex and ASCII formats.
Boot Loader
其实应该叫Kernel Loader,作用是载入Linux Kernel
Optional, initial RAM disk
GRUB和LILO是最流行的两种Linux Boot Loader
GRUB, GRand Unified Bootloader
GRUB is an operating system independant boot loader
A multiboot software packet from GNU
GRUB boot process
1. The BIOS finds a bootable device (hard disk) and transfers control to the master boot record
2. The MBR contains GRUB stage 1. Given the small size of the MBR, Stage 1 just load the next stage of GRUB
3. GRUB Stage 1.5 is located in the first 30 kilobytes of hard disk immediately following the MBR. Stage 1.5 loads Stage 2.
4. GRUB Stage 2 receives control, and displays to the user the GRUB boot menu (where the user can manually specify the boot parameters).
5. GRUB loads the user-selected (or default) kernel into memory and passes control on to the kernel.
LILO, LInux LOader
Not depend on a specific file system
Can boot from harddisk and floppy
Up to 16 different images
Must change LILO when kernel image file or config file is changed
Kernel
大多数计算机操作系统的核心部分
Kernel会一直存在于内存中,直至断电
Tasks
1. Process management
2. Memory management
3. Device management
4. System call
Kernel Image
压缩过的Kernel图标
zImage size less than 512 KB
bzImage size greater than 512 KB
Major functions flow for Linux kernel boot
Init Process
Kernel运行的第一段代码,也是Linux中所有进程的父进程
The first processes that init starts is a script /etc/rc.d/rc.sysinit
Based on the appropriate run-level, scripts are executed to start various processes to run the system and make it functional
Process Id = 1
Init is responsible for starting system processes as defined in the /etc/inittab file
Init typically will start multiple instances of "getty" which waits for console logins which spawn one's user shell process
Upon shutdown, init controls the sequence and processes for shutdown
Inittab file
The inittab file describes which processes are started at bootup and during normal operation
/etc/init.d/boot
/etc/init.d/rc
The computer will be booted to the runlevel as defined by the initdefault directive in the /etc/inittab file
id:5:initdefault:
RunLevels
A runlevel is a software configuration of the system which allows only a selected group of processes to exist
The processes spawned by init for each of these runlevels are defined in the /etc/inittab file
Init can be in one of eight runlevels: 0-6
rc#.d files
rc#.d files are the scripts for a given run level that run during boot and shutdown
The scripts are found in the directory /etc/rc.d/rc#.d/ where the symbol # represents the run level
init.d
Deamon is a background process
init.d is a directory that admin can start/stop individual demons by changing on it
/etc/rc.d/init.d/ (Red Hat/Fedora )
/etc/init.d/ (S.u.s.e.)
/etc/init.d/ (Debian)
Start/stop deamon
Admin can issuing the command and either the start, stop, status, restart or reload option
i.e. to stop the web server:
cd /etc/rc.d/init.d/
(or /etc/init.d/ for S.u.s.e. and Debian)
httpd stop

模块(什么是内核模块、优缺点、举例、如何编译)

Module, Kernel Module
(wiki) An object file that contains code to extend the running kernel;
(RedHat) Modules are pieces of code that can be loaded and unloaded into the kernel upon demand.
Advantages
1. Allowing the dynamic insertion and removal of code from the kernel at run-time.
2. Save memory cost
Disadvantages
1. Fragmentation Penalty \(\rightarrow\) decrease memory performance

/proc 文件系统(介绍、在其内创建文件的模块编程)

安卓介绍

Java回顾

XML回顾

电源管理

Linux

Android

Wava Lock

System Sleep (又名Suspend)

Battery Service

读源代码 -- Booting

代码类型

基础知识

系统引导

通过LILO来进行引导

实模式下的系统初始化

实模式下的系统初始化-setup.S

保护模式下的系统初始化

Init进程和系统配置

思考题

读源代码 -- 文件系统

Project 1 -- 编译Linux内核

下载Linux内核

https://www.kernel.org

编译Linux内核

Project 2A -- 模块编程

调试方法

编程方法

样例代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

//struct proc_dir_entry *entry;
static char* whom="world";
static int howmany=1;

static int hello_proc_show(struct seq_file *m, void *v) {
    seq_printf(m, "Hello proc!\n");
    return 0;
}

static int hello_proc_open(struct inode *inode, struct file *file) {
    return single_open(file, hello_proc_show, NULL);
}

static const struct file_operations hello_proc_fops = {
    .owner = THIS_MODULE,
    .open = hello_proc_open,
    .read = seq_read,
    .llseek = seq_lseek,
    .release = single_release,
};

static int __init hello_init(void) {
    printk("<6>Greeting from a linux kernel module.\n");
    printk("<6>whom=%s,howmany=%d\n",whom,howmany);
    proc_create("hello_proc", 0, NULL, &hello_proc_fops);
    return 0;
}

static void __exit hello_exit(void) {
    remove_proc_entry("hello_proc", NULL);
    printk("<6>Bye.\n");
}

module_init(hello_init);

module_exit(hello_exit);

MODULE_LICENSE("GPL");

module_param(howmany, int, S_IRUGO);
module_param(whom, charp, S_IRUGO);

Project 2B -- 进程管理

编程方法

Commands Frequently Used

Project 3 -- 内存管理

调试流程

  1. 安装自己写的mtest模块
  2. 执行下列命令,分别使用dmesg察看结果
    1. echo listvma > mtest
      • 列出当前进程的所有虚拟内存
    2. echo findpage <addr> > mtest
      • 找到某虚拟地址对应的物理地址
    3. echo writeval <addr> <value> > mtest
      • 向某虚拟地址写对应的值

Project 4 -- 文件系统

调试流程

  1. 安装自己改好的romfs模块
  2. apt-get install genromfs
  3. 创建一个文件夹,里面放上各种文件(当然得有NULL文件),把执行权限都删去
  4. 把这个文件夹生成romfs:genromfs -f <xx.img>
  5. 生成一个空文件夹
  6. 然后挂载:mount -o loop <xx.img> <empty_dir>
  7. 执行测试命令
    • ls -l <empty_dir>,结果应当没有NULL
    • ls -l <empty_dir>/NULL,应当可以看到NULL,并且发现其有x权限
    • cat <empty_dir>/NULL,应当看不到NULL的内容,只能看到*******
    • cat <origin_dir>/NULL,应当看得到NULL的内容

附录

Page created on 2020-12-06