怎样才能一个C / C ++程序自动变为背景是什么? [英] How can a C/C++ program put itself into background?

查看:128
本文介绍了怎样才能一个C / C ++程序自动变为背景是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是对一个已经从命令行启动把自己变成了一个后台运行的C或C ++程序,相当于如果用户已经从UNIX外壳,推出最好的方式'和;'在命令的结束? (但用户没有。)这是一个GUI应用程序,不需要任何外壳的I / O,因此没有理由推出后绑起来的外壳。但我想一个shell命令启动是自动后台运行,而不'和;' (或在Windows上)。

What's the best way for a running C or C++ program that's been launched from the command line to put itself into the background, equivalent to if the user had launched from the unix shell with '&' at the end of the command? (But the user didn't.) It's a GUI app and doesn't need any shell I/O, so there's no reason to tie up the shell after launch. But I want a shell command launch to be auto-backgrounded without the '&' (or on Windows).

在理想情况下,我想一个解决方案,将在任何Linux,Mac OS X和Windows的工作。 (或者单独的解决方案,我可以选择的#ifdef)。这是确定的假设,这应该就在开始执行时进行,如在中间反对地方。

Ideally, I want a solution that would work on any of Linux, OS X, and Windows. (Or separate solutions that I can select with #ifdef.) It's ok to assume that this should be done right at the beginning of execution, as opposed to somewhere in the middle.

一个解决方案是有主程序是启动真正的二进制文件的脚本,仔细地把它放到后台。但似乎不能令人满意需要这些加上外壳/二进制对。

One solution is to have the main program be a script that launches the real binary, carefully putting it into the background. But it seems unsatisfying to need these coupled shell/binary pairs.

另一个解决方案是立即发射的另一个的执行版本(与'系统'或CreateProcess的),具有相同的命令行参数,但把在后台子,然后具有父退出。但相对于过程,这似乎把笨重的本身的成背景。

Another solution is to immediately launch another executed version (with 'system' or CreateProcess), with the same command line arguments, but putting the child in the background and then having the parent exit. But this seems clunky compared to the process putting itself into background.

几个答案后编辑:是的,一叉()(或系统(),或CreateProcess的Windows系统)是一种方式进行排序的做到这一点,我在我原来的问题暗示。但是,所有这些解决方案使得即后台运行,然后终止原处理的第二处理。我在想,如果有把现有的工艺到背景的一种方式。一个区别是,如果应用程序是从记录它的进程ID的脚本启动(也许以后杀害或其他用途),新叉或创建进程将有不同的ID,因此将不会对任何启动脚本可控的,如果你明白我在得到。

Edited after a few answers: Yes, a fork() (or system(), or CreateProcess on Windows) is one way to sort of do this, that I hinted at in my original question. But all of these solutions make a SECOND process that is backgrounded, and then terminate the original process. I was wondering if there was a way to put the EXISTING process into the background. One difference is that if the app was launched from a script that recorded its process id (perhaps for later killing or other purpose), the newly forked or created process will have a different id and so will not be controllable by any launching script, if you see what I'm getting at.

编辑#2

fork()的不是OS X,那里的'叉'的手册页说,如果正在使用某些框架或库是不安全的一个很好的解决方案。我试了一下,我的应用程序在运行时大声抱怨:这一进程已经分叉的,你不能安全地使用此功能的CoreFoundation你必须执行exec()。

fork() isn't a good solution for OS X, where the man page for 'fork' says that it's unsafe if certain frameworks or libraries are being used. I tried it, and my app complains loudly at runtime: "The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec()."

我被守护进程()很感兴趣,但是当我试图在OS X上,它给了同样的错误信息,所以我认为它只是一个叉一个奇特的包装(),并有相同的限制。

I was intrigued by daemon(), but when I tried it on OS X, it gave the same error message, so I assume that it's just a fancy wrapper for fork() and has the same restrictions.

请问OS X的中心主义,它只是恰巧在此刻我面前的制度。但我确实是在寻找一个解决方案,所有三个平台。

Excuse the OS X centrism, it just happens to be the system in front of me at the moment. But I am indeed looking for a solution to all three platforms.

推荐答案

我的建议是:不这样做,至少不会在Linux / UNIX

My advice: don't do this, at least not under Linux/UNIX.

GUI程序做传统的的自动后台自己。虽然这可能偶尔是恼人的新手,它具有许多优点:

GUI programs under Linux/UNIX traditionally do not auto-background themselves. While this may occasionally be annoying to newbies, it has a number of advantages:


  • 让您轻松捕捉标准错误核心的情况下,转储需要调试/其他问题。

  • Makes it easy to capture standard error in case of core dumps / other problems that need debugging.

很容易让一个shell脚本来运行程序,并等待,直到它完成。

Makes it easy for a shell script to run the program and wait until it's completed.

很容易让一个shell脚本在后台运行的程序并获得其进程ID:

Makes it easy for a shell script to run the program in the background and get its process id:

gui-program &
pid=$!
# do something with $pid later, such as check if the program is still running

如果你的程序本身的叉子,这种行为将中断。

If your program forks itself, this behavior will break.

脚本化是很多意想不到的情况下是有用的,即使有GUI程序,我会毫不犹豫地打破明确这些行为。

"Scriptability" is useful in so many unexpected circumstances, even with GUI programs, that I would hesitate to explicitly break these behaviors.

Windows是另一回事。据我所知,Windows程序会自动在后台运行 - 从命令shell调用,即使 - 除非他们明确地要求访问命令窗口

Windows is another story. AFAIK, Windows programs automatically run in the background--even when invoked from a command shell--unless they explicitly request access to the command window.

这篇关于怎样才能一个C / C ++程序自动变为背景是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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