0

If we want to fork anything from inside a bash script (or even at the command line), we can simply surround it with $() or back-ticks. For example;

echo $(ls) - this will fork the current environment and run ls within the (forked) subshell

How can we use vfork (http://man7.org/linux/man-pages/man2/vfork.2.html) instead of the standard fork functionality?

1
  • 1
    Do you want the functionality of the parent blocking, or do you actually want to use the vfork function itself. If you're really concerned about performance benefits you should probably not be using any sort of shell as your development language Jul 27, 2017 at 11:41

1 Answer 1

0

Doing a grep -Ri vfork in the bash source code doesn't find any hits, so there's no way to directly use vfork from bash.

If you just want something that will invoke a child and block the parent, well, your subshell example already does that. Otherwise echo would likely get at best incomplete results.

You can also run a command in the background with & and then use wait to have the parent block until the child finishes.

2
  • Yes all good thoughts/comments. We have a script which does A LOT of forking and we want to make it faster. Hence the looking into vfork. Surely there must be a way to somehow call vfork instead of fork, even if we have to write a little C wrapper or something. It would be good though to be able to easily change the actual fork call into something else - in pseudo code for example; vork=true; echo $(ls) Jul 27, 2017 at 11:57
  • If performance is an issue, shell scripting is probably not the right solution. If there's just one performance critical part you might write that in something fast like C and then you can call that from bash, but bash is not really built for speed Jul 27, 2017 at 12:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .