V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
ReputationZh
V2EX  ›  Linux

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

  •  
  •   ReputationZh · 2020-11-19 17:01:49 +08:00 · 1760 次点击
    这是一个创建于 1491 天前的主题,其中的信息可能已经有所发展或是发生改变。
    3 条回复    2020-11-20 11:57:53 +08:00
    fasionchan
        1
    fasionchan  
       2020-11-19 17:20:32 +08:00   ❤️ 1
    应该是差不多的,我才 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()系统调用来实现的,见倒数第四行。
    julyclyde
        3
    julyclyde  
       2020-11-20 11:57:53 +08:00
    你拿 strace 看到的是 syscall
    unistd.h 只是 libc 而已
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1903 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 16:16 · PVG 00:16 · LAX 08:16 · JFK 11:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.