Why no `fork()` in `std::process`?

The discussion at Pre-RFC overhaul Command for 2021 ed made me think of a question that's been bugging me for a while now; why isn't there a fork() function in std::process? I know that there has to be a good reason, but I have no idea what it is...

1 Like

It's OS specific and it seems potentially very unsafe, particularly in multi-threaded programs. See fork(2) and signal-safety(7).

1 Like

@chrisd Thank you for the quick reply.

I see what you mean now, I didn't realize it was such a problem.

Also see the discussion around before_exec, which was a safe way to execute code in the forked process, and was deprecated in favor of an unsafe function:

2 Likes

See also https://www.microsoft.com/en-us/research/uploads/prod/2019/04/fork-hotos19.pdf

2 Likes

Also, you can easily call libc::fork(), if pre_exec isn't sufficient for your use case.

1 Like

No, no particular use case at this time; I didn't mean to imply that it should be added, I knew that this group would have already considered it, and so the fact that it wasn't available was for a good reason. I just didn't know what that reason was. Now I do. Thank you all for clarifying the situation for me.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.