有没有办法不等待 system() 命令完成?(在 c)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6962156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:02:08  来源:igfitidea点击:

Is there a way to not wait for a system() command to finish? (in c)

c++csystemcallhang

提问by gh4x

Similar to:
program not executing anything after a call to system()

类似于:
程序在调用 system() 后不执行任何操作

I am fairly new to using C but basically, I want to execute the following line:

我对使用 C 相当陌生,但基本上,我想执行以下行:

int a = system("python -m plotter");

which will launch a python module I developed. However, I want the rest of my c program to keep running instead of waiting for the command to finish executing (the python app is on an infinite loop so it wont close automatically). Is there any way to do this using C/C++?

这将启动我开发的 python 模块。但是,我希望我的 c 程序的其余部分继续运行,而不是等待命令完成执行(python 应用程序处于无限循环中,因此它不会自动关闭)。有没有办法使用 C/C++ 做到这一点?

Update:the solution was:

更新:解决方案是:

int a = system("start python -m plotter &");

回答by Keith Thompson

system()simply passes its argument to the shell (on Unix-like systems, usually /bin/sh).

system()简单地将它的参数传递给 shell(在类 Unix 系统上,通常是/bin/sh)。

Try this:

尝试这个:

int a = system("python -m plotter &");

Of course the value returned by system()won't be the exit status of the python script, since it won't have finished yet.

当然返回的值system()不会是python脚本的退出状态,因为它还没有完成。

This is likely to work only on Unix-like systems (probably including MacOS); in particular, it probably won't work on MS Windows, unless you're running under Cygwin.

这可能只适用于类 Unix 系统(可能包括 MacOS);特别是,它可能无法在 MS Windows 上运行,除非您在 Cygwin 下运行。

On Windows, system()probably invokes cmd.exe, which doesn't accept commands with the same syntax used on Unix-like systems. But the Windows startcommand should do the job:

在 Windows 上,system()可能调用cmd.exe,它不接受与类 Unix 系统上使用的语法相同的命令。但是 Windowsstart命令应该可以完成这项工作:

int a = system("start python -m plotter");

As long as you're writing Windows-specific code (startwon't work on Unix unless you happen to have a startcommand in your $PATH), you might consider using some lower-level Windows feature, perhaps by calling StartProcess. That's more complicated, but it's likely to give you more control over how the process executes. On the other hand, if system()meets your requirements, you might as well use it.

只要你编写Windows的特定代码(start会不会在Unix上工作,除非你碰巧有一个start命令你$PATH),你可以考虑使用一些较低级别的Windows功能,或许通过调用StartProcess。这更复杂,但它可能会让您更好地控制流程的执行方式。另一方面,如果system()满足您的要求,您也可以使用它。

回答by love_me_some_linux

I believe if you add a '&' to the end of the command it will work. '&' tells the command to run in the background

我相信如果您在命令末尾添加“&”,它将起作用。'&' 告诉命令在后台运行

int a = system("python -m plotter &");

回答by littleadv

There's no way in the standard library, but you can fork out or create a separate thread that would run it on the background, if your OS supports it.

标准库中没有办法,但是如果您的操作系统支持,您可以分叉或创建一个单独的线程来在后台运行它。

回答by jscode

"System" command on Linux will let the rest of code execute only when it has done executing itself.

Linux 上的“系统”命令将让其余代码仅在其完成执行后才执行。

You should use fork() if you want simultaneous processing.

如果您想要同时处理,您应该使用 fork()。

回答by David Brown

The word for this is an Asynchronous function/method call. C and C++ don't have such a feature so you have to either call the function in a separate thread or use a system call that will run it in a separate process. Both of these methods tend to be platform specific, although there are cross platform threading libraries.

这个词是异步函数/方法调用。C 和 C++ 没有这样的功能,因此您必须在单独的线程中调用该函数或使用将在单独的进程中运行它的系统调用。尽管存在跨平台线程库,但这两种方法往往是特定于平台的。

I know that in unix to do this in a separate process you would use fork(2)to create a new process and exec(3)to execute a new program in that process. I do not know what system calls would be required in Windows, but should be something similar.

我知道在 unix 中要在单独的进程中执行此操作,您将使用fork(2)创建一个新进程并使用exec(3)在该进程中执行一个新程序。我不知道在 Windows 中需要什么系统调用,但应该是类似的。