开发者

What does _locking() really do?

Looking for answer of this question I found function _locking(). There tells that it Locks or unlocks bytes of a file (actually I can't understand what does this sentence really mean). If someone have expe开发者_运维百科rience of using of this function, is it possible to use the function for solving problem described in first question?


Quoting the MSDN page you linked:

int _locking(
   int fd,
   int mode,
   long nbytes 
);

The _locking function locks or unlocks nbytes bytes of the file specified by fd. Locking bytes in a file prevents access to those bytes by other processes. All locking or unlocking begins at the current position of the file pointer and proceeds for the next nbytes bytes. It is possible to lock bytes past end of file.


It simply reserves a range of a file for the exclusive use of the process that acquires the file lock. If a lock call succeeds, another process that tries to read or write that portion of the file will fail. This allow multiple processes to access the same file and update it in a coherent manner. It's kind of like a mutex for a range of a file.

Basically it allows you to update portions of a file atomically, so any other process reading or writing the file will see (or change) either all of the update, or none of it. It also applies to read - you can lock a range of a file that you want to read to prevent another process from changing part of it while you're in the middle of reading it.

But processes can still access other parts of the file without error or delay.

It will not solve the problem in the question you're referring to because _lock() on;t works at a process granularity. If thread A locks a file range, then thread B in that same process can still read/write that range. To prevent another thread in the same process from accessing a file range, the process would have to implement its own internal mechanism for respecting that a file range has been locked by another thread. At least I'm unaware of something that does that in the Win32 API (I suppose there could be something that I don't know about).


http://msdn.microsoft.com/en-us/library/8054ew2f(v=vs.71).aspx

I found that this helps "fix" the problem Race condition!

The last person to write to a file wins. Say you only need to read the first half of a file no reason to lock the whole file.

So you take the file size in bytes pass it to this function then lock it down.

The function will returns a 0 if successful. A return value of –1 indicates failure, in which case errno is set to what the MSDN page tells you.

To answer your question

You can take the size of the file then lock it down but you will only be able to read form the file. you lock it into a kind of read mode only.

In the wiki of Race condition it tell you examples of how to lock a file by getting a 2nd process to check for a flag this may work in your case look into it.


It prevents other processes from accessing that same part of the file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜