posix_spawn的功能 [英] capability of `posix_spawn`

查看:520
本文介绍了posix_spawn的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小程序,它有点像是一台服务器,它会在本地(而不是通过网络)生成客户端程序,并在它们之间做一些有趣的事情.虽然我使用的主要操作系统是Linux,但我希望它可以在其他操作系统(包括Windows)上运行.有forkexec可以完成工作,但是当我通过cygwin将程序移植到Windows时,我不希望在cygwin中实现的that脚的fork踢进来,而实际上调用CreateProcess并进行复制使用带有共享内存互斥锁的setjmp/longjmp,将当前进程的内存区域转移到新进程,所有这些都将被另一个程序(exec)替换.在阅读cygwin的FAQ页面时,我发现了spawn.h及其posix_spawn,基本上在Windows中看起来像CreatePorcess(Ex).似乎是一项新功能(...我的意思不是原始UNIX系统功能之一),所以对此我有一些疑问.

I'm writing a small program which is somewhat a server that spawns its client programs (locally, not over the network) and do interesting things between them. While my primary OS in use is Linux, I expect this to run on other OSes including Windows. There is fork and exec which does the job, but when I port the program to Windows via cygwin, I don't want that crappy fork implemented in cygwin to kick in, which actually calls CreateProcess and copies the current process's memory area to the new process using setjmp/longjmp with shared memory mutexes, all of this in order to be replaced with another program (exec). While reading the FAQ page of cygwin I discovered spawn.h and its posix_spawn, which basically looks like CreatePorcess(Ex) in windows. It seems like a new feature (... I mean not one of the original UNIX system functions), so I have some questions upon it.

  1. 实施范围广泛吗? (我在互联网上看到一些帖子,该帖子未在他/她的系统下定义.)

  1. Is it implemented well, widely? (I saw some posts on the internet that it is not defined under his/her system.)

我可以期望在Linux中使用posix_spawn代替fork/exec来改善或降低性能吗?

Can I expect any performance improvement or deterioration by using posix_spawn instead of fork/exec in Linux?

为什么自1999年以来,posix_spawn的知名度和使用率却低于fork/exec?

Why is posix_spawn less known and less used than fork/exec although being standard since 1999?

推荐答案

posix_spawn尚未广泛实施,但是无论如何都应该使用它.

posix_spawn is not widely implemented, but you should use it when you can anyway.

  • 如果平台缺少它,那么使用只执行fork-and-exec的直接替换总是很容易的.

  • If a platform lacks it, it's always easy to use a drop-in replacement that just does fork-and-exec.

如果可以使用本机"/最佳实现(作为系统的一部分,或者是特定于平台的嵌入式平台),则对于fork昂贵的系统将有很大的不同,对于根本无法执行fork的系统(无MMU的系统,没有cygwin之类的Windows等),甚至还有更多的区别.

If a "native"/optimal implementation is available (either as part of the system, or a drop-in platform-specific one), it will make a big difference for systems where fork is expensive, and even more of a difference for systems that can't do fork at all (MMU-less systems, Windows without something like cygwin, etc.).

即使在fork是本机且快速"的系统上,如果父级具有许多虚拟映射,它仍然可能相当慢,即使对于小型父级,它也比最佳posix_spawn稍慢.请参阅此Twitter主题以获取一些发现: https://twitter.com/RichFelker/status/602313644026761216

Even on systems where fork is native and "fast", it can still be rather slow if the parent has lots of virtual mappings, and it's mildly slower than an optimal posix_spawn even for a small parent. See this Twitter thread for some findings: https://twitter.com/RichFelker/status/602313644026761216

关于为什么它没有被更广泛地使用/实现,可能是由于它不为人所知,以及相当笨拙的,无偿的面向对象的API设计的结合.

As to why it's not more widely used/implemented, it's probably a mix of not being widely known, and the rather klunky, gratuitously object-oriented API design.

这篇关于posix_spawn的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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