简体   繁体   English

在pthread中运行shell命令的最佳方法是什么?

[英]What is a good and optimized way to run shell command in a pthread?

Basically, I want to compress a file in a pthread thread using gzip. 基本上,我想使用gzip在pthread线程中压缩文件。 The first solution that pops up in mind and on Google is to call system(). 在Google上出现的第一个解决方案是调用system()。

What does the stackoverflow community suggest? stackoverflow社区建议什么?

  1. Shall I use system() in a pthread? 我应该在pthread中使用system()吗?

  2. Or shall I myself just fork and exec in pthread? 还是我自己只是在pthread中执行并执行? But since pthread is a thread, is it advisable to do a fork() and exec() in pthread thread? 但是由于pthread是线程,是否建议在pthread线程中进行fork()和exec()?

  3. Or what is the better approach than the above? 还是比上面更好的方法?

Start with system call in another thread and only add complexity when needed. 从另一个线程中的system调用开始,仅在需要时才增加复杂性。

The extra complexity of doing fork / exec or using a zip library is only worth the effort if system is not sufficient for some reason (ie you want to redirect both stdin and stdout of the child process into your parent process, or your want to compress a file in memory for sending it over the network without writing new files). 仅当system由于某些原因不够用时(例如,您要将子进程的stdinstdout都重定向到父进程中,或者想要压缩),执行fork / exec或使用zip库的额外复杂性才值得付出努力。内存中的文件,无需编写新文件即可通过网络发送该文件)。

  1. You shouldn't use system for this, but not because it's expensive. 您不应该为此使用system ,但不要因为它昂贵。 (For any file that is worth bothering to compress, the overhead of any technique for invoking a background gzip compression is negligible relative to the cost of doing the compression itself.) The reason you shouldn't use system is, system invokes a shell, and that means you have to worry about quoting the arguments. (对于任何值得压缩的文件,调用后台gzip压缩的任何技术的开销相对于执行压缩本身的成本而言都是微不足道的。)不应使用system的原因是, system调用shell,这意味着您必须担心引用参数。 If you use fork and execvp instead, you don't have to worry about quoting anything. 如果改用forkexecvp ,则不必担心引用任何内容。

  2. The problems associated with mixing fork and wait with threads are real, but they are tractable. 与混合forkwait线程相关的问题是真实的,但它们很容易解决。 If your OS has posix_spawn , it will take care of some of those problems for you. 如果您的操作系统具有posix_spawn ,它将为您解决其中的一些问题。 I don't normally recommend posix_spawn because code that uses it is, in general, harder to maintain than code that uses fork , but for this application it should be OK. 我通常不推荐posix_spawn因为使用它的代码通常比使用fork代码更难维护,但是对于此应用程序来说应该可以。 I cannot fit a comprehensive guide to mixing fork and wait with threads into this answer box. 我无法提供综合指南来混合forkwait线程进入此答案框。

  3. An alternative you should consider is compressing the data in the thread that would be waiting for the gzip process, using zlib . 您应该考虑的替代方法是使用zlib在等待gzip进程的线程中压缩数据。 This avoids the problems of mixing fork and wait with threads, but it adds a library dependency to your program, which may not be as convenient as relying on an external gzip executable. 这避免了混合的问题, fork ,并wait与线程,但它增加了一个库依赖于你的程序,这可能不一样便利依赖于外部gzip可执行文件。

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

相关问题 在C ++中对算法的内存使用情况进行实验的一种好方法是什么? - what is a good way to run experiments for the memory usage of an algorithm in C++? 在C ++中执行复杂的shell命令的正确方法是什么? - What is the proper way to execute a complex shell command within C++? 将MFC gui添加到Win32 C ++命令行应用程序的好方法是什么? - What is a good way to add MFC gui to a Win32 C++ command line application? 验证输入的好方法是什么? - What is a good way to validate input? 什么是存储颜色的好方法? - What is a good way to store color? 从C ++中已执行的Shell命令获取返回状态的安全方法是什么? - What is the safe way of get the return status from an executed shell command in c++ 什么是优秀的C / C ++算法,用于通过抖动将24位位图转换为16位? - What is a good, optimized C/C++ algorithm for converting a 24-bit bitmap to 16-bit with dithering? 命令与shell一起运行,但卡在QProcess中 - Command run with shell but get stuck with QProcess C++ 在同一个 shell 实例上运行 shell 命令 - C++ Run a shell command on the same shell instance 将多个客户端连接到服务器的好方法是什么? - What is a good way to connect multiple clients to the server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM