Linux C 语言的 unistd.h 中的 sync()和 Linux 命令行中 sync 功效一样吗?

2020-11-19 17:01:49 +08:00
 ReputationZh
1760 次点击
所在节点    Linux
3 条回复
fasionchan
2020-11-19 17:20:32 +08:00
应该是差不多的,我才 sync 命令执行时就是调 sync()系统调用。你可以执行以下 strace sync 跟踪一下系统调用,就知道 sync 命令到底干了什么了:

fasion@ubuntu:~$ strace sync
execve("/usr/bin/sync", ["sync"], 0x7ffe44037610 /* 30 vars */) = 0
brk(NULL) = 0x55d3c942f000
arch_prctl(0x3001 /* ARCH_??? */, 0x7fff9667d0b0) = -1 EINVAL (Invalid argument)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=26487, ...}) = 0
mmap(NULL, 26487, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ff948082000
close(3) = 0
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\360q\2\0\0\0\0\0"..., 832) = 832
pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784
pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32, 848) = 32
pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0cBR\340\305\370\2609W\242\345)q\235A\1"..., 68, 880) = 68
fstat(3, {st_mode=S_IFREG|0755, st_size=2029224, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff948080000
pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784
pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32, 848) = 32
pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0cBR\340\305\370\2609W\242\345)q\235A\1"..., 68, 880) = 68
mmap(NULL, 2036952, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7ff947e8e000
mprotect(0x7ff947eb3000, 1847296, PROT_NONE) = 0
mmap(0x7ff947eb3000, 1540096, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x25000) = 0x7ff947eb3000
mmap(0x7ff94802b000, 303104, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19d000) = 0x7ff94802b000
mmap(0x7ff948076000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7ff948076000
mmap(0x7ff94807c000, 13528, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7ff94807c000
close(3) = 0
arch_prctl(ARCH_SET_FS, 0x7ff948081580) = 0
mprotect(0x7ff948076000, 12288, PROT_READ) = 0
mprotect(0x55d3c7e1e000, 4096, PROT_READ) = 0
mprotect(0x7ff9480b6000, 4096, PROT_READ) = 0
munmap(0x7ff948082000, 26487) = 0
brk(NULL) = 0x55d3c942f000
brk(0x55d3c9450000) = 0x55d3c9450000
openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=3035952, ...}) = 0
mmap(NULL, 3035952, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ff947ba8000
close(3) = 0
sync() = 0
close(1) = 0
close(2) = 0
exit_group(0) = ?

我在我的虚拟机上跑了一下,发现 sync 命令确实是调用 sync()系统调用来实现的,见倒数第四行。
barathrum
2020-11-20 10:14:27 +08:00
julyclyde
2020-11-20 11:57:53 +08:00
你拿 strace 看到的是 syscall
unistd.h 只是 libc 而已

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/727205

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX