系统调用:sys_exit(),SYS_exit和exit()之间的区别 [英] System calls : difference between sys_exit(), SYS_exit and exit()

查看:685
本文介绍了系统调用:sys_exit(),SYS_exit和exit()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SYS_exit,sys_exit()和exit()有什么区别?

What is the difference between SYS_exit, sys_exit() and exit()?

我的理解:

  • Linux内核提供系统调用,这些调用在man 2 syscalls中列出.
  • glibc提供的那些系统调用的包装函数具有与该系统调用最相似的名称.
  • The linux kernel provides system calls, which are listed in man 2 syscalls.
  • There are wrapper functions of those syscalls provided by glibc which have mostly similar names as the syscalls.

我的问题:在man 2 syscalls中,例如,没有提及SYS_exit和sys_exit().他们是什么?

My question : In man 2 syscalls, there is no mention of SYS_exit and sys_exit(), for example. What are they?

注意:此处的系统调用exit仅是一个示例.我的问题确实是:什么是SYS_xxx和sys_xxx()?

Note : The syscall exit here is only an example. My question really is : What are SYS_xxx and sys_xxx()?

推荐答案

我将像您的示例一样使用exit(),尽管这适用于所有系统调用.

I'll use exit() as in your example although this applies to all system calls.

sys_exit()形式的函数是内核例程的实际入口点,该例程实现您认为是exit()的函数.这些符号甚至不适用于用户模式程序员.也就是说,除非您正在破解内核,否则无法链接到这些函数,因为它们的符号在内核外部不可用.如果我写的libmsw.a具有类似文件范围的功能

The functions of the form sys_exit() are the actual entry points to the kernel routine that implements the function you think of as exit(). These symbols are not even available to user-mode programmers. That is, unless you are hacking the kernel, you cannot link to these functions because their symbols are not available outside the kernel. If I wrote libmsw.a which had a file scope function like

static int msw_func() {}

在其中定义

时,尝试链接到它不会成功,因为它没有在libmsw符号表中导出.即:

defined in it, you would have no success trying to link to it because it is not exported in the libmsw symbol table; that is:

cc your_program.c libmsw.a

会产生类似以下错误:

ld: cannot resolve symbol msw_func

因为它没有被导出;内核中包含的sys_exit()也是如此.

because it isn't exported; the same applies for sys_exit() as contained in the kernel.

为了使用户程序能够进入内核例程,需要使用syscall(2)接口来实现从用户模式到内核模式的切换.当该模式切换(有时称为陷阱)发生时,将使用一个小整数在内核表中查找适当的内核例程,该例程将整数映射到内核函数.表中的条目的格式为

In order for a user program to get to kernel routines, the syscall(2) interface needs to be used to effect a switch from user-mode to kernel mode. When that mode-switch (somtimes called a trap) occurs a small integer is used to look up the proper kernel routine in a kernel table that maps integers to kernel functions. An entry in the table has the form

{SYS_exit, sys_exit},

其中SYS_exit是预处理器宏,

Where SYS_exit is an preprocessor macro which is

#define SYS_exit (1)

,因为您没有更改它的理由,所以从您出生之前一直为1.它也恰好是系统调用表中的第一项,它使查找简单的数组索引成为可能.

and has been 1 since before you were born because there hasn't been reason to change it. It also happens to be the first entry in the table of system calls which makes look up a simple array index.

正如您在问题中指出的那样,常规用户模式程序访问sys_exit的正确方法是通过glibc(或类似的核心库)中的精简包装.您需要弄乱SYS_exit或sys_exit的唯一原因是,如果您正在编写内核代码.

As you note in your question, the proper way for a regular user-mode program to access sys_exit is through the thin wrapper in glibc (or similar core library). The only reason you'd ever need to mess with SYS_exit or sys_exit is if you were writing kernel code.

这篇关于系统调用:sys_exit(),SYS_exit和exit()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆