您的位置:首页 > 运维架构 > Linux

linux文件是否被当前进程外的其他进程打开

2015-06-08 16:03 399 查看
lsof命令的用法

QProcess 调用shell命令,并获取标准输出内容:

QString pPath = "/tmp/1.txt";
    QProcess commandProcess;
commandProcess.start("lsof", QStringList(pPath));
commandProcess.waitForStarted();
commandProcess.waitForFinished();
QString pResult = QString(commandProcess.readAllStandardOutput());


判断文件被当前进程外的其他进程打开与否:
QStringList pidList = pResult.split("COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME").last().split("\n", QString::SkipEmptyParts);
QString pidStr = QString(" " + QString::number(getpid()) + " ");
for(int i=0; i<pidList.count(); i++)
{
if(pidList.at(i).contains(pidStr) == false)
return true;
}
<pre name="code" class="cpp">    return false;



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