PreviousUpNext

5.5.57  Run a long-lived Linux subprocess

The easiest way to run a subprocess is to use the backquote operator or system, 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:

    #!/usr/bin/mythryl

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

            file::write (to_stream, "echo 'xyzzy'\n");                      # file  is from   src/lib/std/src/posix/file.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: bugs@mythryl.org

PreviousUpNext