简体   繁体   English

C ++启动新进程并在linux下终止当前的进程

[英]C++ start new process and terminate the current one under linux

This question has been asked several times here, but I can't find answer for the following situation: 这里有几次问这个问题,但我找不到以下情况的答案:

My program uses statically linked libraries, which open file handles, so, I'm unable to set FD_CLOEXEC on those file handles 我的程序使用静态链接库,打开文件句柄,因此,我无法在这些文件句柄上设置FD_CLOEXEC

simply calling exec causes alot of errors in new process, because of unavailable file handles 简单地调用exec会导致新进程中出现很多错误,因为文件句柄不可用

Basically I need: 基本上我需要:
1. spawn new process without blocking current one 1.生成新进程而不阻塞当前进程
2. terminate current process (close all handles) 2.终止当前进程(关闭所有句柄)

Can I do it on linux? 我可以在linux上做吗?

Closing all filedescriptors should be as simple as 关闭所有文件描述符应该很简单

#include <unistd.h>

for (i=getdtablesize();i>=0;--i) 
     close(i); /* close all descriptors */

This is also a standard step during daemonizing, see eg http://www.enderunix.org/docs/eng/daemon.php 这也是守护进程中的标准步骤,例如http://www.enderunix.org/docs/eng/daemon.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM