简体   繁体   中英

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. The first solution that pops up in mind and on Google is to call system().

What does the stackoverflow community suggest?

  1. Shall I use system() in a pthread?

  2. Or shall I myself just fork and exec in pthread? But since pthread is a thread, is it advisable to do a fork() and exec() in pthread thread?

  3. Or what is the better approach than the above?

Start with system call in another thread and only add complexity when needed.

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).

  1. You shouldn't use system for this, but not because it's expensive. (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. If you use fork and execvp instead, you don't have to worry about quoting anything.

  2. The problems associated with mixing fork and wait with threads are real, but they are tractable. If your OS has posix_spawn , it will take care of some of those problems for you. 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. I cannot fit a comprehensive guide to mixing fork and wait with threads into this answer box.

  3. An alternative you should consider is compressing the data in the thread that would be waiting for the gzip process, using zlib . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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