1

I'm toying around with a side project where I want to have multiple processes share the same listening socket. In the linux documentation I read that posix_spawn will retain the filedescriptors so it should work perfectly; but I'm unsure on how this sharing is actually done.

Passing the filedescriptor as an argument feels hacky in a way, so does hardcoding the value of the FD even though it'll likely by the same its just not correct.

Q: How does one correctly use file descriptor sharing in child processes? All the results I find is how to prevent it, but in this case its desired behaviour :-)

Sample code I'm playing with:

let pathToExecutable = CommandLine.arguments[0]

// CLI Options parsing
let options = IgniteOptions.parseOrExit()

// For now the child does nothing.
if (options.isChild) {
    print("Hello World from child process!")
    exit(0)
}

// Creates and binds a TCP socket to the given host / port
let socket = Socket(host: "0.0.0.0", port: options.port)

// Calls posix_spawn in a swift friendly manner (only transforms the params really)
let pid = try spawn(pathToExecutable, args: [pathToExecutable, "-d"])
print("PID of child: \(pid)")

0 Answers0