【问题标题】:execl in a detached thread - killing the process (c++ std 11)在分离的线程中执行 - 杀死进程(c++ std 11)
【发布时间】:2014-02-28 02:54:35
【问题描述】:

考虑一下

#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::cout 行,因为没有命令中断并跳转到 while 结束来庆祝打印消息之前的终止。

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

编辑:(感谢 minitech)我不想让原始程序死掉

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

【问题讨论】:

  • 我不知道你在这里问什么。您能否展示一下您得到的输出以及您的期望?

标签: c++ multithreading exec


【解决方案1】:

来自execl's documentation

exec() 系列函数将当前进程映像替换为 一个新的过程映像。本手册页中描述的功能是 execve(2) 的前端。 (请参阅 execve(2) 的手册页 有关替换当前过程映像的更多详细信息。)

如果您不希望原始程序死掉,您应该始终在调用 execl 之前调用 fork。

【讨论】:

  • 并非总是如此,但几乎总是如此。不首先调用 fork() 的原因往往是奇怪的情况,例如 SUID 包装程序需要在运行另一个程序之前调整进程环境中的某些内容(尽管这通常会使用 execv() 或 execve() - 而不是通常是execvp()——因为它可能会将用户的参数传递给已执行的程序。但是,首先近似地,“总是”会这样做。而且exec*() 终止进程中的所有线程是绝对正确的。跨度>
  • 刚刚编辑了答案,谢谢你的叫醒:)
  • 谢谢你,你是个英雄——在我这边真是个愚蠢的错误,grr
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多