Linux编程使用的C语言标准库和system call

Linux系统使用的C标准库

Linux系统中的C语言标准库通常用的都是GNU C Library,即glibc。

glibc基于ISO C 标准,如C89、C90等。并且进行了一定扩展,比如:支持POSIX和一些其他的系统特有功能。

glibc是开源的,在Linux各个发行版中广泛使用,作为默认的C语言库。

除此之外,对于一些嵌入式系统,还有其他C库,比如musl libc,uClibc等。这些库的设计,满足了轻量化,并专门为嵌入式系统打造。

GNU C Library (glibc) 是一系列文件的集合。由一些头文件、object文件和动态链接库组成,一起为Linux系统提供标准C库的功能。

具体构成如下:

1,Header files:

在/usr/include中,包含函数原型和宏定义,在编译C程序时,引用到glibc时要使用。

2,Shared libraries:

最主要的共享库文件是libc.so.x,这里的“x” 表示版本号。这个文件位于 /lib 或 /lib64 ,这个取决于你的系统架构。

这个动态库为标准C库的函数提供动态链接。

3,Object files:

这些文件,包括 crt1.o、crti.o 和 crtn.o 等,对于连接 C 程序至关重要。它们为初始化 C 程序提供启动代码和其他必要功能。

4,Configuration files and directories:

Glibc 还包括一些配置文件和目录,如 /etc/ld.so.conf 和 /etc/ld.so.conf.d/,它们指定了运行时搜索共享库的目录。

5,Source code:

glibc 的源代码可供检查和修改。源代码通常保存在 Linux 发行版提供的源代码包中,或直接从 GNU 项目中获取。

glibc中使用的系统调用

以使用open函数为例。open() 函数是在 C 标准库(libc)中定义的。不过,需要注意的是,C 标准库中 open() 函数的实现只是内核提供的实际系统调用的一个封装。

libc 提供的 open() 函数通常位于 libc.so 中,而 libc.so 是 C 标准库的共享库文件。当你在 C 程序中调用 open() 时,它会调用相应的系统调用,通过内核打开文件。

因此,虽然你可以在 libc 中找到 open() 的声明和实现,但 open() 实际调用的系统调用是由内核而不是 libc 处理的。

The open() function is defined in the C standard library (libc). However, it's important to note that the implementation of the open() function in the C standard library is just a wrapper around the actual system call provided by the kernel.

The open() function provided by libc typically resides in libc.so, which is the shared library file for the C standard library. When you call open() in your C program, it invokes the corresponding system call to open a file through the kernel.

So, while you'll find the declaration and implementation of open() in libc, the actual system call that open() invokes is handled by the kernel, not by libc.

在 Linux 中,系统调用是进程向操作系统内核提出的请求。它允许用户级进程请求内核提供服务。这些服务包括创建或管理进程、访问硬件设备、管理文件等操作。

系统调用提供了用户空间应用程序与内核之间的接口,允许用户程序以受控方式执行特权操作。系统调用的例子包括打开文件的 open()、从文件读取数据的 read()、向文件写入数据的 write()、创建新进程的 fork()、执行新程序的 exec()等。

每个系统调用都有一个唯一的标识符,通常是一个数字,用来调用它。当进程调用系统调用时,它会从用户模式转换到内核模式,允许内核代表进程执行请求的操作。操作完成后,控制权返回用户空间进程。

In Linux, a system call is a request made by a process to the kernel of the operating system. It allows user-level processes to request services from the kernel. These services include operations such as creating or managing processes, accessing hardware devices, managing files, and more.

System calls provide an interface between user-space applications and the kernel, allowing user programs to perform privileged operations in a controlled manner. Examples of system calls include open() for opening files, read() for reading data from files, write() for writing data to files, fork() for creating a new process, exec() for executing a new program, etc.

Each system call has a unique identifier, typically a number, which is used to invoke it. When a process invokes a system call, it transitions from user mode to kernel mode, allowing the kernel to perform the requested operation on behalf of the process. After the operation is completed, control returns to the user-space process.

使用system call需要的头文件

在 Linux 中,系统调用的声明并不包含在 <stdio.h> 或 <stdlib.h> 等标准 C 头文件中。相反,系统调用通常在操作系统专用的头文件中声明,这些头文件通常位于 /usr/include/linux 目录或相关子目录中。

虽然标准 C 语言头文件可能间接包含系统调用声明,但系统调用的具体声明和定义通常存在于操作系统特定的头文件中。

In Linux, the declaration of system calls is not included in standard C header files like <stdio.h> or <stdlib.h>. Instead, system calls are typically declared in header files specific to the operating system, often found in the /usr/include/linux directory or related subdirectories.

While standard C header files may indirectly include system call declarations, the specific declarations and definitions for system calls are typically found in OS-specific header files.

Linux 中的系统调用头文件通常位于 /usr/include/linux 目录或相关子目录中。这些头文件包含进行系统调用所需的必要声明、常量和数据结构。

下面是一些常见的系统调用头文件及其典型位置:

1. /usr/include/linux/syscall.h: 该文件包含系统调用号的宏。C 库使用它来定义进行系统调用的函数。

2. /usr/include/sys/syscall.h: 系统调用号宏的另一个位置。它通常与 <unistd.h> 结合使用。

3. /usr/include/sys/types.h: 该头文件定义了系统调用中使用的各种数据类型,如 pid_t、uid_t、gid_t 等。

4. /usr/include/asm/unistd.h: 该文件包含不同体系结构的系统调用编号。内核和 C 库使用它将系统调用号映射到相应的函数。

5. 其他相关头文件: 除此以外,/usr/include/linux 和 /usr/include/sys 中还有许多其他头文件,它们提供了特定系统调用和相关数据结构的定义。

这些头文件对于编写通过系统调用与 Linux 内核直接交互的程序至关重要。它们为用户空间程序进行系统调用提供了必要的接口。

System call header files in Linux are typically located in the /usr/include/linux directory or related subdirectories. These header files contain the necessary declarations, constants, and data structures required for making system calls.

Here are some common system call header files and their typical locations:

1. /usr/include/linux/syscall.h: This file contains macros for system call numbers. It is used by the C library to define functions for making system calls.

2. /usr/include/sys/syscall.h: Another location for the system call number macros. It's often used in conjunction with <unistd.h>.

3. /usr/include/sys/types.h: This header file defines various data types used in system calls, such as pid_t, uid_t, gid_t, etc.

4. /usr/include/asm/unistd.h: This file contains the system call numbers for different architectures. It's used by the kernel and C library to map system call numbers to their corresponding functions.

5. Other related headers: In addition to these, there are many other header files in /usr/include/linux and /usr/include/sys that provide definitions for specific system calls and related data structures.

These header files are essential for writing programs that interact directly with the Linux kernel through system calls. They provide the necessary interface for making system calls from user-space programs.

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值