简体   繁体   English

如何在c ++中使用system()命令获取进程的pid

[英]How to get pid of process executed with system() command in c++

When we use system() command, program wait until it complete but I am executing a process using system() and using load balance server due to which program comes to next line just after executing system command.当我们使用system()命令时,程序会等待它完成,但我正在使用system()并使用负载平衡服务器执行一个process ,因为该程序在执行系统命令后立即进入下一行。 Please note that that process may not be complete.请注意,该process可能不完整。

system("./my_script");

// after this I want to see whether it is complete or not using its pid.
// But how do i Know PID?
IsScriptExecutionComplete();

Simple answer: you can't. 简单回答:你做不到。

The purpose of system() is to block when command is being executed. system()的目的是阻止何时执行命令。

But you can 'cheat' like this: 但你可以像这样“欺骗”:

pid_t system2(const char * command, int * infp, int * outfp)
{
    int p_stdin[2];
    int p_stdout[2];
    pid_t pid;

    if (pipe(p_stdin) == -1)
        return -1;

    if (pipe(p_stdout) == -1) {
        close(p_stdin[0]);
        close(p_stdin[1]);
        return -1;
    }

    pid = fork();

    if (pid < 0) {
        close(p_stdin[0]);
        close(p_stdin[1]);
        close(p_stdout[0]);
        close(p_stdout[1]);
        return pid;
    } else if (pid == 0) {
        close(p_stdin[1]);
        dup2(p_stdin[0], 0);
        close(p_stdout[0]);
        dup2(p_stdout[1], 1);
        dup2(::open("/dev/null", O_RDONLY), 2);
        /// Close all other descriptors for the safety sake.
        for (int i = 3; i < 4096; ++i)
            ::close(i);

        setsid();
        execl("/bin/sh", "sh", "-c", command, NULL);
        _exit(1);
    }

    close(p_stdin[0]);
    close(p_stdout[1]);

    if (infp == NULL) {
        close(p_stdin[1]);
    } else {
        *infp = p_stdin[1];
    }

    if (outfp == NULL) {
        close(p_stdout[0]);
    } else {
        *outfp = p_stdout[0];
    }

    return pid;
}

Here you can have not only PID of the process, but also it's STDIN and STDOUT . 在这里,您不仅可以拥有流程的PID ,还可以拥有STDINSTDOUT Have fun! 玩得开心!

Not an expert on this myself, but if you look at the man page for system : 我自己不是这方面的专家,但如果你看一下系统手册页

system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed system()通过调用/ bin / sh -c命令执行命令中指定的命令,并在命令完成后返回

You can go into the background within the command/script you're executing (and return immediately), but I don't think there's a specific provision in system for that case. 您可以在正在执行的命令/脚本中进入后台(并立即返回),但我认为系统中没有针对该情况的特定规定。

Ideas I can think of are: 我能想到的想法是:

  1. Your command might return the pid through the return code. 您的命令可能会通过返回代码返回pid。
  2. Your code might want to look up the name of the command in the active processes (eg /proc APIs in unix-like environments). 您的代码可能希望在活动进程中查找命令的名称(例如,类似于unix的环境中的/ proc API)。
  3. You might want to launch the command yourself (instead of through a SHELL) using fork / exec 您可能希望使用fork / exec自己启动命令(而不是通过SHELL)

You can check exit status of your command by following code : 您可以通过以下代码检查命令的退出状态:

int ret = system("./my_script");

if (WIFEXITED(ret) && !WEXITSTATUS(ret))
{
    printf("Completed successfully\n"); ///successful 
}
else
{
    printf("execution failed\n"); //error
}

As the other answers said, std::system blocks until complete anyway.正如其他答案所说,无论如何, std::system阻塞直到完成。 However, if you want to run the child process async and you are ok with boost you can use boost.process ( ref ):但是,如果您想异步运行子进程并且您可以使用 boost,则可以使用boost.process ( ref ):

#include <boost/process.hpp>

namespace bp = boost::process;

bp::child c(bp::search_path("echo"), "hello world");

std::cout << c.id() << std::endl;
// ... do something with ID ...

c.wait();

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

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