从多线程程序中调用system() [英] Calling system() from multithreaded program

查看:605
本文介绍了从多线程程序中调用system()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一个用C ++编写的消耗内存的多线程应用程序. 我们必须执行许多shellscript/linux命令(并获取返回代码).

We are working on a multithreaded memory-consuming application written in C++. We have to execute lots of shellscript/linux commands (and get the return code).

在阅读了文章之后,我们清楚地看到了知道在我们的上下文中使用system()是个坏主意.

After reading that article we clearly understood that it would be a bad idea to use system() in our context.

一种解决方案是在程序启动之后并且在创建任何线程之前进行派生 但是与该过程的通信可能并不容易(套接字,管道?).

A solution would be to fork after program starts and before creating any threads but the communication with that process may not be easy (socket, pipe ?).

我们考虑的第二个解决方案可能包括一个专用的用Python(使用xinetd?)编写的守护程序,该守护程序可以处理我们的系统调用.

The second solution we considered may consist of a dedicated deamon written in python (using xinetd ?) that would be able to process our system calls.

您遇到过这个问题吗?您是如何解决的?

Have you ever had that problem? How did you solve it?

注意: 这里是解释此问题的更完整的文章: http://developers.sun. com/solaris/articles/subprocess/subprocess.html 他们建议使用posix_spawn,它使用vfork()而不是fork()(在system()中使用).

Note : Here is a more complete article that explain this problem : http://developers.sun.com/solaris/articles/subprocess/subprocess.html They recommend to use posix_spawn which uses vfork() instead of fork() (used in system()).

推荐答案

所链接的文章主要讨论的是如果您使用fork()而不立即使用exec *()跟踪问题.由于system()通常将由fork()后跟exec()来实现,因此大多数问题都不适用.但是,确实有一个问题是关于关闭文件描述符的要点.除非有特殊原因,否则默认情况下使用O_CLOEXEC打开文件可能是一个很好的经验法则.

The article you link to mostly talks about issues if you fork() and don't immediately follow it with an exec*(). As system() typically would be implemented by a fork() followed by exec() most of the issues don't apply. One issue which does apply is the point about closing file descriptors, though; Unless you have specific reasons to do otherwise, opening files with O_CLOEXEC by default is probably a good rule of thumb.

大型内存消耗应用程序的fork()+ exec()的一个问题是,如果操作系统配置为不允许内存过量使用,则fork()可能会失败.一种解决方案是在开始在主进程中分配大量内存之前,派生一个外部进程处理程序"进程.

One issue with fork()+exec() of large memory consuming applications is that if your OS is configured to not allow memory overcommit, the fork() may fail. One solution to this is to fork an "external process handler" process before you start allocating a lot of memory in your main process.

最好的解决方案是,如果您需要的功能作为库提供,而无需首先进行派生.不过,这可能不会在短期内使您心动.

The best solution is if the functionality you require is available as a library, obviating the need to fork in the first place. That probably doesn't warm your heart in the short term, though.

这篇关于从多线程程序中调用system()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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