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

a bgexec thingy

1 view
Skip to first unread message

Phil Ehrens

unread,
Dec 21, 2001, 6:34:55 PM12/21/01
to
Hi All,

When you do this sort of thing:

set fid [ open |$program ]

does closing the fid kill the process, always?

Darren New

unread,
Dec 21, 2001, 7:14:58 PM12/21/01
to
Phil Ehrens wrote:
> does closing the fid kill the process, always?

No. Closing the fid closes the pipe. Next time the process writes to the
pipe, it gets a sigpipe. If it doesn't write to the pipe, or otherwise
catches or avoids the sigpipe, then it can keep running.

--
Darren New
San Diego, CA, USA (PST). Cryptokeys on demand.
"This wine goes good with feet."

Phil Ehrens

unread,
Dec 21, 2001, 7:45:55 PM12/21/01
to
Darren New wrote:
> Phil Ehrens wrote:
>> does closing the fid kill the process, always?
>
> No. Closing the fid closes the pipe. Next time the process writes to the
> pipe, it gets a sigpipe. If it doesn't write to the pipe, or otherwise
> catches or avoids the sigpipe, then it can keep running.

In THAT case, how do I get the pid of the process opened by
"set fid [ open |foo ]"?

Sorry about the subject line of the post, I intended to post the
following code, but find that when it is called on "cat" for
instance, the timeout code fails strangely.

Interested persons can test like this:

_exec "whois tcltk.org" foo
vwait ::foo
puts $::foo

All sorts of interesting things work, but "_exec cat foo" fails
unexpectedly, the timeout occurs, but the code does not do what
I expect.

proc _exec { prog var { tmpfile "" } { i 0 } { to 5 } } {

if { ! [ regexp {^file\d+$} $prog ] } {
set tmpfile _exec-[ clock seconds ][ clock clicks ]
set tmpfile /tmp/$tmpfile

if { [ catch {
set fid [ open "|$prog 2>$tmpfile" ]
fconfigure $fid -blocking off
fconfigure $fid -buffering full
} err ] } {
file delete -force $tmpfile
return -code error "_exec: $err"
}
} else {
set fid $prog
}

set ret [ read $fid ]

if { ($i / 10) >= $to } {
if { ! [ string length $ret ] } {
set ret timed_out
}
}

if { [ string length $ret ] || [ file size $tmpfile ] } {
catch { ::close $fid }
set fid [ open $tmpfile r ]
set err [ read $fid [ file size $tmpfile ] ]
catch { ::close $fid }
file delete -force $tmpfile
set ::$var [ list $ret $err ]
} else {
incr i
after 100 [ list _exec $fid $var $tmpfile $i ]
}
}

David Gravereaux

unread,
Dec 21, 2001, 7:41:02 PM12/21/01
to
peh...@nospam.ligo.caltech.edu (Phil Ehrens) wrote:

>In THAT case, how do I get the pid of the process opened by
>"set fid [ open |foo ]"?

pid $fid
--
David Gravereaux <davy...@pobox.com>
Tomasoft Engineering, Hayward, CA

Frank Pilhofer

unread,
Dec 22, 2001, 8:58:40 AM12/22/01
to
David Gravereaux <davy...@pobox.com> wrote:
> peh...@nospam.ligo.caltech.edu (Phil Ehrens) wrote:
>
> >In THAT case, how do I get the pid of the process opened by
> >"set fid [ open |foo ]"?
>
> pid $fid
>

But there's nothing you can really do with this information, other
than maybe "exec kill [pid $fid]". But on systems that don't have
"kill", like Win32, you're stuck.

For one software of mine, I include a third-party kill program into
the Win32 distribution, just so that I can exec it.

Wouldn't it be nice if there was a kill in the Tcl core?

Frank


--
Frank Pilhofer ........................................... f...@fpx.de
Even the man who has everything is envious of the man who has two of
everything. - Alfred E. Neuman

Roy Terry

unread,
Dec 23, 2001, 7:54:40 PM12/23/01
to
Processes can be killed on Win32 and
XTcl extension provides it. I just tried
with 8.3 on NT4.0. exec and kill notepad.exe
OK.

Roy Terry

unread,
Dec 23, 2001, 7:57:26 PM12/23/01
to
Sorry that's TclX extension :(

Phil Ehrens

unread,
Dec 26, 2001, 12:43:40 PM12/26/01
to
Frank Pilhofer wrote:
> David Gravereaux <davy...@pobox.com> wrote:
>> peh...@nospam.ligo.caltech.edu (Phil Ehrens) wrote:
>>
>> >In THAT case, how do I get the pid of the process opened by
>> >"set fid [ open |foo ]"?
>>
>> pid $fid
>>
>
> But there's nothing you can really do with this information, other
> than maybe "exec kill [pid $fid]". But on systems that don't have
> "kill", like Win32, you're stuck.

Which means I will never be stuck ;^)

Phil Ehrens

unread,
Dec 26, 2001, 12:46:35 PM12/26/01
to
Thanks Dave!

Now, should the pipe be closed first or the process killed
first to get the maximum usable info about what happened...

0 new messages