I have a requirement where I want to have a tcl script run a couple
of processes in background parallely. Then it should wait for 1 of
them to complete and then fork a third process. To elaborate say I
have three utils known as Util1, util2 and util3. I want to do as
exec util1 and exec util2 (both in background)
then wait for util1 to finish to launch util3. In case util2 has not
finished when util3 starts its no issue. But before launching util3 I
need to be sure that util 1 has completed successfully. Is there a way
to do this in TCl using proc ids or something else/
What you want is [fileevent] (or the [chan event] command in Tcl 8.5).
The Wiki has quite a few examples of how to do this.
Basically:
- start util1 via [open "|util1"]
- use fileevent or chan event to connect a script (proc) to the
channel you created this way
- in that proc the end-of-file condition means that the program
stopped. So that is the signal to start util3.
Regards,
Arjen
~Deepanshu
Use [fileevent] - that has been around for ages.
(the new chan command bundles a number of related commands, that
is - from one practical, though not necessarily correct, point of
view - all there is to it).
Regards,
Arjen
If you don't need the processes to actually both be in the background,
but just want them processed at the same time, then [exec] can handle
this -- just spawn util2 first:
exec util2 & ;# in background
exec util1 ;# in foreground
exec util3
Here util1 and util2 will be launched immediately, but util3 will only
launch when util1 finishes. If you really need all processes in the
background (e.g. so your script can do something else), then Arjen's
advice to look at fileevent is the way to go.
-- Neil
This is extracted from the page, where 3 processes are
launched in background :
set h1 [bgExec "[info nameofexe] delayout1.tcl" [list dummy *1*]
pCount]
set h2 [bgExec "[info nameofexe] delayout2.tcl" [list dummy2 *2*]
pCount]
set h3 [bgExec "[info nameofexe] delayout3.tcl" [list dummy *3*]
pCount 5000 toExit]
Whenever a command is launched, the local variable "pCount" is
incremented.
Whenever a process is done (eof), the local variable "pCount" is
decremented.
The whole trick is to use the "vwait" command to look for
modifications of the value of the variable pCount. When the
value of pCount is zero, you are done :
while {$pCount > 0} {
vwait pCount
}
This may be defined in a command, say bgExec_barrier, for example.
Regards,
Michaël
You don't mention what platform you're on.
I would consider something like this -- let someone else worry about
queuing your external programs:
exec sh -c {util1 && util3} &
exec util2 &
That will only launch util3 if util1 returns an exit status of 0. If
you don't care about the exit status of util1, do:
exec sh -c {util1 ; util3} &
--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous