6

使用 fcntl(非阻塞)或某种自定义方式锁定文件。所以我正在使用 lsof 并检查进程的 pid 是否在那里。如果 lsof 返回空白,则没有使用它。

但是我脚本中的 lsof 需要 200 毫秒。

在 Windows 上,当我尝试测试文件是否被锁定时,我只需打开文件并在错误时锁定,这需要 5 毫秒。是否有任何替代 lsof 的方法可以进行快速测试以查看是否有文件保存?

4

1 回答 1

10

fuser 命令是一个非常智能的 unix 实用程序,用于查找哪个进程正在使用文件、目录或套接字。它还提供有关拥有进程的用户和访问类型的信息。阅读更多——digitalocean.com

要显示访问特定目录的进程,请使用:

fuser -uvm /somedir

下面的输出显示,当以详细模式运行时,fuse 实用程序提供有关USER, PID, ACCESS and COMMAND

root@exampleuser-X55CR:~# fuser -v .
                     USER        PID ACCESS COMMAND
/root:               root       3378 ..c.. vim
                     root       3398 ..c.. bash
                     root       3449 ..c.. bash
                     root      19370 ..c.. bash

fuser在识别打开特定文件的进程 ID 时很有用。

lsof对于找出特定进程打开的所有文件很有用。

有关 fuser 的更多选项,您可以查看他们的手册页man fuser

这是一些:

]$ fuser
No process specification given
Usage: fuser [ -a | -s | -c ] [ -n SPACE ] [ -SIGNAL ] [ -kimuv ] NAME...
             [ - ] [ -n SPACE ] [ -SIGNAL ] [ -kimuv ] NAME...
       fuser -l
       fuser -V
Show which processes use the named files, sockets, or filesystems.

    -a        display unused files too
    -c        mounted FS
    -f        silently ignored (for POSIX compatibility)
    -i        ask before killing (ignored without -k)
    -k        kill processes accessing the named file
    -l        list available signal names
    -m        show all processes using the named filesystems
    -n SPACE  search in this name space (file, udp, or tcp)
    -s        silent operation
    -SIGNAL   send this signal instead of SIGKILL
    -u        display user IDs
    -v        verbose output
    -V        display version information
    -4        search IPv4 sockets only
    -6        search IPv6 sockets only
    -         reset options

  udp/tcp names: [local_port][,[rmt_host][,[rmt_port]]]
于 2014-09-22T20:20:23.900 回答