问题描述
我工作的音频连接codeR CGI脚本,利用了libmp3lame。
我正在写在C / C ++的混合物。
I'm working on an audio encoder cgi script that utilises libmp3lame.I'm writing in a mixture of C/C++.
我计划有一个入口点的CGI,可以产卵,在后台运行多种编码过程。我需要的编码过程是异步的编码可能需要几个小时,但我需要的入口点CGI立即返回,以便浏览器可以继续对其业务。
I plan to have an entry-point cgi that can spawn multiple encoding processes that run in the background. I need the encoding processes to be asynchronous as encoding can take several hours but I need the entry-point cgi to return instantly so the browser can continue about its business.
我发现几个方案来解决这个(些完整/有些没有),但还有几件事情,我想澄清。
I have found several solutions for this (some complete/ some not) but there are still a few things I'd like to clear up.
解决方法1(简单):入口点CGI是一个bash脚本,然后可以通过输出发送到在后台运行一个C ++程序CGI的/ dev / null的/ 2 /&安培;> 1安培; (simples!但不是很优雅)。
Solution 1 (easiest): The entry-point cgi is a bash script which can then run a C++ process cgi in the background by sending the output to /dev/null/ 2/&>1& (simples! but not very elegant).
解决方案2:就像解决方案1,除了入口点cgi是C ++和使用系统()运行在proc / s,并输出发送到/ dev / null的/ 2 /&安培;> 1安培;试。
Solution 2: Much like solution 1, except the entry-point cgi is in C++ and uses system() to run the proc/s and send the output to /dev/null/ 2/&>1& again.
[提问]这工作很好,但我不知道,如果共享托管公司允许使用系统()函数。这样的话?
[question] This works well but I'm not sure if shared hosting companies allow use of the system() function. Is this the case?
解决方案3(不完全):我看着使用fork()的/在pthread_create()来派生独立的线程,这似乎更优雅,我可以留在C的领域唯一的问题存在:似乎父线程也不会退出,直到所有子线程回来了。
Solution 3 (incomplete): I've looked into using fork()/pthread_create() to spawn separate threads which seems more elegant as I can stay in the realms of C. The only problem being: It seems that the parent thread doesn't exit until all child threads have returned.
[提问]请问有什么办法让父线程退出,同时让子线程继续在后台运行。
[question] Is there any way to get the parent thread to exit whilst allowing child threads to continue in the background.
[思路]也许我可以在孩子PROC / s的输出发送到黑洞!我可以简单地重定向标准输出到/ dev / null的。如果是这样,我怎么做呢?
[idea] Maybe I can send the child proc/s output to the black hole! Can I simply redirect stdout to /dev/null. If so, how do I do this?
我希望这是有道理的人。我还是有点用C的东西一noob,所以我可能会丢失非常基本的概念(请怜悯!)。
I hope this makes sense to someone. I'm still a bit of a noob with C stuff so I may be missing very basic concepts (please have mercy!).
我会非常感激在这个问题上任何意见的。
I'd be very grateful of any advise on this matter.
在事先非常感谢,
乔希
推荐答案
您可能希望标准的Unix守护技术,涉及双叉:
You probably want the standard Unix daemon technique, involving a double fork:
void daemonize(void)
{
if (fork()) exit(0); // fork. parent exits.
setsid(); // become process group leader
if (fork()) _exit(0); // second parent exits.
chdir("/"); // just so we don't mysteriously prevent fs unmounts later
close(0); // close stdin, stdout, stderr.
close(1);
close(2);
}
看起来像现代的Linux机器有守护进程()
库函数presumably做同样的事情。
Looks like modern Linux machines have a daemon()
library function that presumably does the same thing.
这有可能是第一个退出
应 _exit
,但这code一直为我工作
It's possible that the first exit
should be _exit
, but this code has always worked for me.
这篇关于运行C ++ CGI脚本作为后台进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!