coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using posix_spawn in coreutils?


From: Collin Funk
Subject: Using posix_spawn in coreutils?
Date: Sun, 19 Oct 2025 00:08:29 -0700
User-agent: Gnus/5.13 (Gnus v5.13)

I was curious about the performance of posix_spawn, so I wrote a test
program to experiment with, attached to this message.

Here is my testing:

     # Using fork + execvp to execute 'true' 10000 times.
     $ gcc main.c
     $ perf stat --repeat 5 ./a.out 2>&1 | grep -F 'seconds time elapsed'
            5.8890 +- 0.0761 seconds time elapsed  ( +-  1.29% )

     # Using vfork + execvp
     $ gcc -DFORK=vfork main.c
     $ perf stat --repeat 5 ./a.out 2>&1 | grep -F 'seconds time elapsed'
            4.4907 +- 0.0596 seconds time elapsed  ( +-  1.33% )

     # Using posix_spawnp
     $ gcc -DUSE_POSIX_SPAWN=1 main.c
     $ perf stat --repeat 5 ./a.out 2>&1 | grep -F 'seconds time elapsed'
            4.6260 +- 0.0508 seconds time elapsed  ( +-  1.10% )

It seems like this is due to the different clone flags:

     # fork + execvp
     clone(child_stack=NULL, 
flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, 
child_tidptr=0x7ff8b35f6a10)

     # posix_spawnp
     clone3({flags=CLONE_VM|CLONE_VFORK|CLONE_CLEAR_SIGHAND, 
exit_signal=SIGCHLD, stack=0x7f9978d00000, stack_size=0x9000}

The vfork man-page says that it uses "CLONE_VM | CLONE_VFORK | SIGCHLD"
[1].

How about using posix_spawn in coreutils where possible (that is, not
'env' which does not fork)?

The micro-optimization could add up for for 'install -s'. It would also
help port to Windows, which does not have fork and vfork. The code in
src/split.c also looks like a good candidate for
posix_spawn_file_actions_adddup2, etc. [2].

Collin

[1] https://man7.org/linux/man-pages/man2/vfork.2.html
[2] https://pubs.opengroup.org/onlinepubs/9699919799/

Attachment: main.c
Description: Text document


reply via email to

[Prev in Thread] Current Thread [Next in Thread]