![新手可以从哪些内核知识中受益?](https://linux22.com/image/111732/%E6%96%B0%E6%89%8B%E5%8F%AF%E4%BB%A5%E4%BB%8E%E5%93%AA%E4%BA%9B%E5%86%85%E6%A0%B8%E7%9F%A5%E8%AF%86%E4%B8%AD%E5%8F%97%E7%9B%8A%EF%BC%9F%20.png)
我知道,你必须了解 C 编程语言,并对操作系统的工作原理有更深入的了解,但如果你只是对编程语言、shell 脚本和命令行有基本了解的人,是否有一些关于内核的知识值得了解?即使能力较弱也能从中受益?
答案1
作为 Linux 用户类别,我也一直在问自己这个问题。
第一个想到的是strace
(手册页)。
了解内核的各个部分可以让您深入了解如何解释 strace 的输出并进一步从其常规日志中调试代码。
例如,这是 的输出strace cat text.txt
,其中text.txt
包含句子my name is test
。
execve("/usr/bin/cat", ["cat", "test.txt"], [/* 76 vars */]) = 0
brk(0) = 0x1e4f000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1b8ecd4000
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=138212, ...}) = 0
mmap(NULL, 138212, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f1b8ecb2000
close(3) = 0
open("/lib64/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@\34\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2118128, ...}) = 0
mmap(NULL, 3932672, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f1b8e6f5000
mprotect(0x7f1b8e8ab000, 2097152, PROT_NONE) = 0
mmap(0x7f1b8eaab000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b6000) = 0x7f1b8eaab000
mmap(0x7f1b8eab1000, 16896, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f1b8eab1000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1b8ecb1000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1b8ecaf000
arch_prctl(ARCH_SET_FS, 0x7f1b8ecaf740) = 0
mprotect(0x7f1b8eaab000, 16384, PROT_READ) = 0
mprotect(0x60b000, 4096, PROT_READ) = 0
mprotect(0x7f1b8ecd5000, 4096, PROT_READ) = 0
munmap(0x7f1b8ecb2000, 138212) = 0
brk(0) = 0x1e4f000
brk(0x1e70000) = 0x1e70000
brk(0) = 0x1e70000
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=106070960, ...}) = 0
mmap(NULL, 106070960, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f1b881cc000
close(3) = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
open("test.txt", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0664, st_size=16, ...}) = 0
fadvise64(3, 0, 0, POSIX_FADV_SEQUENTIAL) = 0
read(3, "my name is test\n", 65536) = 16
write(1, "my name is test\n", 16my name is test
) = 16
read(3, "", 65536) = 0
close(3) = 0
close(1) = 0
close(2) = 0
exit_group(0) = ?
+++ exited with 0 +++
全部系统调用上面可以看到,因此,如果您对内核有一定的了解,那么您在调试代码时就会有更多线索。
答案2
这为您提供了一个视图,在某些情况下,了解内核类型、体系结构、模块……以及它们的兼容性或……是有好处的。