lsof命令

 

cat hold_file.c 
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
    int cnt = 0;
    while(1)
    {
        char name[64] ={0};
        snprintf(name,sizeof(name),"%d.txt",cnt);    
        int fd = creat(name,644);//  creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC.
        sleep(10);
        ++cnt;
    }
    return 0;
}

 

 

./a.out &
[1] 35724

lsof -p 35724 > log

创建文件未关闭,可以查看打开了哪些文件没有被关闭

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
a.out   35724  gec  cwd    DIR   0,50     4096   4468 /mnt/hgfs/share/file
a.out   35724  gec  rtd    DIR    8,1     4096      2 /
a.out   35724  gec  txt    REG   0,50     8712   4482 /mnt/hgfs/share/file/a.out
a.out   35724  gec  mem    REG    8,1  1868984 927825 /lib/x86_64-linux-gnu/libc-2.23.so
a.out   35724  gec  mem    REG    8,1   162632 927823 /lib/x86_64-linux-gnu/ld-2.23.so
a.out   35724  gec    0u   CHR  136,1      0t0      4 /dev/pts/1
a.out   35724  gec    1u   CHR  136,1      0t0      4 /dev/pts/1
a.out   35724  gec    2u   CHR  136,1      0t0      4 /dev/pts/1
a.out   35724  gec    3w   REG   0,50        0   4483 /mnt/hgfs/share/file/0.txt
a.out   35724  gec    4w   REG   0,50        0   4484 /mnt/hgfs/share/file/1.txt
a.out   35724  gec    5w   REG   0,50        0   4485 /mnt/hgfs/share/file/2.txt
a.out   35724  gec    6w   REG   0,50        0   4486 /mnt/hgfs/share/file/3.txt
a.out   35724  gec    7w   REG   0,50        0   4487 /mnt/hgfs/share/file/4.txt
a.out   35724  gec    8w   REG   0,50        0   4488 /mnt/hgfs/share/file/5.txt
a.out   35724  gec    9w   REG   0,50        0   4489 /mnt/hgfs/share/file/6.txt
a.out   35724  gec   10w   REG   0,50        0   4490 /mnt/hgfs/share/file/7.txt
a.out   35724  gec   11w   REG   0,50        0   4491 /mnt/hgfs/share/file/8.txt
a.out   35724  gec   12w   REG   0,50        0   4492 /mnt/hgfs/share/file/9.txt
a.out   35724  gec   13w   REG   0,50        0   4493 /mnt/hgfs/share/file/10.txt
a.out   35724  gec   14w   REG   0,50        0   4494 /mnt/hgfs/share/file/11.txt

 

posted @ 2019-12-05 10:37  狂奔~  阅读(366)  评论(0编辑  收藏  举报