2014-02-28 35 views
0

考虑这个EXECL中分离线程 - 查杀方法(C++ STD 11)

#include <cstdio> 
#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <cstdlib> 
#include <vector> 
#include <string> 
#include <iterator> 
#include <cmath> 
#include <thread> 
#include <cerrno> 
#include <cstring> 

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <getopt.h> 

#include <boost/algorithm/string.hpp> 

#include "utils.cpp" 
#include "pstream.h" 
//pstream is the process stream library as below 
void doThings() 
{ 
while(not exitflag, a global){ 
if(some condition) {execl (a bash script); std:cout << "this text will never get printed, process ends at execl" <<endl;} 
}}} 

int main() 
{//detach doThings as a thread here 

} 

我可以保证的是,全球exitflag没有设置,也将被印在std ::法院线,甚至标志被设置,因为在打印信息之前,没有命令可以中断并跳到结尾,以便解除终止。

我做错了什么?顺便说一句:shell脚本运行完美。

编辑:(感谢MiniTech移动)我不想原单程序死

并行线程:http://sourceforge.net/projects/pstreams/files/pstreams/Release%200.8.x/

+0

我不知道你在问什么。你能否展示你得到的结果和你期望的结果? – Ryan

回答

4

execl's documentation

的执行exec()函数系列替换当前的进程图像 一个新的过程映像。本手册页中描述的功能为execve(2)的前端 。 (参见execve的手册页(2) 关于更换当前的进程映像的进一步的细节。)

你应该总是电话调用fork之前execl如果你不想原来的程序死。

+2

并不总是,但几乎总是如此。首先不调用fork()的原因往往是古怪的情况,就像SUID包装程序,在运行另一个程序之前需要在进程的环境中调整某些东西(尽管这通常使用'execv()'或'execve ()' - 而不是'execvp()' - 因为它可能会将用户的参数传递给执行的程序,但是,第一个近似值,'always'将会执行,而且你完全正确的是exec *( )'终止一个进程中的所有线程 –

+0

刚刚编辑答案,谢谢你的唤醒电话:) –

+0

NP - 我不能给你第二个票,我害怕。 –