What's the high resolution time axis behavior of TCL 'exec ' ? I understand that a 'fork' command will be used which will at first create a copy of the memory image of the process and then proceed.
Here's the motivation for my question:
A user gave me following observation. A 64 GB machine has a TCL based tool interface running with 60GB memory used. (let's assume swap is small). At the TCL prompt he gives 'exec ls' and the process crashes with a memory error.
You insight is much appreciated. Thanks, Gert
答案 0 :(得分:1)
exec
命令将在内部调用fork()
系统调用。这通常没问题,但是当操作系统配置为不交换并且原始Tcl进程非常大时(或者如果有很少的slop空间;它当然取决于实际情况)可能会耗尽内存。
我减少内存使用量的想法是使用vfork()
(通过修补tclUnixPipe.c
;您可以在makefile中定义USE_VFORK
以启用它,而且我不会这样做。知道为什么不能更广泛地使用)或者在早期创建一个帮助程序(在使用大量内存之前),这将在主进程上执行exec
& #39;代表。以下是如何做到后一种选择:
# This is setup done at the start
set forkerProcess [open "|tclsh" r+]
fconfigure $forkerProcess -buffering line -blocking 0
puts $forkerProcess {
fconfigure stdout -buffering none
set tcl_prompt1 ""
set tcl_prompt2 ""
set tcl_interactive 0
proc exechelper args {
catch {exec {*}$args} value options
puts [list [list $value $options]]
}
}
# TRICKY BIT: Yield and drain anything unwanted
after 25
read $forkerProcess
# Call this, just like exec, to run programs without memory hazards
proc do-exec args {
global forkerProcess
fconfigure $forkerProcess -blocking 1
puts $forkerProcess [list exechelper {*}$args]
set result [gets $forkerProcess]
fconfigure $forkerProcess -blocking 0
while {![info complete $result]} {
append result \n [read $forkerProcess]
}
lassign [lindex $result 0] value options
return -options $options $value
}