Remix.run Logo
sanderjd 2 hours ago

I just ran into this recently, where I had an obscure bug caused by needing to close more file descriptors in the forked process. "I want a clone of the current process" is just way less common in my experience than "I want a completely new process". It feels crazy that we don't have a way to directly express the latter thing, and can only approximate it by cloning and then fixing things up in post.

7jjjjjjj 5 minutes ago | parent | next [-]

>It feels crazy that we don't have a way to directly express the latter thing

Isn't that what posix_spawn is for?

1718627440 an hour ago | parent | prev | next [-]

But you generally want to communicate with that process, so you do need to setup e.g. file descriptors and stuff, which needs information from the parent process to be passed.

jonhohle an hour ago | parent [-]

Most programming languages abstract this out to be able to connect or drop the 3 standard pipes. Typically this is the only thing that can be shared anyway unless the other program is specifically shared and expects other file handles to be available, in which case fork might be the right system call anyway.

dnw 2 hours ago | parent | prev | next [-]

What do you mean by "a completely new process"?

wongarsu an hour ago | parent | next [-]

The equivalent of CreateProcessW https://learn.microsoft.com/en-us/windows/win32/api/processt...

sanderjd 2 hours ago | parent | prev [-]

A process that shares nothing with the process that spawned it.

jerf an hour ago | parent | next [-]

A thing that makes that complicated is that while you want that conceptually, you don't want that in reality. For instance, if the spawning process is in a container of some sort and it spawned a process that "shares nothing with the process that spawned it", the spawned process would no longer be in that container, because the state of "being in the container" is one of the things it shares with the parent process.

This is just an example of I don't even know how many things a modern-day process will share from its parent.

By "complicated" I do not even remotely mean "unsolvable". I just mean that if you really dig down into what it means to "share nothing" in a modern operating system, it's a lot richer than it was back when fork+exec was a practical solution. There's a lot of fuzzy things that could go either way when you say "shares nothing".

JoBrad an hour ago | parent | prev [-]

That’s how you get zombie processes and memory leaks.

stabbles an hour ago | parent | prev [-]

Isn't that covered by O_CLOEXEC?

anarazel an hour ago | parent [-]

There's a bunch of nastiness around that too. If you have e.g. library state that assumes the fd still works you can get her very confusing bugs once another file is opened into that fd number...