|
|
To share a file between the interpreters in a group, use the Tcl detach command. A shared file is left open after an interpreter is deallocated when the AutoClose parameter is on (it is on by default). For example:
#init time:
set sharedFile [open myfile.dat]
detach $sharedFile
You must manually close the shared file (by performing a close) when it is no longer needed or when the server is shut down.
Note that there is no implicit locking between interpreters. This means that one interpreter can be performing a gets command on the shared file at the same time another interpreter performs a close on the same file. The results are unpredictable, and could potentially cause a core dump. To avoid this situation, use a mutex to protect access to the file whenever it is accessed.
For example:, at initialization time (for example, in your init.tcl script), open a shared file and create a mutex:
set sharedLock [ns_mutex create]
set sharedFile [open myfile.dat]
detach $sharedFile
At run time, use the mutex to protect any actions you perform on the shared file:
ns_share sharedLock
ns_share sharedFile
ns_mutex lock $sharedLock
puts $sharedFile ...
gets $sharedFile ...
ns_mutex unlock $sharedLock
At shutdown (for example, in your shutdown procedure registered with ns_atshutdown), close the file and destroy the lock:
ns_share sharedLock
ns_share sharedFile
close $SharedFile
ns_mutex destroy $SharedLock