参考引用
- Unix和Linux操作系统有什么区别?
- 一文带你彻底搞懂posix
Linux系统编程(文章链接汇总)
Linux 应用编程(系统编程)与裸机编程、驱动编程有什么区别?
- 1. 裸机编程:一般把没有操作系统支持的编程环境称为裸机编程环境,如:单片机上的编程开发,编写直接在硬件上运行的程序,没有操作系统支持
- 2. 驱动编程:狭义上 Linux 驱动编程指的是基于内核驱动框架开发驱动程序,驱动开发工程师通过调用 Linux 内核提供的接口完成设备驱动的注册,驱动程序负责底层硬件操作相关逻辑
- 3. 应用编程:基于 Linux 操作系统的应用编程,在应用程序中通过调用系统调用 API 完成应用程序的功能和逻辑,应用程序运行于操作系统之上。通常在操作系统下有两种不同的状态:内核态和用户态,应用程序运行在用户态,而内核则运行在内核态
为什么需要库函数?
- 有些系统调用使用起来并不是很方便,于是就出现了 C 语言库,这些 C 语言库函数的设计是为了提供比底层系统调用更为方便、更为好用、且更具有可移植性的调用接口
$ cd /lib/x86_64-linux-gnu
$ ls -l libc.so.6 # libc.so.6 软链接到 libc-2.27.so 库文件,2.27 便是 Linux 系统的 glibc 版本
lrwxrwxrwx 1 root root 12 5月 3 2022 libc.so.6 -> libc-2.27.so
用户态–>系统调用–>内核态–>返回用户态
实例
// test.c
#include
int main(int argc, char **argv) {
printf("Hello world!");
return 0;
}
$ gcc test.c -o test
$ strace test # 运行程序前加上 strace,可以追踪库函数调用过程
execve("./test3", ["./test3"], 0x7fffb2493520 /* 82 vars */) = 0
brk(NULL) = 0x560a8c38b000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/ros/melodic/lib/tls/x86_64/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/ros/melodic/lib/tls/x86_64/x86_64", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/ros/melodic/lib/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/ros/melodic/lib/tls/x86_64", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/ros/melodic/lib/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/ros/melodic/lib/tls/x86_64", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/ros/melodic/lib/tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/ros/melodic/lib/tls", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/ros/melodic/lib/x86_64/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/ros/melodic/lib/x86_64/x86_64", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/ros/melodic/lib/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/ros/melodic/lib/x86_64", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/ros/melodic/lib/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/ros/melodic/lib/x86_64", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/opt/ros/melodic/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/ros/melodic/lib", {st_mode=S_IFDIR|0755, st_size=16384, ...}) = 0
openat(AT_FDCWD, "/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/tls/x86_64/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/tls/x86_64/x86_64", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/tls/x86_64", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/tls/x86_64", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/tls", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/x86_64/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/x86_64/x86_64", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/x86_64", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/x86_64", 0x7ffff162afb0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/home/yue/PX4-Autopilot/build/px4_sitl_default/build_gazebo", {st_mode=S_IFDIR|0775, st_size=4096, ...}) = 0
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=274620, ...}) = 0
mmap(NULL, 274620, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f2f5e388000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240\35\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2030928, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2f5e386000
mmap(NULL, 4131552, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2f5ddb2000
mprotect(0x7f2f5df99000, 2097152, PROT_NONE) = 0
mmap(0x7f2f5e199000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f2f5e199000
mmap(0x7f2f5e19f000, 15072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f2f5e19f000
close(3) = 0
arch_prctl(ARCH_SET_FS, 0x7f2f5e3874c0) = 0
mprotect(0x7f2f5e199000, 16384, PROT_READ) = 0
mprotect(0x560a8c066000, 4096, PROT_READ) = 0
mprotect(0x7f2f5e3cc000, 4096, PROT_READ) = 0
munmap(0x7f2f5e388000, 274620) = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
brk(NULL) = 0x560a8c38b000
brk(0x560a8c3ac000) = 0x560a8c3ac000
write(1, "Hello world!", 12Hello world!) = 12
exit_group(0) = ?
+++ exited with 0 +++
程序虽然只有一个printf函数,但在执行过程中前后调用了 execve、access、open、fstat、mmap、brk、write 等系统调用。其中 write 系统调用会把字符串 Hello world! 通过设备文件 1 发送到驱动,该设备节点对应终端 stdout
$ ls /dev/std* -l
lrwxrwxrwx 1 root root 15 12月 11 21:10 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 12月 11 21:10 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 12月 11 21:10 /dev/stdout -> /proc/self/fd/1