如何在C ++程序中启动可执行文件并获取其进程ID(在linux中)?

时间:2022-08-31 22:49:43

i tried system(), but somehow when the secondary program runs, my main program(primary program which executes the secondary) hangs

我尝试了system(),但不知何故,当辅助程序运行时,我的主程序(执行辅助程序的主程序)挂起

and second issue is how do i obtain the process id of the secondary program in my main program?

第二个问题是如何在主程序中获取辅助程序的进程ID?

2 个解决方案

#1


13  

In the parent process you want to fork.

在您希望fork的父进程中。

Fork creates an entirely new process and returns either the child process's pid to the calling process, and 0 to the new child process.

Fork创建一个全新的进程,并将子进程的pid返回给调用进程,将0返回给新的子进程。

In the child process you can then use something like execl to execute your desired secondary program. In the parent process you can use waitpid to wait for the child to complete.

在子进程中,您可以使用类似execl的内容来执行所需的辅助程序。在父进程中,您可以使用waitpid等待子进程完成。

Here is a simple illustrative example:

这是一个简单的说明性示例:

#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>

int main()
{
    std::string cmd = "/bin/ls"; // secondary program you want to run

    pid_t pid = fork(); // create child process
    int status;

    switch (pid)
    {
    case -1: // error
        perror("fork");
        exit(1);

    case 0: // child process
        execl(cmd.c_str(), 0, 0); // run the command
        perror("execl"); // execl doesn't return unless there is a problem
        exit(1);

    default: // parent process, pid now contains the child pid
        while (-1 == waitpid(pid, &status, 0)); // wait for child to complete
        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
        {
            // handle error
            std::cerr << "process " << cmd << " (pid=" << pid << ") failed" << std::endl;
        }
        break;
    }
    return 0;
}

#2


4  

Use fork to create a new process, then exec to run a program in the new process. There are many such examples.

使用fork创建一个新进程,然后执行exec以在新进程中运行程序。有很多这样的例子。

#1


13  

In the parent process you want to fork.

在您希望fork的父进程中。

Fork creates an entirely new process and returns either the child process's pid to the calling process, and 0 to the new child process.

Fork创建一个全新的进程,并将子进程的pid返回给调用进程,将0返回给新的子进程。

In the child process you can then use something like execl to execute your desired secondary program. In the parent process you can use waitpid to wait for the child to complete.

在子进程中,您可以使用类似execl的内容来执行所需的辅助程序。在父进程中,您可以使用waitpid等待子进程完成。

Here is a simple illustrative example:

这是一个简单的说明性示例:

#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>

int main()
{
    std::string cmd = "/bin/ls"; // secondary program you want to run

    pid_t pid = fork(); // create child process
    int status;

    switch (pid)
    {
    case -1: // error
        perror("fork");
        exit(1);

    case 0: // child process
        execl(cmd.c_str(), 0, 0); // run the command
        perror("execl"); // execl doesn't return unless there is a problem
        exit(1);

    default: // parent process, pid now contains the child pid
        while (-1 == waitpid(pid, &status, 0)); // wait for child to complete
        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
        {
            // handle error
            std::cerr << "process " << cmd << " (pid=" << pid << ") failed" << std::endl;
        }
        break;
    }
    return 0;
}

#2


4  

Use fork to create a new process, then exec to run a program in the new process. There are many such examples.

使用fork创建一个新进程,然后执行exec以在新进程中运行程序。有很多这样的例子。