This is actually a 2 part question.
- Why is
posix_spawn()faster than hand writtenfork()+exec()whenposix_spawn()is itself just a wrapper aroundfork()+exec()? - Why is
posix_spawn()withPOSIX_SPAWN_USEVFORKslower than normal hand writtenvfork()+exec()?
According to Ruby posix spawning library work it seems like posix_spawn() is giving consistent throughput. I read the source code of glibc, but I couldn't find anything obvious that is giving this consistency. Can someone explain it to me?
I made a small test source file (too big for here, so GitHub), and had it run about 30 times - each time invoking /bin/true several times (random number between 1000-10000) - then dividing the total time consumed by total iterations. The results are shown below. Its run on an average Linux Mint desktop with no bells and whistles attached.
I gather the man page considers vfork() to be an architectural blemish, but for my use cases, its just fine.
Here, the v prefix is using vfork() and n prefix is the normal fork().
