refactor: Replace popen with QProcess for process count and kill oper…#419
Conversation
…ations - Updated `getProcessCountByName` to use `QProcess` for executing the `ps` command, improving reliability and handling of process output. - Refactored `killProcessByName` to utilize `QProcess` for killing processes, enhancing code consistency and error handling. - Improved null and empty string checks for process names in both functions. This change enhances the maintainability and performance of process management in the application.
There was a problem hiding this comment.
Sorry @dengzhongyuan365-dev, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
deepin pr auto review这段代码的修改将原本使用 C 标准库 ( 以下是对修改后代码的详细审查,包括语法逻辑、代码质量、代码性能和代码安全方面的改进建议。 1. 代码逻辑与功能
2. 代码质量
3. 代码性能
4. 代码安全
综合改进代码示例结合以上分析,建议对代码进行如下进一步优化: #include <QProcess>
#include <QStringList>
#include <QDebug>
int Utils::getProcessCountByName(const char *pstrName)
{
qDebug() << "Enter getProcessCountByName, pstrName:" << pstrName;
if (pstrName == NULL || strlen(pstrName) == 0) {
qDebug() << "pstrName is null or empty";
return -1;
}
QProcess process;
// 使用 ps -e 仅显示进程名,或者 ps -ef 显示完整信息
// 这里为了准确匹配,建议解析 ps 的输出
process.start("ps", QStringList() << "-e"); // 或者 "-eo comm=" 仅获取命令名
if (!process.waitForFinished(5000)) {
qDebug() << "ps command timeout or failed";
return -1;
}
QByteArray output = process.readAllStandardOutput();
QString processName = QString::fromUtf8(pstrName);
int count = 0;
// 逐行处理,避免 split 产生的大内存分配
QList<QByteArray> lines = output.split('\n');
for (const QByteArray &line : lines) {
QString lineStr = QString::fromUtf8(line).trimmed();
if (lineStr.isEmpty()) continue;
// 简单的包含匹配仍然存在子串匹配问题
// 理想情况下应该解析出完整的进程名进行全等比较
// 这里假设 ps -e 的输出格式,最后一列是进程名(实际情况可能更复杂)
// 如果使用 ps -eo comm=,则整行就是进程名
if (lineStr == processName) {
count++;
}
}
qDebug() << "Exit getProcessCountByName, count:" << count;
return count;
}
void Utils::killProcessByName(const char *pstrName)
{
qDebug() << "Enter killProcessByName, pstrName:" << pstrName;
if (pstrName == NULL || strlen(pstrName) == 0) {
qDebug() << "pstrName is null or empty";
return;
}
QProcess process;
// 注意:killall 并非所有系统都默认可用(如某些最小化 Linux 发行版或默认的 macOS 行为不同)
// 但对于大多数桌面应用场景是可以接受的
process.start("killall", QStringList() << QString::fromUtf8(pstrName));
// 检查执行结果
if (!process.waitForFinished(5000)) {
qDebug() << "killall command timeout";
} else {
int exitCode = process.exitCode();
if (exitCode != 0) {
// exitCode 1 通常表示没有找到进程
qDebug() << "killall finished with exit code:" << exitCode << process.readAllStandardError();
} else {
qDebug() << "Process killed successfully";
}
}
qDebug() << "Exit killProcessByName";
}总结建议
|
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: dengzhongyuan365-dev, lzwind The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/forcemerge |
…ations
getProcessCountByNameto useQProcessfor executing thepscommand, improving reliability and handling of process output.killProcessByNameto utilizeQProcessfor killing processes, enhancing code consistency and error handling.This change enhances the maintainability and performance of process management in the application.