有没有办法不等待系统()命令来完成? (C语言) [英] Is there a way to not wait for a system() command to finish? (in c)

查看:486
本文介绍了有没有办法不等待系统()命令来完成? (C语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

类似于:结果
  <一href=\"http://stackoverflow.com/questions/6552712/program-not-executing-anything-after-a-call-to-system\">program系统()结果一个电话后,不执行任何
  


我是相当新的使用C,但基本上,我想执行以下命令行:

  int类型的=系统(蟒蛇-m绘图仪);

这将启动我开发了一个Python模块。不过,我想我的C程序的其他部分继续运行,而不是等待命令执行完毕(Python的应用程序是一个无限循环,所以它不会自动关闭)。有什么办法做到这一点使用C / C ++?我希望这不是一个previous问题的重职,但老实说,我不知道该怎么CAL这种行为,我发现没有在我的搜索。

更新:
该溶液是:

  int类型的=系统(启动蟒蛇-m绘图仪和放大器;);


解决方案

系统()只是简单地传递它的参数到shell(在类Unix系统中,通常是 / bin / sh的)。

试试这个:

  int类型的=系统(蟒蛇-m绘图仪和放大器;);

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

这是可能只在类Unix系统(可能包括MacOS的)工作;特别是,它可能不会在MS Windows上工作,除非你在Cygwin下运行。

在Windows上,系统()可能调用的cmd.exe ,它不接受相同的命令在类Unix系统中使用的语法。但Windows 启动命令应该做的工作:

  int类型的=系统(启动蟒蛇-m绘图仪);

只要你写Windows专用code(启动除非你碰巧有一个不会在Unix上运行在你的 $ PATH ),你可能会考虑使用一些较低级别的Windows功能,启动命令可能通过调用<一个href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx\"><$c$c>StartProcess.这是比较复杂的,但它可能给你如何在过程中执行的控制。在另一方面,如果系统()满足你的要求,你可能也使用它。

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

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

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++? I hope this is not a re-post of a previous question but I honestly do not know what to cal this behavior and i found nothing in my search.

Update: the solution was:

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

解决方案

system() simply passes its argument to the shell (on Unix-like systems, usually /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.

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.

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

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

As long as you're writing Windows-specific code (start won't work on Unix unless you happen to have a start command 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.

这篇关于有没有办法不等待系统()命令来完成? (C语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆