'I need to check if the process is running using pid in QT

I want to check if the process is running with process id (pid).
My project is developed using QT and can be executed on both Windows and Linux. So my code need to be able to run on both OS.
I have searched full through stackoverflow but I couldn't find expected result.

I already tried to execute cmd command - tasklist, kill. But I still can't get result.
I think there is a way to use QProcess to get access of specified process with pid.

Here are my code I used.

        #ifdef Q_OS_WIN32
            QVariant variant(this->pid);
            QString cmd = "tasklist /nh /fi \"pid eq " + variant.toString() + "\"";

            QProcess process;
            process.start(cmd);
            process.waitForReadyRead();

            QString result = process.readAll();
            process.terminate();
            if (result.indexOf(variant.toString()) >= 0)
            {
                return true;
            }
            else {
                return false;
            }

        #else
            QVariant variant(this->pid);
            QString cmd = "kill -0 " + variant.toString();

            QProcess process;
            process.start(cmd);
            process.waitForReadyRead();

            QString result = process.readAll();
            process.terminate();
            if (result.indexOf("No") >= 0)
            {
                return false;
            }
            else {
                return true;
            }
        #endif


I want to get help.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source