Linux系统调用与C lib函数 [英] Linux system calls vs C lib functions

查看:73
本文介绍了Linux系统调用与C lib函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这两点有些困惑,所以这是我的问题;

I have bit bit of confusion regarding these two so here are my questions;

Linux手册页项目列出了所有这些功能: https://www.kernel.org/doc/man-pages/

The Linux man-pages project lists all these functions: https://www.kernel.org/doc/man-pages/

recvfrom 为例,该函数既作为Linux系统调用又作为C库函数存在.他们的文档似乎有所不同,但是都可以使用 #include< sys/socket.h> 进行访问.我不明白他们的区别吗?

Looking at recvfrom as an example, this function exists both as a Linux system call as well as a C library function. Their documentation seems different but they are both reachable using #include <sys/socket.h>. I don't understand their difference?

我还认为系统调用是使用十六进制值定义的,可以直接在汇编中实现,它们的列表在这里: https://syscalls.kernelgrok.com/

I also thought systems calls are defined using hex values which can be implemented in assembly directly, their list is here: https://syscalls.kernelgrok.com/

但是我在上面的链接中找不到 recvfrom .在这一点上,我对Linux系统调用与C lib函数之间有些困惑!

However I cannot find recvfrom in the above link. I'm a bit confused between Linux system calls vs C lib functions at this point!

要添加到问题中,很多功能在(3)下,但不在(2)下,即 clean .这是否意味着这些操作是直接由C运行时完成的,而不依赖于系统调用和底层操作系统?

To add to the questions, alot of functions are under (3) but not (2), i.e clean. Does this mean these are done by C runtime directly without relying on system calls and the underlying OS?

推荐答案

首先,请理解C函数和系统调用是 两个完全不同的东西 .

First, understand that that C functions and system calls are two completely different things.

未包装在C库中的系统调用必须通过 syscall 函数进行调用.这样的呼叫的一个例子是 gettid .

System calls not wrapped in the C library must be called through the syscall function. One example of such a call is gettid.

要使用 syscall 创建 gettid 系统调用包装,请执行以下操作:

To create a gettid system call wrapper with syscall, do this:

#define _GNU_SOURCE
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

pid_t gettid(void)
{
    pid_t tid = (pid_t)syscall(SYS_gettid);
    return tid;
}

这是手册页的NOTES部分的摘录,其中明确指出此函数未在C库中定义:

Here's an excerpt from the NOTES section of the man-page, which explicitly states that this function is not defined within the C library:

注意
Glibc不为该系统调用提供包装器.使用syscall(2)调用它.

NOTES
Glibc does not provide a wrapper for this system call; call it using syscall(2).


recvfrom 是围绕系统调用的C库包装.
第(2)节中的所有内容都是系统调用.第(3)节中的所有内容都不是.第(3)节中的所有内容(有一些值得注意的例外,例如 getumask )在C库中都有一个定义.与 gettid 一样,第(2)节中大约一半的内容在C库中都没有定义(或包装)(除了POSIX和其他一些扩展名所具有的功能).代码>.


recvfrom is a C library wrapper around a system call.
Everything in section (2) is a system call. Everything in section (3) is not. Everything in section (3) (with a few notable exceptions, such as getumask) has a definition in the C library. About half of everything in section (2) does not have a definition (or wrapper) within the C library (with the exception of functions mandated by POSIX, and some other extensions, which all do), as with gettid.

在C中调用 recvfrom 时,C库将调用内核以进行系统调用.

When calling recvfrom in C, the C library calls the kernel to do the syscall.

syscall 函数是将系统调用号码放入%eax 寄存器并使用 int $ 0x80 的函数.

The syscall function is the function that puts the system call number in the %eax register and uses int $0x80.

https://syscalls.kernelgrok中看不到 recvfrom 的原因.com/是因为 https://syscalls.kernelgrok.com/非常非常不完整

The reason you don't see recvfrom in https://syscalls.kernelgrok.com/ is because https://syscalls.kernelgrok.com/ is very, very incomplete.

(3)中没有在(2)中看到许多功能的原因是,(3)中的许多功能没有系统调用.它们可能依赖也可能不依赖系统调用,只是没有支持它们的特定名称的系统调用.

The reason there are many functions in (3) that you don't see in (2) is because many functions on (3) don't have a system call. They may or may not rely on system calls, they just don't have a system call with that specific name that backs them.

这篇关于Linux系统调用与C lib函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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