Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to wait on execution of processes

1,466 views
Skip to first unread message

Deepanshu

unread,
Aug 21, 2008, 3:51:46 AM8/21/08
to
Hi Frnds,

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/

Arjen Markus

unread,
Aug 21, 2008, 5:02:06 AM8/21/08
to

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

unread,
Aug 21, 2008, 5:41:12 AM8/21/08
to
Srry forgot to add ; I would need something that could work on
Tcl8.4.10

~Deepanshu

Arjen Markus

unread,
Aug 21, 2008, 6:49:49 AM8/21/08
to
On 21 aug, 11:41, Deepanshu <pahujadeepan...@gmail.com> wrote:
> Srry forgot to add ; I would need something that could work on
> Tcl8.4.10
>

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

Neil Madden

unread,
Aug 21, 2008, 7:18:19 AM8/21/08
to

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

relaxmike

unread,
Aug 21, 2008, 10:04:59 AM8/21/08
to
Arjen's solution is partly described on the wiki,
a script written Matthias Hoffmann called bgexec1.8.tcl :

http://wiki.tcl.tk/12704

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

Glenn Jackman

unread,
Aug 21, 2008, 10:57:40 AM8/21/08
to

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

0 new messages