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;