简体   繁体   English

同时运行两个程序

[英]Running two programs concurrently

I have two C++ programs built in Ubuntu, and I want to run them concurrently. 我有两个用Ubuntu构建的C ++程序,我想同时运行它们。 I do not want to combine them into one C++ project and run each on a different thread, as this is causing me all sorts of problems. 我不想将它们组合成一个C ++项目并在不同的线程上运行它们,因为这会给我带来各种各样的问题。

The solution I effectively want to emulate, is when I open two tabs in the terminal, and run each program in a separate tab. 我有效地想要模拟的解决方案是,当我在终端中打开两个选项卡,并在单独的选项卡中运行每个程序。 However, I also want one program (let's call this Program A) to be able to quit and rerun the other program (Program B). 但是,我还想要一个程序(让我们称之为程序A)能够退出并重新运行其他程序(程序B)。 This cannot be achieved just in the terminal. 这不能仅在终端中实现。

So what I want to do is to write some C++ code in Program A, which can run and quit Program B at any point. 所以我想要做的是在程序A中编写一些C ++代码,它可以在任何时候运行和退出程序B. Both programs must run concurrently, so that Program A doesn't have to wait until Program B returns before continuing on with Program A. 两个程序必须同时运行,因此程序A不必等到程序B返回后再继续程序A.

Any ideas? 有任何想法吗? Thanks! 谢谢!

You have multiple options here: 这里有多个选项:

  1. The traditional POSIX fork / exec (there are literally tons of examples on how to do this in SO, for example this one). 传统的POSIX fork / exec (在SO中有很多关于如何做到这一点的例子,例如这个 )。
  2. If you can use Boost then Boost process is an option. 如果您可以使用Boost,那么Boost流程是一个选项。
  3. If you can use Qt then QProcess is an option. 如果你可以使用Qt,那么QProcess是一个选项。

Boost and Qt also provide nice means manipulating the standard input/output of the child process if this is important. 如果这很重要,Boost和Qt也提供了很好的方法来操纵子进程的标准输入/输出。 If not the classical POSIX means should do fine. 如果不是经典的POSIX意味着应该做得很好。

In Linux you can fork the current process, which creates a new process. 在Linux中,您可以fork当前进程,从而创建一个新进程。 Then you have to launch the new process with some exec system call. 然后你必须使用一些exec系统调用启动新进程。

Refer to: http://man7.org/linux/man-pages/man2/execve.2.html 请参阅: http//man7.org/linux/man-pages/man2/execve.2.html

For example: 例如:

#include <unistd.h> /* for fork */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for wait */

int main(int argc,char** argv)
{
    pid_t pid=fork();
    if (pid==0)
    {
      execv("/bin/echo",argv);
    }
}

Take a look at the Linux operating system calls, fork() and exec() . 看看Linux操作系统调用, fork()exec() The fork() call will create two copies of the current process which continue to execute simultaneously. fork()调用将创建当前进程的两个副本,这些副本将继续同时执行。

  • In the parent process, fork() 's return value is the PID (process ID) of the child process. 在父进程中, fork()的返回值是子进程的PID(进程ID)。
  • In the child process, fork() 's return value is 0. 在子进程中, fork()的返回值为0。
  • On error, fork() 's return value is -1. 出错时, fork()的返回值为-1。

You can use this to your advantage to control the behavior of the parent and child. 您可以利用此优势来控制父级和子级的行为。 As an example: 举个例子:

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

int main(int argc,char** argv)
{
    char* progB = "/bin/progB";
    char* args[progName, "arg1", "arg2", ..., NULL];
    char* env[NULL]; // can fill in environment here.

    pid_t pid=fork();
    if (pid==0)
    {
        // In child...
        execv(progB, args, env);
    }
    else if (pid == -1) 
    {
        // handle error...
    } 
    else 
    {
        // In parent; pid is the child process.
        // can wait for child or kill child here.
    }
}

To wait until your child exits (in the third case above), you can use wait(2) , which returns your child pid on successful termination or -1 on error: 要等到你的孩子退出(在上面的第三种情况下),你可以使用wait(2) ,它会在成功终止时返回你的孩子pid,或者在出错时返回-1:

pid_t result = waitpid(pid, &status, options);

To kill your child preemptively, you can send a kill signal as described in kill(2) : 为了先发制人地杀死你的孩子,你可以按照kill(2)中的描述发送一个kill信号:

int result = kill(pid, SIGKILL); // or whatever signal you wish

This should allow you to manage your processes as described in the original question. 这应该允许您按照原始问题中的描述管理您的流程。

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

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