News for hackers. Stuff that matters.
Re:Poor child process management in Rust terminal apps leads... by koakuma-chan on Wednesday July 30, 2025 @08:40PM (#44741186)
Are you saying that after the main process has exited, child processes can still run and write to stdout/stderr?
Re:Poor child process management in Rust terminal apps leads... by CGamesPlay on Wednesday July 30, 2025 @11:25PM (#44742046)
Child processes are created using, generally, 2 syscalls: fork, then exec. When you fork, all file descriptors the main process has open are copied, and are now open in two places. Then, when the child calls exec (to transform itself into the target program), all file descriptors stay open in the new process (unless a specific fd is explicitly configured otherwise, FD_CLOEXEC).

Standard output are just file descriptors with the number 0, 1, and 2, and you can use the dup2 syscall to assign those numbers to some pipes that you originally created before you fork. Now the standard output of your child process is going to those pipes in your parent process. Or you can close those file descriptors, which will prevent the child process from reading/writing them at all. Or you can do nothing, and the copied file descriptors from the parent still apply.

Conceptually, you think of "spawning a child" as something that is in some kind of container (the parent process), but the underlying mechanics are not like this at all, and processes don't actually exist in a "tree", they just happen to keep a record of their "parent process ID" so the OS knows who to notify when the process dies.

Re:Poor child process management in Rust terminal apps leads... by burnt-resistor on Wednesday July 30, 2025 @11:51PM (#44742162)
fork() when followed by exec*() is generally inefficient. That's why vfork(), clone(), and clone3() exist. There's no point in duplicating (even CoW) the entire kernel side and libc internal state of a process if it's going to be replaced with exec*() by a new, unrelated process.
Re:Poor child process management in Rust terminal apps leads... by zokier on Thursday July 31, 2025 @03:15AM (#44743212)
> Conceptually, you think of "spawning a child" as something that is in some kind of container (the parent process), but the underlying mechanics are not like this at all,

That is not quite right either, the newly created child processes generally go to the same process group as the parent, the process groups (and sessions) forming those "containers".

Tbh this is one of the many murky areas of UNIX.

Re:Poor child process management in Rust terminal apps leads... by CGamesPlay on Thursday July 31, 2025 @06:57AM (#44744330)
My explanation definitely glosses over some details, but a process group isn't really a "container" in any meaningful sense, either. It can be left by any member (who can form a new process group completely unrelated to its old one, setsid), which resets the "controlling terminal" but isn't related to the standard output channels at all.
Re:Poor child process management in Rust terminal apps leads... by duped on Wednesday July 30, 2025 @11:06PM (#44741973)
Yes, this is easy to test too:

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    int main() {
        pid_t pid = fork();
        if (pid == 0) {
            sleep(1);
            printf("after parent died!\n");
            return 0;
        }
        return 0;
    }
You'll see the message printed out 1 second after the process ends.
Re:Poor child process management in Rust terminal apps leads... by cwillu on Wednesday July 30, 2025 @11:44PM (#44742125)
fd's don't inherently belong to a process, and indeed there are several ways of asking the kernel to pass an fd to another process.

stdin/out/err is just a shorthand (well, longhand) for the numbers 0, 1 and 2. There's nothing special about them except by convention, and forgetting that can cause hilarious bugs.

Re:Poor child process management in Rust terminal apps leads... by Callicles on Wednesday July 30, 2025 @08:46PM (#44741227)
I believe I am saying child processes can write to stdout as the main process is shutting down. Also, if the child processes are not shut down properly and are left dangling, and the child processes were set up as 'inherit' to be able to write directly to stdout/stderr then yes.