Question

I am spawning multiple sh (sipp) scripts from a tcl script . I want to know those scripts will run in parallel or as a child process? Because I want to run it in parallel.

If I use threads extension then do I need to use any other packages along with that?

Thanks in advance.

Was it helpful?

Solution

Tcl can quite easily run multiple subprocesses in parallel. The way you do so depends on how you want to handle what those subprocesses do. (Bourne shell — sh — scripts work just fine as subprocesses.) None of these require threads. You can use threads too, but they're not necessary for just running subprocesses as, from Tcl's perspective at least, subprocess handling is a purely I/O-bound matter.

For more details, please narrow down (in another question) which type of subprocess handling you want to do.

Fire and Forget

If you don't care about tracking the subprocesses at all, just set them going in the background by putting a & as the last word to exec:

exec /bin/sh myscript.sh &

Keeping in Touch

To keep in touch with the subprocess, you need to open a pipeline (and use this weird stanza to do so; put the arguments inside a list with a | concatenated on the front):

set thePipe [open |[list /bin/sh myscript.sh]]

You can then read/gets from the pipe to get the output (yes, it supports fileevent and asynchronous I/O on all platforms). If you want to write to the pipe (i.e., to the subprocess's stdin) open with mode w, and to both read and write, use mode r+ or w+ (doesn't matter which, as it is a pipe and not a file). Be aware that you have to be a bit careful with pipes; you can get deadlocked or highly confused. I recommend using the asynchronous I/O style, with fconfigure $thePipe -blocking 0, but that's quite a bit different to a synchronous style of I/O handling.

Great Expectations

You can also use the Expect extension to work with multiple spawned subprocesses at once. To do that, you must save the id from each spawn in its own variable, then pass that id to expect and send with the -i option. You probably want to use expect_background.

set theId [spawn /bin/sh myscript.sh]
expect_background {
    -i $theId
    "password:" {
        send -i $theId "$mypass\r"
        # Etc.
    }
}
# Note that [expect_background] doesn't support 'timeout'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top