简体   繁体   English

即使进程终止,waitpid始终返回-1

[英]waitpid always returns -1 even if the process is terminated

I'm on Mac and I use Qt 5. I have a button in my QMainWindow that execute a program when the it s clicked. 我在Mac上,使用Qt5。QMainWindow中有一个按钮,单击该按钮即可执行程序。

I used this static function to execute a detached process :QProcess::startDetached ( http://doc.qt.io/qt-5/qprocess.html#startDetached ). 我使用此静态函数执行了一个独立的进程:QProcess :: startDetached( http://doc.qt.io/qt-5/qprocess.html#startDetached )。

I have no problem to start / kill a process with this function on Mac. 我在Mac上使用此功能启动/终止进程没有问题。

I would like to determine if the user terminates the process (an OpenGL application). 我想确定用户是否终止进程(OpenGL应用程序)。 I used this code 我用了这段代码

void MyProgram::startApplication()
{
bool ret = QProcess::startDetached(program,arguments, workingDirectory, &m_PID);
if (ret && m_PID)
{
    printf("m_PID = %d (started)\n", (int)m_PID);
    QThread* thread = new QThread;
    connect(thread, &QThread::started, [this]() {
        qint64 pid = m_PID;
        printf("pid = %d (started)\n", (int)pid);
        QThread::msleep(2000);
        while (pid)
        {
            int wstatus = 0;
            pid_t ret = waitpid(pid, &wstatus, WNOHANG);
            if (ret > 0)
            {
                if (WIFSIGNALED(wstatus) || WIFSTOPPED(wstatus) || WIFEXITED(wstatus))
                {
                    // dont kill or stop cuz the process is already finished
                    printf("pid = %d ret = %d (stopped)\n", (int)pid, (int)ret);
                    break;
                }
            }
            else if (ret < 0)
            {
                printf("pid = %d ret = %d(error)\n", (int)pid, (int)ret);
                //break;
            }
            QThread::msleep(200);
        }
        m_PID = 0;
    });
    }

   bool MyProgram::IsRunning()
   {
       return m_PID != 0;
   }

This problem is that waitpid() always returns -1 and I can't never know when the started process is finished or killed. 这个问题是,waitpid()始终返回-1,而我永远不知道启动进程何时完成或终止。

I think that waitpid works only when you re the parent no in detached process but kill(m_PID, SIGINT) works (stop the process) 我认为waitpid仅在您在分离的过程中将父项重新设置为no时才有效,但是kill(m_PID,SIGINT)起作用(停止过程)

I need only to know if the process is finished or not 我只需要知道程序是否完成

QProcess::startDetached documents that: QProcess :: startDetached文件说明:

Unix: The started process will run in its own session and act like a daemon. Unix:启动的进程将在其自己的会话中运行,并充当守护程序。

(so I guess it calls daemon(3) or do something equivalent) (所以我猜它会调用daemon(3)或执行等效的操作)

If you really want to use waitpid(2) you'll better use the usual fork(2) and execve(2) in your own code (but that is not the Qt way of doing). 如果您真的想使用waitpid(2) ,则最好在自己的代码中使用常规的fork(2)execve(2) (但这不是Qt的做法)。

BTW, why don't you use QProcess the usual way? 顺便说一句,为什么不按常规方式使用QProcess It should be more Qt friendly (and more portable to other OSes). 它应该更加Qt友好(并且对于其他操作系统更易移植)。 You'll use (ie connect to some of your slot ) the QProcess::finished qt-signal. 您将使用(即连接到您的某些插槽QProcess :: finished qt-signal。

but kill(m_PID, SIGINT) works 但杀死(m_PID,SIGINT)工作

Of course, kill(2) can be used on some non-child process. 当然, kill(2)可以用于某些非子进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM