set childList ""
set pid [fork]
if { $pid > 0 } {
puts "Child PID is $pid"
lappend childList $pid
} else {
set prompt ".*\\$ "
spawn sh
expect -re $prompt
exp_send "ls -l\r"
expect -re $prompt
exit
# child dies
}
# parent continues, do some thing and exit
# TODO: Code to check if all the children died
exit
I suggest these changes:
A. Read <URL: http://wiki.tcl.tk/1639 >;
B. Detail for us what "does not work for me"
means to you. I have a personal belief
that that fragment *does* work; is there
evidence to the contrary?
When i execute the above code, the spawn process does not get any
data. Each expect statement times out. Now i comment the fork
statement, then everything works well.
Here is the output
# with fork commented out
$ tclsh a
spawn sh
sh-3.2$ ls -l
total 8
drwx------+ 6 kiran staff 204 Jul 15 19:25 Desktop
drwx------+ 22 kiran staff 748 Jun 26 07:03 Documents
drwx------+ 37 kiran staff 1258 Jun 18 22:25 Downloads
drwx------+ 48 kiran staff 1632 Jul 9 19:25 Library
drwx------+ 8 kiran staff 272 Jun 18 21:53 Movies
drwx------+ 6 kiran staff 204 Apr 26 12:55 Music
drwx------+ 8 kiran staff 272 May 31 11:02 Pictures
drwxr-xr-x+ 5 kiran staff 170 Jan 3 2008 Public
drwxr-xr-x+ 9 kiran staff 306 Jun 20 13:31 Sites
-rw-r--r-- 1 kiran staff 240 Jul 16 16:54 a
drwxr-xr-x 8 kiran staff 272 Feb 15 10:57 automation
sh-3.2$ $
With fork enabled, the CLI shows nothing and just keeps waiting. with
no output at all. Eventually expect timesout, but the child does not
exit and i can see that in ps
$ tclsh a
Child PID is 3651
spawn sh
$
$ ps
PID TTY TIME CMD
3616 ttys000 0:00.04 -bash
3651 ttys000 0:00.01 tclsh a
3652 ttys001 0:00.00 sh
The child does not even die with kill, i have to use kill -9.
One of the forums posting said that tcl with threads enabled (which is
in my case) has problems with fork
expect version 5.42.0
% info tclversion
8.4
System is Mac OS X Leopard. But the same problem persists on Linux (FC
8,7)
Yes, the threading and fork issue is true (a pthreads incompatibility
that Tcl does not try and work around). Can you use a non-threaded
build?
Jeff
What's the *real* goal? Presumably you want to do
something more demanding than multiprocessing directory
reports. As melancholy as it is that Expect isn't
compatible with threading, it's also likely that we can
come up with a satisfactory solution by operating at a
lower level. Does event-based concurrency not suffice
for your application?