您的位置:首页 > 其它

查看程序在运行过程中使用到的文件

2020-09-09 23:54 134 查看
查看程序在运行过程中使用到的文件 .title { text-align: center; margin-bottom: 0.2em } .subtitle { text-align: center; font-size: medium; font-weight: bold; margin-top: 0 } .todo { font-family: monospace; color: rgba(255, 0, 0, 1) } .done { font-family: monospace; color: rgba(0, 128, 0, 1) } .priority { font-family: monospace; color: rgba(255, 165, 0, 1) } .tag { background-color: rgba(238, 238, 238, 1); font-family: monospace; padding: 2px; font-size: 80%; font-weight: normal } .timestamp { color: rgba(190, 190, 190, 1) } .timestamp-kwd { color: rgba(95, 158, 160, 1) } .org-right { margin-left: auto; margin-right: 0; text-align: right } .org-left { margin-left: 0; margin-right: auto; text-align: left } .org-center { margin-left: auto; margin-right: auto; text-align: center } .underline { text-decoration: underline } #postamble p, #preamble p { font-size: 90%; margin: 0.2em } p.verse { margin-left: 3% } pre { border: 1px solid rgba(204, 204, 204, 1); box-shadow: 3px 3px 3px rgba(238, 238, 238, 1); padding: 8pt; font-family: monospace; overflow: auto; margin: 1.2em } pre.src { position: relative; overflow: visible; padding-top: 1.2em } pre.src:before { display: none; position: absolute; background-color: rgba(255, 255, 255, 1); top: -10px; right: 10px; padding: 3px; border: 1px solid rgba(0, 0, 0, 1) } pre.src:hover:before { display: inline } pre.src-asymptote:before { content: "Asymptote" } pre.src-awk:before { content: "Awk" } pre.src-C:before { content: "C" } pre.src-clojure:before { content: "Clojure" } pre.src-css:before { content: "CSS" } pre.src-D:before { content: "D" } pre.src-ditaa:before { content: "ditaa" } pre.src-dot:before { content: "Graphviz" } pre.src-calc:before { content: "Emacs Calc" } pre.src-emacs-lisp:before { content: "Emacs Lisp" } pre.src-fortran:before { content: "Fortran" } pre.src-gnuplot:before { content: "gnuplot" } pre.src-haskell:before { content: "Haskell" } pre.src-hledger:before { content: "hledger" } pre.src-java:before { content: "Java" } pre.src-js:before { content: "Javascript" } pre.src-latex:before { content: "LaTeX" } pre.src-le 56c dger:before { content: "Ledger" } pre.src-lisp:before { content: "Lisp" } pre.src-lilypond:before { content: "Lilypond" } pre.src-lua:before { content: "Lua" } pre.src-matlab:before { content: "MATLAB" } pre.src-mscgen:before { content: "Mscgen" } pre.src-ocaml:before { content: "Objective Caml" } pre.src-octave:before { content: "Octave" } pre.src-org:before { content: "Org mode" } pre.src-oz:before { content: "OZ" } pre.src-plantuml:before { content: "Plantuml" } pre.src-processing:before { content: "Processing.js" } pre.src-python:before { content: "Python" } pre.src-R:before { content: "R" } pre.src-ruby:before { content: "Ruby" } pre.src-sass:before { content: "Sass" } pre.src-scheme:before { content: "Scheme" } pre.src-screen:before { content: "Gnu Screen" } pre.src-sed:before { content: "Sed" } pre.src-sh:before { content: "shell" } pre.src-sql:before { content: "SQL" } pre.src-sqlite:before { content: "SQLite" } pre.src-forth:before { content: "Forth" } pre.src-io:before { content: "IO" } pre.src-J:before { content: "J" } pre.src-makefile:before { content: "Makefile" } pre.src-maxima:before { content: "Maxima" } pre.src-perl:before { content: "Perl" } pre.src-picolisp:before { content: "Pico Lisp" } pre.src-scala:before { content: "Scala" } pre.src-shell:before { content: "Shell Script" } pre.src-ebnf2ps:before { content: "ebfn2ps" } pre.src-cpp:before { con 56c tent: "C++" } pre.src-abc:before { content: "ABC" } pre.src-coq:before { content: "Coq" } pre.src-groovy:before { content: "Groovy" } pre.src-bash:before { content: "bash" } pre.src-csh:before { content: "csh" } pre.src-ash:before { content: "ash" } pre.src-dash:before { content: "dash" } pre.src-ksh:before { content: "ksh" } pre.src-mksh:before { content: "mksh" } pre.src-posh:before { content: "posh" } pre.src-ada:before { content: "Ada" } pre.src-asm:before { content: "Assembler" } pre.src-caml:before { content: "Caml" } pre.src-delphi:before { content: "Delphi" } pre.src-html:before { content: "HTML" } pre.src-idl:before { content: "IDL" } pre.src-mercury:before { content: "Mercury" } pre.src-metapost:before { content: "MetaPost" } pre.src-modula-2:before { content: "Modula-2" } pre.src-pascal:before { content: "Pascal" } pre.src-ps:before { content: "PostScript" } pre.src-prolog:before { content: "Prolog" } pre.src-simula:before { content: "Simula" } pre.src-tcl:before { content: "tcl" } pre.src-tex:before { content: "TeX" } pre.src-plain-tex:before { content: "Plain TeX" } pre.src-verilog:before { content: "Verilog" } pre.src-vhdl:before { content: "VHDL" } pre.src-xml:before { content: "XML" } pre.src-nxml:before { content: "XML" } pre.src-conf:before { content: "Configuration File" } table { border-collapse: collapse } caption.t-above { caption-side: top } c 56c aption.t-bottom { caption-side: bottom } td, th { vertical-align: top } th.org-right { text-align: center } th.org-left { text-align: center } th.org-center { text-align: center } td.org-right { text-align: right } td.org-left { text-align: left } td.org-center { text-align: center } dt { font-weight: bold } .footpara { display: inline } .footdef { margin-bottom: 1em } .figure { padding: 1em } .figure p { text-align: center } .equation-container { display: table; text-align: center; width: 100% } .equation { vertical-align: middle } .equation-label { display: table-cell; text-align: right; vertical-align: middle } .inlinetask { padding: 10px; border: 2px solid rgba(128, 128, 128, 1); margin: 10px; background: rgba(255, 255, 204, 1) } #org-div-home-and-up { text-align: right; font-size: 70%; white-space: nowrap } textarea { overflow-x: auto } .linenr { font-size: smaller } .code-highlighted { background-color: rgba(255, 255, 0, 1) } .org-info-js_info-navigation { border-style: none } #org-info-js_console-label { font-size: 10px; font-weight: bold; white-space: nowrap } .org-info-js_search-highlight { background-color: rgba(255, 255, 0, 1); color: rgba(0, 0, 0, 1); font-weight: bold } .org-svg { width: 90% }

查看程序在运行过程中使用到的文件

Table of Contents

  • 4. 总结
  • 1 场景

    试想这个场景

    • 写了一个脚本在运行
    • 脚本运行时间需要很长,并且你不能停下脚本(会有中断问题)
    • 这时候你想知道自己的脚本进程在处理什么文件,在做什么事情,该怎么办呢?

    2 想知道进程在读写什么文件

    首先 使用

    ps aux 或者 ps ef
    查看自己的进程 id

    然后到

    /proc/
    目录中下查看相应进程的进程运行数据

    ps aux
    数据

    wudanya+  3166  0.0  0.0 107956   616 ?        S    22:04   0:00 sleep 180
    wudanya+  3189  0.0  0.0 107956   616 pts/3    S+   22:06   0:00 sleep 1000000
    wudanya+  3191  0.0  0.0 112728   972 pts/5    R+   22:06   0:00 grep --color=auto sleep

    第二列就是每个进程的 pid ,在系统中是唯一的

    查看

    /proc/[pid]/fd
    目录

    如图, 可以看到,进程打开了 4 个文件描述符

    其中 0、1、2 分别代表 标准输入、输出、错误(规定)

    25 代表的是一个虚拟终端

    3 打开的是一个文件

    这样就可以知道你打开的是什么文件了

    是不是很简单呢

    3 我怎么知道文件正在读取还是写入呢

    这还用问,当然是看代码了

    开个玩笑

    咱们这次是说不用看代码就可以看出来在写入什么文件,在读取什么文件

    3.1 lsof -p pid

    [wudanyang@bogon ~]$ lsof -p 3352
    COMMAND  PID      USER   FD      TYPE DEVICE  SIZE/OFF     NODE NAME
    tail    3352 wudanyang  cwd       DIR  253,0      4096  4731473 /home/wudanyang
    tail    3352 wudanyang  rtd       DIR  253,0       240       64 /
    tail    3352 wudanyang  txt       REG  253,0     66824 12829983 /usr/bin/tail
    tail    3352 wudanyang  mem       REG  253,0 106075056 12587626 /usr/lib/locale/locale-archive
    tail    3352 wudanyang  mem       REG  253,0   2156160    44517 /usr/lib64/libc-2.17.so
    tail    3352 wudanyang  mem       REG  253,0    163400    44510 /usr/lib64/ld-2.17.so
    tail    3352 wudanyang    0u      CHR  136,3       0t0        6 /dev/pts/3
    tail    3352 wudanyang    1u      CHR  136,3       0t0        6 /dev/pts/3
    tail    3352 wudanyang    2u      CHR  136,3       0t0        6 /dev/pts/3
    tail    3352 wudanyang    3r      REG  253,0         0  5171247 /home/wudanyang/a
    tail    3352 wudanyang    4r  a_inode   0,10         0     5338 inotify
    tail    3352 wudanyang   25u      CHR    5,2       0t0     7061 /dev/ptmx

    前三列可以不用管,第四列是打开的描述符,可以看到

    FD:

    u 读/写
    r 只读
    w 只写
    W 获得写锁

    OK,搞定,可以看到咱们程序打开了一个

    /home/wudanyang/a
    文件只读描述符

    3.2 使用 strace -p pid 看一下系统调用

    通过系统调用查看一下当前正在执行的系统调用,也可以看到是 读还是写。上图中是一个读取的系统调用,参数是文件描述符 4

    4 总结

    • 日常工作中会经常遇到这种需求,总结一下
    • /proc/[pid]/
      不止有 fd ,可以继续深入了解一下

    ===

    作者: 吴丹阳 https://www.cnblogs.com/wudanyang

    更新时间: 2020-09-10 Thu 00:09

    Emacs 28.0.50 (Org mode 9.3.7)

    ===

    天行健,君子以自强不息。

    地势坤,君子以厚德载物。

    ===

    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: 
    相关文章推荐