|
|
|
|
| windows 2000下文件加锁的问题 file flock |
 windows 2000下文件加锁的问题 file flock - wiwj [ 2005-01-14 17:03 | 529 byte(s)]
 Re: windows 2000下文件加锁的问题 file flock - Qiang [ 2005-01-19 12:13 | 47 byte(s)]
 Re: windows 2000下文件加锁的问题 file flock - wiwj [ 2005-01-17 16:20 | 1,701 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
有一程序A会不停的更新一些文件(简称原文件)。
我用 perl 了个小程序,读“原文件”,并做些处理,
将结果另存为新文件。
现在会出现这样的情况:
perl程序在读取“原文件”时,程序A会同时更新这些文件,
结果我读到的内容就不完整(只有前面一部分)。
我现在试图在perl中用flock在读“原文件”之前现加个锁。
问题是:
我不知道这会不会影响程序A对“原文件”的写?
还是操作系统会自动挂起A,直到我unlock,然后正常写入?
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
网上查了下,我的问题好像不能用flock解决。
因为flock是“非强制的”,只有当2个程序都使用flock才行。我无法修改程序A。
暂时用其他的方法解决文件完整性的问题。
下面是一些关于的flock的说明:(第五条我看了半天,才看明白,乱翻一下)
Note that flock() locks are advisory, so it won't affect your other
script unless that also uses flock. The rules governing locks are:
1. Locks don't affect anything except other locks: they don't stop
anyone opening, reading, writing, deleting, etc. the file.
2. You can only hold a lock on an open file. (The details of what
happens when you have several handles open on the same file depend
on the underlying system: don't go there.
3. If someone holds a LOCK_EX lock, all attempts to get a lock will
fail. (LOCK_EX means 'exclusive lock'.)
4. If someone holds a LOCK_SH lock, attempts to get a LOCK_EX lock
will fail but attempts to get another LOCK_SH lock will
succeed. (LOCK_SH means 'shared lock'.)
5. If you can't get the lock you are trying for, then if you specify
LOCK_NB (use | rather than +, i.e. 'flock $FH, LOCK_SH | LOCK_NB)
the flock call will fail, and if you don't flock will just not
return until you can.
5. 当你试图得到一个文件的“锁”,又无法得到时,
如果你指定了 LOCK_NB,那么flock将立即返回fail,
如果你没有指定 LOCK_NB,flock直到你得到锁,才返回。
|
|
|
[Original]
[Print]
[Top]
|
|
|