linux系统分析 1.课程简介 - nanjing university · • 《linux kernel...

65
Linux系统分析 1.课程简介 [email protected] 南京学计算机科学与技术系

Upload: others

Post on 17-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

  • Linux系统分析

    1.课程简介

    张雷雷

    [email protected]

    南京⼤大学计算机科学与技术系

  • 课程概况

    • 性质

    • 选修课

    • 课时

    • 2学时/周

    • 课程前导知识(推荐)

    • 操作系统原理理

    • C语⾔言编程

    • 汇编语⾔言

    • Linux系统编程 !2

  • 课程概况

    • 教学形式

    • 单周课堂讲座

    • 双周上机实验(地点:基础实验楼⼄乙126)

    • 课程考评⽅方式

    • 实验(75%):4~5个实验, 分数最低的实验不不计,可⾃自选题⽬目

    • 课堂报告(20%): ⾃自选题⽬目

    • 课堂参与(5%)!3

  • 课程⽬目标

    • 以Linux内核为基础,结合操作系统基本概念及原理理:

    • 剖析实⽤用操作系统的实现机制(内核⻆角度)

    • 理理解操作系统设计精髓(实现问题)

    • 强化应⽤用实践技能(⽤用户空间⻆角度)

    !4

  • 学习资源

    • 课程主⻚页:http://cs.nju.edu.cn/zhangl/linux/

    • 参考资料料

    • 《Linux Kernel Development》(第3版),Robert Love,机械⼯工业出版社,2011年年

    • 《深⼊入理理解Linux内核》(第3版),陈莉君等译,中国电⼒力力出版社,2007年年

    • 《奔跑吧 Linux内核》,张天⻜飞 著,⼈人⺠民邮电出版社,2017年年

    • 《Linux操作系统实验教程》,费翔林林主编,⾼高等教育出版社,2009年年4⽉月

    • 云主机 !5

    http://cs.nju.edu.cn/zhangl/linux/

  • 学习资源

    • Linux/UNIX系统编程

    • TCPL: The C Programming Language,Brian Kernighan and Dennis Ritchie,1978

    • APUE:Advanced Programming in the Unix Environment(UNIX环境⾼高级编程)W.Richard Stevens,2006

    • The Linux Programming Interface: A Linux and UNIX System Programming Handbook (Linux/UNIX系统编程⼿手册),Michael Kerrisk, 2014

    • Linux内核

    • Professional Linux Kernel Architecture,Wolfgang Mauerer,2008

    • 边学边⼲干-Linux内核指导,第⼆二版,李李善平,浙江⼤大学出版社,2008

    • 学术会议

    • OSDI, USENIX Symposium on Operating Systems Design and Implementation

    • SOSP, ACM Symposium on Operating Systems Principles  !6

  • Linux系统⽆无处不不在

    • Linux基⾦金金会2017年年发布的⼀一组数据:

    • 90%的公有云应⽤用在使⽤用Linux系统

    • 62%的嵌⼊入式市场在使⽤用Linux系统

    • 99%的超级计算机在使⽤用Linux系统

    • 82%的⼿手机操作系统在使⽤用Linux系统

    • 全球100万个顶级域名中超过90%都在使⽤用Linux

    • 全球⼤大部分的股票交易易市场都是基于Linux系统来部署的

    • 全球知名的淘宝⽹网、亚⻢马逊⽹网、易易趣⽹网、沃尔玛等电⼦子商务平台都在使⽤用Linux

    !7

  • Linux简介

    • 符合POSIX标准规范的操作系统

    • POSIX: Portable Operation System Interface of Unix:可移植的操作系统接⼝口

    • 由IEEE开发,ANSI和ISO标准化

    • Free or open software

    • GNU General Pubic Licese(GPL)2.0

    • 可在原程序基础上⾃自由进⾏行行修改,开发⾃自⼰己的程序

    • 发布修改时需要提供源代码

    • 开源⽂文化

    !8

  • Unix

    • 1969, Bell Labs, AT&T

    • Ken Thompson,1943-

    • Dennis Ritchie, 1941-2011

    • Design: simplicity and elegance

    • everything is a file: open(), read(), write(), close()

    • file: array of bytes, unstructured

    • do one thing and do it well

    • separation between policy and mechanism !9

  • Unix

    • AT&T monopoly

    • 1956美国DOJ和解协议, AT&T不不进⼊入计算机业,不不销售任何与计算机有关的产品

    • 1974年年 Berkeley获得Unix源码,BSD开始开发

    • 1974年年美国DOJ起诉AT&T违反《反垄断法》

    • 1982年年AT&T败诉分拆为7个公司,和解协议失效,AT&T进⼊入计算机业,产⽣生商业版Unix

    • 80年年代末,Intel 80x86芯⽚片巨⼤大发展,PC时代来临,OS平台化!10

  • Linux发展历史

    !11

    • Linux诞⽣生于1991年年10⽉月5⽇日,它的产⽣生和开源运动有着密切的关系。

    • 1983年年,Richard Stallman发起GNU(GUN’s Not UNIX)计划

    • 1991年年,Linus Torvalds在⼀一台386电脑上学习Minix操作系统,并动⼿手实现了了⼀一个新的操作系统,然后在comp.os.minix新闻组上发布了了第⼀一个版本的Linux内核。

    • 1993年年,Linux 0.99的代码已经有⼤大约10万⾏行行。

    • 1994年年,采⽤用GPL(General Public License)协议的Linux 1.0正式发布

    • 1995年年,Bob Young创办了了Red Hat公司,以GNU/Linux为核⼼心,把当时⼤大部分的开源软件打包成⼀一个发⾏行行版,这就是RedHat Linux发⾏行行版。

    • 1996年年,Linux 2.0发布,该版本可以⽀支持多种处理理器器,如alpha、mips、powerpc等,内核代码量量⼤大约是40万⾏行行。

  • Linux发展历史

    !12

    • Linux诞⽣生于1991年年10⽉月5⽇日,它的产⽣生和开源运动有着密切的关系

    • 1999年年,Linux 2.2发布,⽀支持ARM处理理器器。第⼀一家国产Linux发⾏行行版—蓝点Linux系统诞⽣生,它是第⼀一个⽀支持在帧缓冲上进⾏行行汉化的Linux中⽂文版本

    • 2001年年,Linux 2.4发布,⽀支持对称多处理理器器SMP和很多外设驱动。同年年,⽑毛德操⽼老老师出版了了《Linux 2.4内核源代码情景分析》

    • 2003年年,Linux 2.6发布。与Linux 2.4相⽐比,该版本增加了了很多性能优化的新特性,使它成为真正意义上的现代操作系统

    • 2008年年,⾕谷歌正式发布Android 1.0,Android系统基于Linux内核来构建。在之后的⼗十年年⾥里里,Android系统占据了了⼿手机系统的霸主地位。

    • 2011年年,Linux 3.0发布。在⻓长达8年年的Linux 2.6开发期间,众多IT巨头持续为Linux内核贡献了了很多新特性和新的外设驱动。同年年,全球最⼤大的Linux发⾏行行版⼚厂商Red Hat宣布营收达到10亿美元

  • Unix

    !13

  • Linux发⾏行行版 – Red Hat

    ➢Fedora Core

    ✓Fedora Core发⾏行行版是Red Hat公司的新技术测试平台,很多新的技术⾸首先会应⽤用到Fedora Core上,等测试稳定了了才会加⼊入Red Hat的RHEL版本中

    ➢RHEL (Red Hat Enterprise Linux )

    ✓RHEL是⾯面向服务器器应⽤用的Linux发⾏行行版,注重性能、稳定性和服务器器端软件的⽀支持。

    ✓2018年年4⽉月Red Hat公司发布的RHEL 7.5操作系统,提升了了性能,增强了了安全性。

    ➢CentOS(Community Enterprise Operating System )

    ✓根据RHEL的源代码重新编译⽽而成

    ✓CentOS不不包含封闭源代码的软件

    ✓CentOS可以免费使⽤用,并由社区主导

    !14

  • Linux发⾏行行版 – Debian

    • Debian Linux

    • Debian由Ian Murdock在1993年年创建,是⼀一个致⼒力力于创建⾃自由操作系统的合作组织。因为Debian项⽬目以Linux内核为主,所以Debian⼀一般指的是Debian GNU/Linux。Debian能⻛风靡全球的主要原因是其特有的apt-get/dpkg软件包管理理⼯工具,该⼯工具被誉为所有Linux软件包管理理⼯工具中最强⼤大、最好⽤用的⼀一个。

    • Ubuntu Linux

    • Ubuntu的中⽂文⾳音译是“乌班图”,是以Debian为基础打造的以桌⾯面应⽤用为主的Linux发⾏行行版。Ubuntu注重提⾼高桌⾯面的可⽤用性以及安装的易易⽤用性等⽅方⾯面,因此经过这⼏几年年的发展,Ubuntu已经成为最受欢迎的桌⾯面Linux发⾏行行版之⼀一。

    !15

  • Linux发⾏行行版

    • SuSE Linux

    • SuSE Linux是来⾃自德国的著名Linux发⾏行行版,在Linux业界享有很⾼高的声誉。SuSE公司在Linux内核社区的贡献仅次于Red Hat公司,培养了了⼀一⼤大批Linux内核⽅方⾯面的专家。SuSE Linux在欧洲Linux市场中占有将近80%的份额,但是在中国的市场份额并不不⼤大

    •优麒麟Linux

    •优麒麟(Ubuntu Kylin)Linux诞⽣生于2013年年,由中国国防科技⼤大学联合Ubuntu、CSIP开发的开源桌⾯面Linux发⾏行行版,是Ubuntu的官⽅方衍⽣生版。该项⽬目以国际社区合作⽅方式进⾏行行开发,并遵守GPL协议

    !16

  • Linux系统层级结构

    !17

    ⽤用户态

    标准函数库

    系统调⽤用(POSIX标准)接⼝口

    模块 内核 (进程管理理、存储管理理、⽂文件管理理、设备管理理、⽹网络管理理)

    设备 驱动

    计算机硬件

    核⼼心态单内核模块 接⼝口

    驱动 接⼝口

    应⽤用程序

  • Linux layers

    !18

    User mode

    User applications For example, bash, LibreOffice, GIMP, Blender, 0 A.D., Mozilla Firefox, etc.

    Low-level system components:

    System daemons:systemd, runit, logind, networkd, PulseAudio, ...

    Windowing system:X11, Wayland, SurfaceFlinger(Android)

    Other libraries:GTK+, Qt, EFL, SDL, SFML, FLTK, GNUstep, etc.

    Graphics:Mesa, AMD Catalyst, ...

    C standard library

    open(), exec(), sbrk(), socket(), fopen(), calloc(), ... (up to 2000 subroutines)glibc aims to be POSIX/SUS-compatible, uClibc targets embedded systems, bionic written for Android, etc.

    Kernel mode Linux kernel

    stat, splice, dup, read, open, ioctl, write, mmap, close, exit, etc. (about 380 system calls)The Linux kernel System Call Interface (SCI, aims to be POSIX/SUS-compatible)

    Process schedulingsubsystem

    IPCsubsystem

    Memory managementsubsystem

    Virtual filessubsystem

    Networksubsystem

    Other components: ALSA, DRI, evdev, LVM, device mapper, Linux Network Scheduler, NetfilterLinux Security Modules: SELinux, TOMOYO, AppArmor, Smack

    Hardware (CPU, main memory, data storage devices, etc.)

    https://en.wikipedia.org/wiki/Template:Linux_layers

    https://en.wikipedia.org/wiki/Bourne-again_shellhttps://en.wikipedia.org/wiki/LibreOfficehttps://en.wikipedia.org/wiki/GIMPhttps://en.wikipedia.org/wiki/Blender_(software)https://en.wikipedia.org/wiki/0_A.D._(video_game)https://en.wikipedia.org/wiki/Mozilla_Firefoxhttps://en.wikipedia.org/wiki/Daemon_(computing)https://en.wikipedia.org/wiki/Systemdhttps://en.wikipedia.org/wiki/Runithttps://en.wikipedia.org/wiki/PulseAudiohttps://en.wikipedia.org/wiki/Windowing_systemhttps://en.wikipedia.org/wiki/X11https://en.wikipedia.org/wiki/Wayland_(display_server_protocol)https://en.wikipedia.org/wiki/SurfaceFlingerhttps://en.wikipedia.org/wiki/GTK%2Bhttps://en.wikipedia.org/wiki/Qt_(software)https://en.wikipedia.org/wiki/Enlightenment_Foundation_Librarieshttps://en.wikipedia.org/wiki/Simple_DirectMedia_Layerhttps://en.wikipedia.org/wiki/Simple_and_Fast_Multimedia_Libraryhttps://en.wikipedia.org/wiki/FLTKhttps://en.wikipedia.org/wiki/GNUstephttps://en.wikipedia.org/wiki/Mesa_(computer_graphics)https://en.wikipedia.org/wiki/AMD_Catalysthttps://en.wikipedia.org/wiki/C_standard_libraryhttps://en.wikipedia.org/wiki/Subroutinehttps://en.wikipedia.org/wiki/GNU_C_Libraryhttps://en.wikipedia.org/wiki/POSIXhttps://en.wikipedia.org/wiki/Single_UNIX_Specificationhttps://en.wikipedia.org/wiki/UClibchttps://en.wikipedia.org/wiki/Bionic_(software)https://en.wikipedia.org/wiki/Android_(operating_system)https://en.wikipedia.org/wiki/Stat_(system_call)https://en.wikipedia.org/wiki/Splice_(system_call)https://en.wikipedia.org/wiki/Dup_(system_call)https://en.wikipedia.org/wiki/Read_(system_call)https://en.wikipedia.org/wiki/Open_(system_call)https://en.wikipedia.org/wiki/Ioctlhttps://en.wikipedia.org/wiki/Write_(system_call)https://en.wikipedia.org/wiki/Mmaphttps://en.wikipedia.org/wiki/Close_(system_call)https://en.wikipedia.org/wiki/Exit_(system_call)https://en.wikipedia.org/wiki/System_callhttps://en.wikipedia.org/wiki/POSIXhttps://en.wikipedia.org/wiki/Single_UNIX_Specificationhttps://en.wikipedia.org/wiki/Scheduling_(computing)https://en.wikipedia.org/wiki/Memory_managementhttps://en.wikipedia.org/wiki/Advanced_Linux_Sound_Architecturehttps://en.wikipedia.org/wiki/Direct_Rendering_Infrastructurehttps://en.wikipedia.org/wiki/Evdevhttps://en.wikipedia.org/wiki/Logical_Volume_Manager_(Linux)https://en.wikipedia.org/wiki/Device_mapperhttps://en.wikipedia.org/wiki/Linux_Network_Schedulerhttps://en.wikipedia.org/wiki/Netfilterhttps://en.wikipedia.org/wiki/Linux_Security_Moduleshttps://en.wikipedia.org/wiki/Security-Enhanced_Linuxhttps://en.wikipedia.org/wiki/TOMOYO_Linuxhttps://en.wikipedia.org/wiki/AppArmorhttps://en.wikipedia.org/wiki/Smack_(Linux_security_module)https://en.wikipedia.org/wiki/Central_processing_unithttps://en.wikipedia.org/wiki/Random-access_memoryhttps://en.wikipedia.org/wiki/Computer_data_storage

  • 应⽤用程序:浏览器器

    !19

  • 应⽤用程序:浏览器器

    • 浏览器器是否是操作系统的⼀一部分?

    !19

  • 应⽤用程序:浏览器器

    • 浏览器器是否是操作系统的⼀一部分?

    • United States v. Microsoft Corp. 2001: the U.S. government accused Microsoft of illegally maintaining its monopoly position in the PC market primarily through the legal and technical restrictions it put on the abilities of PC manufacturers (OEMs) and users to uninstall Internet Explorer …

    !19

  • 应⽤用程序:浏览器器

    • 浏览器器是否是操作系统的⼀一部分?

    • United States v. Microsoft Corp. 2001: the U.S. government accused Microsoft of illegally maintaining its monopoly position in the PC market primarily through the legal and technical restrictions it put on the abilities of PC manufacturers (OEMs) and users to uninstall Internet Explorer …

    • Safari/macOS:  possible to delete the Safari application, Safari is in fact merely a front-end for Apple's open source WebKit framework, which is heavily integrated into the operating system and cannot be removed

    !19

  • 应⽤用程序:Shell/终端

    • Linux/Unix 命令⾏行行⽤用户界⾯面

    !20

    cd ls ls > junk ls >> junk ls | wc -l ls | wc -l > junk sleep 10& cat fg cat < foo > foo cat < foo >> foo cp ../user.txt . ssh [email protected] -p 21317 scp -P 21317 [email protected]:~/users.txt ~ wget http://cs.nju.edu.cn/x.mp4

    http://cs.nju.edu.cn/x.mp4

  • 应⽤用程序:Shell/终端

    • 管道pipe

    • process1 | process2 | process3

    • IO重定向

    • prog A > fout

    • prog A < fin

    !21

  • 应⽤用程序:Shell/终端

    • man: manual, documentation for Linux/Unix systems

    • computer programs (including library and system calls), device files, file format and conventions…

    • 脚本语⾔言

    !22

    #!/bin/bash for i in $( cat users.txt ); do useradd -m -p $i -s /bin/bash $i echo "user $i added successfully!" done

  • Linux系统层级结构

    !23

    ⽤用户态

    标准函数库

    系统调⽤用(POSIX标准)接⼝口

    模块 内核 (进程管理理、存储管理理、⽂文件管理理、设备管理理、⽹网络管理理)

    设备 驱动

    计算机硬件

    核⼼心态单内核模块 接⼝口

    驱动 接⼝口

    应⽤用程序

  • 系统调⽤用与函数库

    • 系统调⽤用

    • Linux内核的对外接⼝口

    • ⽤用户程序和内核之间唯⼀一的接⼝口

    • 函数库

    • 依赖于系统调⽤用

    • 标准函数库建⽴立在系统调⽤用的上层,提供的功能⽐比系统调⽤用强,使⽤用也⽐比较⽅方便便

    • 静态库(.a⽂文件)

    • 动态库/共享库 (.so⽂文件) !24

  • 系统调⽤用(syscall)

    • 系统调⽤用:获得OS服务的主要⽅方式

    • Linux 系统有300+个系统调⽤用

    • /bin/ls 使⽤用了了什什么系统调⽤用? syscall追踪⼯工具:

    • Linux: strace /bin/ls

    • OSX: sudo dtruss /bin/ls!25

  • 系统调⽤用举例例

    • pid_t getpid(void);

    • get process id

    • pid_t getppid(void);

    • get parent process id

    • char *getwd(char *buf);

    • get current working directory

    !26

  • 系统调⽤用举例例

    • 创建进程 pid_t fork(void);

    • 执⾏行行⼀一次,返回两次

    • ⽗父进程:返回⼦子进程PID;⼦子进程:返回0;

    !27

    int main(int argc, char *argv[]) { printf("Greetings from process %d! (parent %d)\n", getpid(),getppid()); fork(); printf("Bye-bye from process %d! (parent %d)\n”, getpid(),getppid()); return 0; }

    Greetings from process 31384! (parent 27623)Bye-bye from parent process 31384! (parent 27623)Bye-bye from child process 31385! (parent 31384)

  • 系统调⽤用举例例

    • 等待进程结束 pid_t waitpid(pid_t pid, int *status, int options);

    !28

    int main(int argc, char *argv[]) { pid_t pid = fork();

    if (pid == -1) { perror("fork failed"); exit(-1); } else if (pid == 0) { printf("Hello from the child process!\n"); exit(0); } else { int status; waitpid(pid, &status, 0); } return 0; }

  • 系统调⽤用举例例

    • int execvp(char const *file, char const *argv[]);

    !29

    int main( void ) { char *argv[3] = {"Command-line", ".", NULL};

    int pid = fork();

    if ( pid == 0 ) { execvp( "find", argv ); }

    /* Put the parent to sleep for 2 seconds—let the child finished executing */ wait( 2 );

    printf( "Finished executing the parent process\n” );

    return 0; }

  • 系统调⽤用举例例

    • int execlp(const char *file, const char *arg, ...);

    !30

    int main(void){ char buf[MAXLINE]; /* from apue.h */ pid_t pid; int status; printf("%% "); /* print prompt (printf requires %% to print %) */ while (fgets(buf, MAXLINE, stdin) != NULL) { if (buf[strlen(buf) - 1] == ’\n’) buf[strlen(buf) - 1] = 0; /* replace newline with null */ if ((pid = fork()) < 0) { err_sys("fork error"); } else if (pid == 0) { /* child */ execlp(buf, buf, (char *)0); err_ret("couldn’t execute: %s", buf); exit(127); }

    /* parent */ if ((pid = waitpid(pid, &status, 0)) < 0) err_sys("waitpid error"); printf("%% "); }

    exit(0);

    }

  • 系统调⽤用举例例

    • 创建管道 int pipe(int fd[2]);

    • fd[0]可读,fd[1]可写

    !31

    int main(int argc, char *argv[]) { int fds[2]; pipe(fds);

    pid_t pid = fork(); if (pid == 0) { char buffer[6]; read(fds[0], buffer, 6); printf("%s\n", buffer); return 0; }

    write(fds[1], "hello", 6); return 0; }

  • Linux系统层级结构

    !32

    ⽤用户态

    标准函数库

    系统调⽤用(POSIX标准)接⼝口

    模块 内核 (进程管理理、存储管理理、⽂文件管理理、设备管理理、⽹网络管理理)

    设备 驱动

    计算机硬件

    核⼼心态单内核模块 接⼝口

    驱动 接⼝口

    应⽤用程序

  • 划分⽤用户态/内核态的必要性

    • 不不区分的缺陷(Microsoft DOS)

    • ⽤用户直接修改操作系统数据

    • ⽤用户直接调⽤用操作系统内部函数

    • ⽤用户直接操作外设

    • ⽤用户任意读/写物理理内存

    • 区分意义:保护内核

    • 禁⽌止⽤用户程序和底层硬件直接打交道

    • 如果⽤用户程序往硬件控制寄存器器写⼊入不不恰当的值,可能导致硬件⽆无法正常⼯工作

    • 禁⽌止⽤用户程序访问任意物理理内存,否则可能会破坏其他程序的正常执⾏行行

    • 如果对核⼼心内核所在的地址空间写⼊入数据,会导致系统崩溃 !33

  • CPU对⽤用户态/内核态划分的⽀支持

    • 硬件⽀支持:现代CPU都提供不不同指令执⾏行行级别

    • 在⾼高执⾏行行级别状态下,代码可以执⾏行行特权指令,访问任意的物理理地址,此时CPU执⾏行行级别就对应着内核态

    • 在低级别执⾏行行状态下,代码的掌控范围会受到限制,只能在对应级别允许的范围内活动

    • 举例例

    • intel x86 CPU有四种不不同执⾏行行级别:0 ~ 3

    • Linux只使⽤用0级和3级,分别表示内核态和⽤用户态 !34

  • ⽤用户态/核⼼心态的区分⽅方法

    • ps命令,⽅方括号内的都是内核线程

    !35

  • Linux的内核特点

    • Linux是单内核、多模块系统

    • Linux内核运⾏行行在单独的内核地址空间

    • 所有操作系统功能作为⼀一个模块实现在内核中

    • 模块均运⾏行行在内核态,直接调⽤用函数,⽆无需消息传递

    • 模块化设计、抢占式 (Linux 2.6内核级抢占,Linux 2.4⽤用户级抢占)、⽀支持内核线程及动态装载内核模块的能⼒力力

    • 与Unix主要区别

    • Unix也是单内核系统,但Linux汲取了了微内核设计思想(基于模块定制内核)

    • Unix也是单内核系统

    • Windows NT和Mach是微内核系统

    • 只提供基础功能,其他功能通过服务实现

    • 微内核被划分为多个独⽴立过程,每个过程称为服务器器 !36

  • Linux单内核结构

    !37

    ⽤用户态

    标准函数库

    系统调⽤用(POSIX标准)接⼝口

    模块 内核 (进程管理理、存储管理理、⽂文件管理理、设备管理理、⽹网络管理理)

    设备 驱动

    计算机硬件

    核⼼心态单内核模块 接⼝口

    驱动 接⼝口

    应⽤用程序

  • Linux内核overview

    !38

    • 进程管理理• 进程是现代操作系统中⾮非常重要的概念,包括上下⽂文切换(Context Switch)以及进程调度(Scheduling)

    • 每个进程运⾏行行时都感觉完全占有了了全部的硬件资源,但是进程不不会⻓长时间占有硬件资源

    • 操作系统利利⽤用进程调度器器让多个进程并发执⾏行行• Linux内核并没有严格区分进程和线程,⽽而常⽤用task_struct数据结构来描述

    • Linux内核的调度器器的发展经历了了好⼏几代,从很早的O(n)调度器器到Linux 2.6内核中的O(1)调度器器,再到现在的CFS公平算法调度器器

    • ⽬目前⽐比较热⻔门的讨论是关于性能和功耗的优化,⽐比如ARM阵营提出了了⼤大⼩小核体系结构,⾄至今在Linux内核实现中还没有体现,因此类似EAS(Energy Awareness Scheduling)这样的调度算法是⼀一个研究热点

  • Linux内核overview

    !39

    ➢ 内存管理理✓ 内存管理理模块是Linux内核中最复杂的模块,它涉及物理理内存的管理理和虚拟内存的管理理

    ✓ 虚拟内存的模块有反向映射、⻚页⾯面回收、KSM、mmap映射、缺⻚页中断、共享内存、进程虚拟地址空间管理理等

    ✓ 物理理内存管理理包括:物理理内存初始化,⻚页⾯面分配器器(Page Allocator),伙伴系统,slab分配器器等等。

  • Linux内核overview

    !40

    ➢ 中断管理理✓ 中断管理理包含处理理器器的异常(Exception)处理理和中断(Interrupt)处理理

    ✓ 异常通常是指如果处理理器器在执⾏行行指令时检测到⼀一个反常条件,处理理器器就必须暂停下来处理理这些特殊的情况,如常⻅见的缺⻚页异常(Page Fault)

    ✓ 中断异常⼀一般是指外设通过中断信号线路路来请求处理理器器,处理理器器会暂停当前正在做的事情来处理理外设的请求

    ✓ Linux内核在中断管理理⽅方⾯面有上半部和下半部之分。上半部是在关闭中断的情况下执⾏行行的,因此处理理时间要求短、平、快;⽽而下半部是在开启中断的情况下执⾏行行的,很多对执⾏行行时间要求不不⾼高的操作可以放到下半部来执⾏行行。

    ✓ Linux内核为下半部提供了了多种机制,如软中断、Tasklet和⼯工作队列列等。

  • Linux内核overview

    !41

    • 设备管理理• 设备管理理对于任何的⼀一个操作系统来说都是重中之重。Linux内核之所以这么流⾏行行,就是因为它⽀支持的外设是所有开源操作系统中最多的。当很多⼤大公司有新的芯⽚片诞⽣生时,第⼀一个要⽀支持的操作系统是Linux,也就是尽可能地在Linux内核社区⾥里里推送。

    • Linux内核的设备管理理是⼀一个很⼴广泛的概念,包含的内容很多,如ACPI、设备树、设备模型kobject、设备总线(如PCI总线)、字符设备驱动、块设备驱动、⽹网络设备驱动等。

  • Linux内核overview

    !42

    ➢ ⽂文件系统✓ 为了了⽀支持各种各样的⽂文件系统,Linux抽象出了了⼀一个称为虚拟⽂文件系统(VFS)层的软件层,这样Linux内核就可以很⽅方便便地集成多种⽂文件系统。

    ✓ ⼀一个优秀的操作系统必须包含优秀的⽂文件系统,但是⽂文件系统有不不同的应⽤用场合,如基于闪存的⽂文件系统F2FS、基于磁盘存储的⽂文件系统ext4和XFS等

  • Linux内核源码的获取

    • 下载位置

    • http://www.kernel.org: 以GNU zip和bzip2形式发布

    • https://github.com/torvalds/linux

    • 安装位置

    • ⼀一般安装在/usr/src/linux,不不要将该源码树⽤用于开发

    • 在编译⾃自⼰己编写的C库所⽤用的内核版本要链接到该树

    • 不不要以root身份对内核进⾏行行修改,应先建⽴立⾃自⼰己的主⽬目录,仅以root身份安装新内核

    • 安装新内核应该保持/usr/src/linux原封不不动

    • 补丁

    • patch -p1< ../patch-x.y.z

    !43

    http://www.kernel.orghttps://github.com/torvalds/linux

  • Linux内核版本

    • Linux内核版本号由Linux开发⼩小组(Linus Torvalds总协调)确定

    • Linux内核采⽤用双树系统

    • ⼀一棵是稳定树,主要⽤用于发⾏行行

    • 另⼀一棵是⾮非稳定树(开发树) ⽤用于产品开发和改进

    • Linux内核版本号由3/4位数字组成 !44

  • Android版本

    !45

    Code name Version Linux kernel version[1] Initial release date API level

    (No codename)[2] 1.0 ? September 23, 2008 1

    Petit Four[2] 1.1 2.6.X February 9, 2009 2

    Cupcake 1.5 2.6.27 April 27, 2009 3

    Donut[3] 1.6 2.6.29 September 15, 2009 4

    Eclair[4] 2.0 – 2.1 2.6.29 October 26, 2009 5 – 7

    Froyo[5] 2.2 – 2.2.3 2.6.32 May 20, 2010 8

    Gingerbread[6] 2.3 – 2.3.7 2.6.35 December 6, 2010 9 – 10

    Honeycomb[7] 3.0 – 3.2.6 2.6.36 February 22, 2011 11 – 13

    Ice Cream Sandwich[8] 4.0 – 4.0.4 3.0.1 October 18, 2011 14 – 15

    Jelly Bean[9] 4.1 – 4.3.1 3.0.31 to 3.4.39 July 9, 2012 16 – 18

    KitKat[10] 4.4 – 4.4.4 3.10 October 31, 2013 19 – 20

    Lollipop[11] 5.0 – 5.1.1 3.16.1 November 12, 2014 21 – 22[12]

    Marshmallow[13] 6.0 – 6.0.1 3.18.10 October 5, 2015 23

    Nougat[14] 7.0 – 7.1.2 4.4.1 August 22, 2016 24 – 25

    Oreo[15] 8.0 – 8.1 4.10 August 21, 2017 26 – 27

    Pie[16] 9.0 4.4.107, 4.9.84, and 4.14.42 August 6, 2018 28

    https://en.wikipedia.org/wiki/Linux_kernelhttps://en.wikipedia.org/wiki/Android_version_history#cite_note-1https://en.wikipedia.org/wiki/Android_1.0https://en.wikipedia.org/wiki/Android_version_history#cite_note-unofficial_and_official_codenames-2https://en.wikipedia.org/wiki/Android_1.1https://en.wikipedia.org/wiki/Android_version_history#cite_note-unofficial_and_official_codenames-2https://en.wikipedia.org/wiki/Android_version_history#Android_1.5_Cupcake_(API_3)https://en.wikipedia.org/wiki/Android_version_history#Android_1.6_Donut_(API_4)https://en.wikipedia.org/wiki/Android_version_history#cite_note-3https://en.wikipedia.org/wiki/Android_version_history#Android_2.0_Eclair_(API_5)https://en.wikipedia.org/wiki/Android_version_history#cite_note-4https://en.wikipedia.org/wiki/Android_version_history#Android_2.2_Froyo_(API_8)https://en.wikipedia.org/wiki/Android_version_history#cite_note-5https://en.wikipedia.org/wiki/Android_version_history#Android_2.3_Gingerbread_(API_9)https://en.wikipedia.org/wiki/Android_version_history#cite_note-6https://en.wikipedia.org/wiki/Android_version_history#Android_3.0_Honeycomb_(API_11)https://en.wikipedia.org/wiki/Android_version_history#cite_note-7https://en.wikipedia.org/wiki/Android_version_history#Android_4.0_Ice_Cream_Sandwich_(API_14)https://en.wikipedia.org/wiki/Android_version_history#cite_note-8https://en.wikipedia.org/wiki/Android_version_history#Android_4.1_Jelly_Bean_(API_16)https://en.wikipedia.org/wiki/Android_version_history#cite_note-9https://en.wikipedia.org/wiki/Android_version_history#Android_4.4_KitKat_(API_19)https://en.wikipedia.org/wiki/Android_version_history#cite_note-10https://en.wikipedia.org/wiki/Android_version_history#Android_5.0_Lollipop_(API_21)https://en.wikipedia.org/wiki/Android_version_history#cite_note-11https://en.wikipedia.org/wiki/Android_version_history#cite_note-12https://en.wikipedia.org/wiki/Android_version_history#Android_6.0_Marshmallow_(API_23)https://en.wikipedia.org/wiki/Android_version_history#cite_note-13https://en.wikipedia.org/wiki/Android_version_history#Android_7.0_Nougat_(API_24)https://en.wikipedia.org/wiki/Android_version_history#cite_note-14https://en.wikipedia.org/wiki/Android_version_history#Android_8.0_Oreo_(API_26)https://en.wikipedia.org/wiki/Android_version_history#cite_note-15https://en.wikipedia.org/wiki/Android_version_history#Android_9.0_Pie_(API_28)https://en.wikipedia.org/wiki/Android_version_history#cite_note-16

  • Linux内核开发模式

    !46

    ➢ Linux内核的开发模式完全由社区来主导。✓ Linus是Linux内核的最⼤大的维护者和导演✓ 每个⼦子模块都有各⾃自维护者✓ 在邮件列列表为讨论主战场,⽐比如LKML

    ⼤大约60-70天⼀一个新内核版本发布

  • Linux内核⾏行行数的变化

    !47

    ➢ Linux内核从1991年年的1w⾏行行代码发展到超过200w⾏行行代码

  • Linux内核开发参与者

    !48

    ➢ Linux内核参与的开发者分布全球,数以千计的开发者,⼏几个顶尖的IT公司

  • 主要的内核开发贡献者

    !49

  • 主要的参与公司

    !50

  • 开源协议

    !51

    • GPL许可证• 1989年年发布GPL v1

    • 发布可执⾏行行的⼆二进制代码,就必须同时发布源代码• 1991年年发布GPL v2

    • 当采⽤用了了部分的GPL协议相关的软件,那么这个软件整体就必须采⽤用GPL协议

    • 2005年年发布GPL v3• 任何公司或者实体以GPL v3协议发布软件,那么它将永远以GPL v3协议发布,并且原专利利拥有者在任何时候不不具备收取专利利费的权⼒力力

    • 专利利报复条款,禁⽌止发布软件的公司或者实体向被许可⼈人发起专利利诉讼。

    • BSD许可证• Apache许可证

  • 开源协议

    !52

  • Linux内核源码结构

    • 进程调度程序:负责控制进程访问CPU kernel/

    • 进程间通信:为进程之间的通信提供实现机制 ipc/

    • 内核管理理程序:⽀支持虚拟内存及多进程安全共享主存系统 mm/

    • 虚拟⽂文件系统:抽象异构硬件设备细节,提供公共⽂文件接⼝口 fs/

    • ⽹网络接⼝口:提供对多种组⽹网标准和⽹网络硬件的访问 /net!53

  • Linux内核源码结构

    • 25 million lines of code

    • Every file has a maintainer

    SCHEDULER

    M: Ingo Molnar

    M: Peter Zijlstra

    L: [email protected]

    T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git sched/core

    S: Maintained

    F: kernel/sched/

    F: include/linux/sched.h

    F: include/uapi/linux/sched.h

    F: include/linux/wait.h

    !54

  • Linux内核的编译、安装

    • 配置内核

    • 获取默认.config⽂文件:cp /boot/config-`uname -r` .config

    • ⽣生成配置⽂文件:make config

    • 编译

    • 编译内核:make -jn

    • 编译模块:make modules

    • 安装内核

    • 安装模块:make modules_install

    • ⽣生成模块依赖关系:depmod –a

    • 安装内核:make install

    !55

  • /vmlinux: kernel in hard disk

    • statically linked executable file that contains the Linux kernel

    • /unix: kernel file for unix

    • vmlinux or vmlinuz

    • vm for virtual memory

    • z for compressed (gzipped)

    • 位置

    • /boot/vmlinux-x.y.z

    • /vmlinux-x.y.z !56

  • Linux 内核编程

    • 内核编程与系统编程有很⼤大不不同

    • The kernel has access to neither the C library nor the standard C headers.

    • no printf(), string function…

    • printk(“Hello world! A string '%s' and an integer '%d'\n", str, i);

    • printk(KERN_ERR "this is an error!\n");

    !57

  • Linux 内核编程

    • 使⽤用GNU C,不不是ANSI C

    • if( unlikely(error) ){ /*…..*/ }

    • if( likely(error) ){ /*…..*/ }

    • 尽量量避免使⽤用浮点数

    • 栈⼤大⼩小固定:4KB/8KB

    • 抢占式内核,需考虑同步并发!58

  • 总结

    • Linux发展历史

    • Linux层级结构:应⽤用程序,标准库,系统调⽤用,内核

    • Linux内核开发:开发模式,⼯工具

    !59

  • 实验题 Lab1

    ⼀一. 简单Shell

    • 内置命令:cd,exit,pwd

    • 管道:⾄至少⽀支持两个进程

    • IO重定向:⾄至少1个进程

    • 后台运⾏行行

    ⼆二. 内核编译

    • 在ubuntu环境下,下载内核源码,编译并⽣生成⾃自定义的内核

    • 安装编译好的⾃自定义内核

    • 参考https://kernelnewbies.org/KernelBuild !60

    https://kernelnewbies.org/KernelBuild

  • 实验题 Lab1

    • Linux系统

    • ⾃自⼰己机器器/⼄乙126机房

    • 虚拟机:Windows VMware Workstation, Mac VMware Fusion

    • ⿎鼓励交流

    • 不不能抄袭

    • 第⼀一次警告并扣除当次作业分数

    • 再犯。。!61

  • 谢谢⼤大家!

    下次课:基础实验楼⼄乙126

    !62