3 ms·
posix_spawn (at least in GNU/Linux) is a library function, not a syscall. Under the hood, it calls clone (which, like vfork, avoids copying page tables) and the
by umanwizard 7y ago
posix_spawn (at least in GNU/Linux) is a library function, not a syscall. Under the hood, it calls clone (which, like vfork, avoids copying page tables) and then execve.
The reason fork (or vfork, or clone) and exec need to be separate steps is because you may want to do some stuff in between the two steps. The most obvious example is redirection: when you run "grep foo < bar", the shell needs to set stdin to point to "bar" before it execs "grep".
- mehrdadn 7y agoThanks but why don't people just use the portable posix_spawn when they can (which should be the vast majority of the time)? That it calls clone/execve underneath doesn't really answer my question...
- umanwizard 7y agoAs far as I know, there isn't any good reason not to. So indeed people should, if they can. (IIRC, posix_spawn was introduced relatively recently, so most old software wouldn't use it).
- manwe150 7y agoI was trying to find a year on it and couldn’t, but agree it seems newer. FWIW, it’s not required to be able to run any setup code in the child to do arbitrary setup. For instance, WinNT makes that very hard, but has a larger syscall interface to compensate, so it’s actually more general. An alternate design of running all code in the parent can be made work if the kernel supports passing a pid handle to every syscall: https://lwn.net/Articles/360747/ https://lwn.net/Articles/360747/ On the other hand, this is perhaps a bit more complex in some ways? or at least just different.
- cesarb 7y ago> I was trying to find a year on it and couldn’t, but agree it seems newer. http://man7.org/linux/man-pages/man3/posix_spawn.3.html#CONFORMING_TO http://man7.org/linux/man-pages/man3/posix_spawn.3.html#CONF... says POSIX.1-2001, so I'm guessing between 1998 and 2002 (according to https://en.wikipedia.org/wiki/Single_UNIX_Specification#2001:_Single_UNIX_Specification_version_3,_POSIX:2001 https://en.wikipedia.org/wiki/Single_UNIX_Specification#2001... it started development in 1998 and was released in 2002). That man page also says "since glibc 2.2", which according to Wikipedia was released November 2000.
- chrisseaton 7y agoSeems newer? Fork predates the whole idea of POSIX by decades, let alone the particular API posix_spawn.