Linux:操作系统分析 Chapter6系统调用 陈香兰(xlanchen@ustc.edu.cn) 计算机应用教研室©计算机学院 嵌入式系统实验室●苏州研究院 中国科学技术大学 Fa112014 October 24.2014 4口4四1是14P刀00 陈香兰(x1 anchenoustc,edu,cn)(计算丸应Linux操作系统分新Chapter6系统调用 0 ctober24,20141731
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Linux操作系统分析 Chapter 6 系统调用 陈香兰(xlanchen@ustc.edu.cn) 计算机应用教研室@计算机学院 嵌入式系统实验室@苏州研究院 中国科学技术大学 Fall 2014 October 24, 2014 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 6 系统调用 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) October 24, 2014 1 / 31
Outline 系统调用和API ② 系统调用机制的实现 ●系统调用分派表 ·系统调用处理函数system cal1 ●系统调用的参数传递 ●系统调用参数的验证 ●如何访问进程的地址空间 ●系统调用的返回 作业和project 1口t4四1是42刀00 陈香兰(x1 anchenoustc,edu,cn)(计算丸应【inx操作系统分新Chapter6系统玛用 0 ctober24.20142/31
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Outline 1. 系统调用和API .2 系统调用机制的实现 系统调用分派表 系统调用处理函数system_call 系统调用的参数传递 系统调用参数的验证 如何访问进程的地址空间 系统调用的返回 .3 作业和project 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 6 系统调用 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) October 24, 2014 2 / 31
系统调用的意义 。操作系统为用户态进程与硬件设备进行交互提供了 一组接口一一系统调用 。把用户从底层的硬件编程中解放出来 。极大的提高了系统的安全性 。使用户程序具有可移植性 ●在Linux用户态,通过int Ox80陷入内核以执行系统调用。 ·为避免程序员使用低级的汇编语言编程, 通常使用C库封装后的API接口。 1口t4四1是42刀00 陈香兰(x1 anchenoustc,edu,cn)(计算丸应Linx操作系统分新Chapter6系统调用 0 ctober24.20144/31
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 系统调用的意义 操作系统为用户态进程与硬件设备进行交互提供了 一组接口——系统调用 把用户从底层的硬件编程中解放出来 极大的提高了系统的安全性 使用户程序具有可移植性 在Linux用户态,通过int 0x80陷入内核以执行系统调用。 为避免程序员使用低级的汇编语言编程, 通常使用C库封装后的API接口。 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 6 系统调用 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) October 24, 2014 4 / 31
API和系统调用 ●应用编程接口(application program interface,API) 和系统调用是不同的 ·API只是一个函数定义 。系统调用通过软中断向内核发出一个明确的请求 ●Libc库定义的一些API引用了封装例程(wrapper routine,唯一目的就是发布系统调用) 。一般每个系统调用对应一个封装例程 。库再用这些封装例程定义出给用户的API ·不是每个API都对应一个特定的系统调用· 。API可能直接提供用户态的服务,如一些数学函数 ●一个单独的API可能调用几个系统调用 ·不同的API可能调用了同一个系统调用 4口4四1是4P刀00 陈香兰(xlanchenoustc,edu,cn)(计算丸应【inux操件系统分折Chapter书系统码用 0 ctober24,20145/31
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . API和系统调用 应用编程接口(application program interface, API) 和系统调用是不同的 API只是一个函数定义 系统调用通过软中断向内核发出一个明确的请求 Libc库定义的一些API引用了封装例程(wrapper routine,唯一目的就是发布系统调用) 一般每个系统调用对应一个封装例程 库再用这些封装例程定义出给用户的API 不是每个API都对应一个特定的系统调用。 API可能直接提供用户态的服务,如一些数学函数 一个单独的API可能调用几个系统调用 不同的API可能调用了同一个系统调用 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 6 系统调用 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) October 24, 2014 5 / 31
API和系统调用 ●API的返回值 ·大部分封装例程返回一个整数,其值的含义依赖于相应的系统调用 ●-1在多数情况下表示内核不能满足进程的请求 。Libc中定义的errno变量包含特定的出错码 以open和creat为例 int open(const char pathname,int flags); int open(const char pathname,int flags,modet mode): int creat(const char "pathname,modet mode); RETURN VALUE open()and creat()return the new file descriptor,or-1 if an error occurred (in which case,errno is set appropriately). 4口4四1是4P刀00 陈香兰(x1 anchenoustc,edu,cn)(计算丸应【inux操作系统分新Chapter6系统调用 0 ctober24,20145/31
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . API和系统调用 API的返回值 大部分封装例程返回一个整数,其值的含义依赖于相应的系统调用 -1在多数情况下表示内核不能满足进程的请求 Libc中定义的errno变量包含特定的出错码 . 以open和creat为例 . . int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode\_t mode); int creat(const char *pathname, mode\_t mode); RETURN VALUE open() and creat() return the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately). 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 6 系统调用 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) October 24, 2014 5 / 31