先上脚本:
#!/bin/sh
#example
#Find which process /dev/dri/card0 is occupied by
#./find_file.sh /dev/dri/card0
if [ $# -ne 1 ]
then
echo "Incorrect number of parameters, please add a parameter."
echo "example:Find which process /dev/dri/card0 is occupied by"
echo "./find_file.sh /dev/dri/card0"
exit 1
fi
for process in /proc/*
do
if [ ! -e ${process}/fd ]
then
continue
fi
str=`ls -l ${process}/fd | grep "$1"`
if [ -n "$str" ]
then
echo $process >> tt.log
echo $process
fi
done
脚本输出:
#脚本会输出类似如下内容
#/proc/后面的数字表示进程的ID
#我们可以根据进程的ID找到对应的进程
/proc/1017
/proc/1024
/proc/1350
/proc/584
/proc/836
/proc/843
原理说明:
进程打开的文件,在/proc对应的进程目录下面的fd目录里面有记录。我们通过脚本遍历所有进程的fd目录,以此来达到我们的需求。