5.5.57  Run a long-lived Linux subprocess

The easiest way to run a subprocess is to use bin_sh’, but they are no help if you need to run a persistent subprocess to respond to multiple commands.

The easiest way to do that is to use the spawn package, which hides the messy details of forking and setting up pipes:


    fun run_subprocess ()
            (spawn__premicrothread::spawn ("/bin/sh", []))
                { from_stream, to_stream, ... };                                        # spawn__premicrothread is from   src/lib/std/src/posix/spawn--premicrothread.pkg

            file::write (to_stream, "echo 'xyzzy'\n");                                  # file__premicrothread  is from   src/lib/std/src/posix/file--premicrothread.pkg
            file::flush to_stream;

            printf "Read from subprocess: '%s'\n" (string::chomp (the (file::read_line  from_stream) ) );

            exit 0;

    run_subprocess ();

Comments and suggestions to: