Linux下系统调用的组成与实现
主要包括三个部分:(1)唯一的系统调用号(System Call Number);(2)系统调用表中相应的表项,即具体的函数地址;(3)对应的具体函数,即系统调用函数体。
以getpid()POSIX接口举例如下:
(1)系统调用号在文件 include/asm-x86/unistd_32.h中,表现如下:
/*
* This file contains the system call numbers.
*/
#define __NR_restart_syscall 0
#define __NR_exit 1
#define __NR_fork 2
#define __NR_read 3
#define __NR_write 4
#define __NR_open 5
#define __NR_close 6
#define __NR_waitpid 7
#define __NR_creat 8
#define __NR_link 9
#define __NR_unlink 10
#define __NR_execve 11
#define __NR_chdir 12
#define __NR_time 13
#define __NR_mknod 14
#define __NR_chmod 15
#define __NR_lchown 16
#define __NR_break 17
#define __NR_oldstat 18
#define __NR_lseek 19
#define __NR_getpid 20
#define __NR_mount 21
……
(2)系统调用表在系统中的位置是: arch/avr32/kernel/syscall_table.S
表现如下:
……
sys_call_table:
.long sys_restart_syscall
.long sys_exit
.long __sys_fork
.long sys_read
.long sys_write
.long sys_open /* 5 */
.long sys_close
.long sys_umask
.long sys_creat
.long sys_link
.long sys_unlink /* 10 */
.long __sys_execve
.long sys_chdir
.long sys_time
.long sys_mknod
.long sys_chmod /* 15 */
.long sys_chown
.long sys_lchown
.long sys_lseek
.long sys_llseek
.long sys_getpid /* 20 */
.long sys_mount
.long sys_umount
……
(3)具体的函数实现代码在系统中的位置是:kernel/timer.c
/**
* sys_getpid - return the thread group id of the current process
*
* Note, despite the name, this returns the tgid not the pid. The tgid and
* the pid are identical unless CLONE_THREAD was specified on clone() in
* which case the tgid is the same in all threads of the same group.
*
* This is SMP safe as current->tgid does not change.
*/
asmlinkage long sys_getpid(void)
{
return task_tgid_vnr(current);
}
那么,我们在系统中如何使用这个函数呢流程如下:
getpid(void) -------> int 0x80进入内核(同时从寄存器eax获取超级调用号) ---------> system_call , call *sys_ call_table(,%eax,
4)(这里的动作是将PC值改为%eax * 4 + sys_call_table) -----> sys_getpid(void) -------> system_exit , resume_userspace -----> 给用户空间返回结果
Linux下系统调用的组成与实现的更多相关文章
- 以C语言为例完成简单的网络聊天程序以及关于socket在Linux下系统调用的分析
套接字是网络编程中的一种通信机制,是支持TCP/IP的网络通信的基本操作单元,可以看做是不同主机之间的进程进行双向通信的端点,简单的说就是通信的两方的一种约定,用套接字中的相关函数来完成通信过程. 端 ...
- Linux 下系统调用的三种方法
系统调用(System Call)是操作系统为在用户态运行的进程与硬件设备(如CPU.磁盘.打印机等)进行交互提供的一组接口.当用户进程需要发生系统调用时,CPU 通过软中断切换到内核态开始执行内核系 ...
- linux下系统调用劫持ioctl
实验环境:linux 2.6.32 64位系统 采用lkm(动态加载内核模块)方式劫持ioctl系统调用,系统调用过程如图所示(以open为例子) 实验代码:(头文件有不需要的,但是懒得改了,在系 ...
- 秒杀linux下系统调用fork()面试题(转)
https://blog.csdn.net/chdhust/article/details/10579001 https://www.cnblogs.com/clover-toeic/p/375443 ...
- 浅析基于ARM的Linux下的系统调用的实现
在Linux下系统调用是用软中断实现的,下面以一个简单的open例子简要分析一下应用层的open是如何调用到内核中的sys_open的. t8.c 1: #include <stdio.h> ...
- Linux下的系统调用
张雨梅 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10000 1.linux的的用户态与内核 ...
- linux下64位汇编的系统调用(5)
看到这里大家都基本知道了如何进行linux下的汇编系统调用:不过有些童鞋可能会问:那些C库中函数里为我们解决的额外汇编代码你是怎么知道的? 好吧,我承认:我是通过逆向知道的,这貌似有点犯规的嫌疑- 比 ...
- linux下64位汇编的系统调用(3)
背景知识基本交代清楚了,下面我们实际写一个小例子看一下.代码的功能很简单,显示一行文本,然后退出.我们使用了syscall中的write和exit调用,查一下前面的调用号和参数,我们初步总结如下: w ...
- linux下64位汇编的系统调用(2)
知道了syscall调用号之后还不算完,还要搞清楚2件事: 1 每种调用号需要传递哪些参数: 2 调用如何传递参数以及结果如何返回: 第一个问题的答案是: 在linux系统中某个程序执行时进行的系统调 ...
随机推荐
- [Bayes] MCMC (Markov Chain Monte Carlo)
不错的文章:LDA-math-MCMC 和 Gibbs Sampling 可作为精进MCMC抽样方法的学习材料. 简单概率分布的模拟 Box-Muller变换原理详解 本质上来说,计算机只能生产符合均 ...
- shell中跳出循环语句break和continue
在使用while或for循环语句过程中,也许碰到某个特殊条件,我们需要跳过当次循环或整个循环,这是就需要借助break和continue. break表示跳出本层循环,break n表示跳出循环的层数 ...
- [LeetCode] 773. Sliding Puzzle 滑动拼图
On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...
- [LeetCode] 844. Backspace String Compare 退格字符串比较
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- Linux安全学习
安全游戏说明 游戏形式帮助初学者学习和实践网络安全,主要是Linux shell命令的练习. https://overthewire.org/wargames/ 二进制 灰帽黑客进阶秘籍之一--lin ...
- mongodb 切换wiredtiger
1.由于最近用到mongodb但查询时前十分左右,用压力测试不太稳定,所以换成第三方引擎试试,但效果还是一样. 具说第三方引擎比较给力,但在使用没有发现.现将mongodb切换wiredtiger引擎 ...
- 深入理解-CLI与PHP-FPM
原文地址:https://blog.csdn.net/lzx_victory/article/details/85917161 PHP-FPM模式相对于CLI比较复杂,因为PHP-FPM为常驻进程不断 ...
- java.lang.IllegalArgumentException: TLSv1.2
通过HTTPClient4.5,基于JDK1.6 new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hel ...
- android基础---->SharedPreferences的使用
SharedPreferences 还支持多种不同的数据类型存储,如果存储的数据类型是整型,那么读取出来的数据也是整型的,存储的数据是一个字符串,读取出来的数据仍然是字符串.这样你应该就能明显地感觉到 ...
- vscode添加vue文件模板
文件->首选项->用户代码片段->输入vue,打开vue.json 添加模板json "Print to console": { "prefix&quo ...