Is there anyway to execute 2 processes simultaneously in a tcl script,
so tat each one completes at more or less the same time, so that the
total execution time of the script is reduced, when we do it
sequentially.
I saw few references to fork, exec, and spawn, but not able to figure
out, how to get with them, any reference or example, will be more
helpful.
Thanks,
Nutty.
Are you talking about separate processes in terms of what the computer
understands
as a process (run a browser and a word processor and a calender
application and ..) or
do you want two Tcl procedures that run simultaneously?
In the first case, these external processes can be started via [exec
command &] (or
[open "|command"] to get access to the in/output of the process via a
pipe mechanism).
The & makes sure the process is running in the background.
In the second case you are talking about threads. There is a Threads
package available
with a very nice API.
Whether you will be able to gain time, is a matter of your computer
having two or
more processors.
this is wat I was in need of..
Thanks ya...
jus chked out some thing reg thread class in tcl.
it sounds fine, one more query is will the thread class work in expect?
as am using expect for my scripts
I got some errors, over which am working on now. wil keep posted.
thanks for the response again :)
yes
> as am using expect for my scripts
> I got some errors, over which am working on now. wil keep posted.
> thanks for the response again :)
>
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
If you just need to launch these processes, and youj don't need to
interact with them at all, you only need exec and '&':
# launch 2 processes into the background:
set output1 [exec process1 &]
set output2 [exec process2 &]
If you need to know about their return statuses, or you need to read the
stdout or stderr of those processes, more work is required.
--
Glenn Jackman
Ulterior Designer
ya thats wat I want to do, jus they need to execute in parallel..
I don want to any interaction between those 2 processes.
>
> # launch 2 processes into the background:
> set output1 [exec process1 &]
> set output2 [exec process2 &]
but how do I know these processes are completed, so tat I can proceed
further..?
>
> If you need to know about their return statuses, or you need to read the
> stdout or stderr of those processes, more work is required.
>
wil chk tat out too..
Regards,
Nutty
trying it out, getting lot of errors even for a simple code, wil verify
once and post if I hav any queries.
Regards,
Nutty
Well, that's a form of interaction. Launching a process into the
background is really for times when you truly don't care when it
finishes.
Nevertheless, my example was wrong in that the return value from
[exec ... &] is a list of pids, not output from the process. So, here's
a quick and dirty (and untested) check for process completion.
set pids [list]
lappend pids [exec process1 &]
lappend pids [exec process2 &]
set start [clock seconds]
while {[llength $pids] > 0} {
after 5000 ;# pause for 5 seconds
for {set i [expr {[llength $pids] -1}]} {$i >= 0} {incr i -1} {
set pid [lindex $pids $i]
set rc [catch {exec /usr/bin/ps -p $pid} output]
if {$rc != 0} {
# pid no longer running, remove from list
set pids [lreplace $pids $i $i]
}
}
}
set duration [expr {[clock seconds] - $start}]
puts "both processes are complete, after $duration seconds"
There are, as it turns out, several different ways to program
detection of process completion, but none of them superior in
all circumstances. Please describe your problem at a higher
level; perhaps we can then give you more accurate advice.
Yes and no. The "gaining time" part is more complicated even than this;
<URL: http://www.unixreview.com/documents/s=10097/ur0609g/ur0609g.htm >
touches on some of the issues.
ya jus going thru that link, hope i get some thing over there.1
>
> There are, as it turns out, several different ways to program
> detection of process completion, but none of them superior in
> all circumstances. Please describe your problem at a higher
> level; perhaps we can then give you more accurate advice.
>
my problem is this, i need to call say procedure1 twice
like,
procedure1 arg1 arg2
procedure1 arg3 arg4
both are independent of each other, but same procedure, I want to
execute them parallely so that time is saved finally. Its like doing
same action on 2 different piece of arguments.
Regards,
Nutty.
background process.. I don't think will suit my need..
I want to see the result in the foreground, I mean the logs are needed
for substantiating that the process is completed. The statement after
the above 2 statements wil be executed only after the completion of the
background process.
Regards,
Nutty.
First, what you're attempting is probably not worth the effort.
To save execution time by directing two different [proc]s--not
processes, not ...--to different processors is inherently hard-
ware-dependent, at least for currently popular operatint systems.
Even if you succeed in some measure on your host, it might not
give the same satisfaction on any other.
Are you sure that procedure1 takes so long to execute that it's
worth a rewrite? Can you identify a segment within procedure1
that's the bottleneck? Does procedure1 fill the CPU, or is it
blocking on I/O, network, or memory access?
Regards,
Nutty
What do you have so far?