1
votes

So I am working with a program written in TCL that uses the FLOCK function to lock files. I am testing it on a newer version of Linux than the one it currently runs on and I found that when the newer machine runs the script, it uses FLOCK from /usr/bin/flock, which differs from the TCL version of FLOCK. The TCL version uses -read -write and such, while the Linux version uses completely different options.

In short, the program stops working and errors out when it gets to any FLOCK call. If I change the options to fit the Linux version, it breaks the program on the other machines.

Is there a way to make it use the TCL version as opposed to the Linux one?

1
All FLOCK-named things will go through either the kernel's flock(2) or fcntl(2) locking interfaces; flock(2) is advisory-only and does not work over NFS, so fcntl(2) was introduced to provide mandatory locks (rarely used) and over-NFS-locking. I suggest reading through the flock(2) manpage, especially the NOTES section, for the background that will probably help you better explain the problem you're trying to solve.sarnold
Hmm, well, the TCL script calls FLOCK with -write -nowait on files. This script normally runs on CentOS 5.7. When it is run on Ubuntu 12.04, the FLOCK line fails with "invalid command name 'flock'". If I change it to use FLOCK options in the Linux man pages, it will work on Ubuntu but fail on the CentOS 5.7 computers.Gramps

1 Answers

3
votes

Tcl itself does not come with a flock command, though you might be seeing it automatically trying to use the system command if you're testing interactively. Such automated use of system commands is not done in scripts (that would be hellishly prone to instability due to varying PATHs) so when writing a script you should be explicit as to what you mean.

If you want to use the system command (itself non-portable, especially to non-Linux systems) then just do:

exec flock $options...

Be aware that Tcl uses a different form of argument quoting to the shell. This can sometimes catch people out when writing exec calls.

Alternatively, use the flock Tcl command that is in the TclX package. The syntax is a little different to that of the Linux system utility, in large part because it's a bit lower-level. In its favor, it is rather more portable.