1.1 System call(linux)系统函数调用过程/如何查看linux内核版本
System call
A system call is a controlled entry point into the kernel, allowing a process to
request that the kernel perform some action on the process’s behalf.
系统调用是内核控制的的一个接入点,让进程请求内核代替它进行一系列行为的操作。
本质上讲系统调用就是内核定义的一个数字,不同的系统调用方法用不同的数字标识。
系统调用的过程:
1.调用在C library里包装好的方法。
2.函数参数通过stack传入,参数->stack->copy->registers寄存器。
3.将标识改系统调用方法的数字传入CPU register。
4.执行具体的指令。用户模式切换到内核模式。
The wrapper function executes a trap machine instruction (int 0x80)
0x80是CPU定义的指令,每个数字表示CPU做不同的操作。
trap machine相当于一个开关,要CPU做什么样的操作就按相应的开关,而不同开关用不同的数字标识。
5.对CPU指令相应结果处理。
On Linux, system call service routines follow a convention of returning a
nonnegative value to indicate success. In case of an error, the routine returns a
negative number, which is the negated value of one of the errno constants.
系统调用成功:返回一个正数。
系统调用失败:返回一个负数,在errno中定义中表示的一个错误码。
上图是《The linux programing interface》一书中描述的系统方法调用的过程。
最初在我们个人编写的程序中就是简单一个方法中的调用语句。经过C编译包装就编程了对应到CPU指令的指令集对应的数字,然后内核相应处理执行者将该指令对应的数字抛给CPU,就完成了系统方法的调用。最后如果返回了错误,就返回相应的错误标示。
查看内核版本:uname -srm
查看内核信息:hostnamectl