#Ideomatic ways to `fork` and communicate from child to parent

1 messages · Page 1 of 1 (latest)

grizzled orchid
#

I want to create a child process with fork what can communicate with its parent. I'm using a Zig 0.16 nightly (0.16.0-dev.1976+8e091047b to be precise). What I see is that there is std.posix.fork and std.os.linux.pipe. I have a couple of questions:

  1. Is calling fork the best way to fork right now, or is ther a nicer wrapper somewhere in std that I didn't see?
  2. pipe returns a file descriptor that I can use with std.posix.read and std.posix.write. Is there a way to warp this into an std.Io.File somehow to get the nicer API?
  3. More generally, is a pipe the best way to communicate from child to parent?

I need communication only from child to parent and I want to sent arbitrary bytes. I guess I need to implement some kind of framing (e.g. headers telling me the size of the payload etc.), or are better means or a library that I could use?

Thank you! I've done some searching but I haven't found anything conclusive yet, so I'm asking here.

plain tendon
#

the idiomatic way would be not to fork, since thats posix specific, rather use an Io instance to get the executablePath then call std.process.spawn, that will return a Child which will contain the Io.Files of its stdio which you can then use the nicer api of Reader and Writer

grizzled orchid
#

But it is not a different executable I want to start. Neither do I want to start "myself" with different args. The idea is that I set up some state that both parent and child share before the fork, and then fork. Achieving the same with creating a std.process.Child of myself with special args seems very cumbersome

#

With your idea, I'd have to either start a different executable or start the same executable but have it start again from main. This is now what I'm looking for :/

plain tendon
#

there is nothing stopping you from using fork, its just not idiomatic, especially on master, as the Io interface is the major api for interacting with the OS. It doesnt support fork because its not supported on all of the big platforms.