Fork with expect

195 views
Skip to first unread message

mask...@gmail.com

unread,
Jul 16, 2008, 3:07:50 PM7/16/08
to
Hi,
The following code does not work for me. Can some one please suggest
any changes that i need to do. This was same as in the examples in the
exploring expect book.

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

Cameron Laird

unread,
Jul 16, 2008, 4:26:10 PM7/16/08
to
In article <ec0ceab5-6e26-415e...@f36g2000hsa.googlegroups.com>,

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?

mask...@gmail.com

unread,
Jul 16, 2008, 7:57:51 PM7/16/08
to
On Jul 16, 1:26 pm, cla...@lairds.us (Cameron Laird) wrote:
> In article <ec0ceab5-6e26-415e-9e39-184ba5c0d...@f36g2000hsa.googlegroups.com>,


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)


Jeff Hobbs

unread,
Jul 17, 2008, 3:19:01 AM7/17/08
to
On Jul 16, 4:57 pm, maski...@gmail.com wrote:
> One of the forums posting said that tcl with threads enabled (which is
> in my case) has problems with fork
...

> 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

Cameron Laird

unread,
Jul 17, 2008, 8:44:35 AM7/17/08
to
In article <68381738-e13a-4558...@79g2000hsk.googlegroups.com>,
.
[many more details]
.

.
>One of the forums posting said that tcl with threads enabled (which is
>in my case) has problems with fork
.
.
.
You were right; I was wrong: the code does NOT work.
We ought to do something to protect people from [fork]-
ing in a thread-enabled Tcl ...

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?

Reply all
Reply to author
Forward
0 new messages