Int 21H [3.0] Function 5CH (92) Lock or unlock file region

Locks or unlocks the specified region of a file. This function is not available unless the file-sharing module (SHARE.EXE) is loaded.

Call with:

AH = 5CH

AL = 00H if locking region

01H if unlocking region

BX = handle

CX = high part of region offset

DX = low part of region offset

SI = high part of region length

DI = low part of region length

Returns:

If function successful

Carry flag = clear

If function unsuccessful

Carry flag = set

AX = error code

Notes:

This function is useful for file and record synchronization in a multitasking environment or network. Access to the file as a whole is controlled by the attribute and file-sharing parameters passed in open or create calls and by the file's attributes, which are stored in its directory entry.

The beginning location in the file to be locked or unlocked is supplied as a positive double precision integer, which is a byte offset into the file. The length of the region to be locked or unlocked is similarly supplied as a positive double precision integer.

For every call to lock a region of a file, there must be a subsequent unlock call with exactly the same file offset and length.

Locking beyond the current end of file is not an error.

Duplicate handles created with Int 21H Function 45H, or handles redirected to the file with Int 21H Function 46H, are allowed access to locked regions within the same process.

Programs that are loaded with the EXEC call (Int 21H Function 4BH) inherit the handles of their parent but not any active locks.

If a process terminates without releasing active locks on a file, the result is undefined. Therefore, programs using this function should install their own Int 23H and Int 24H handlers so that they cannot be terminated unexpectedly.

Example:

Assume that a file was previously opened and that its handle was saved in the variable fhandle. Lock a 4096 byte region of the file, starting at 32,768 bytes from the beginning of the file, so that it cannot be accessed by other programs.

fhandle dw ? ; file handle

.

.

.

mov ah,5ch ; function number

mov al,0 ; subfunction 0 = lock

mov bx,fhandle ; file handle

mov cx,0 ; upper part of offset

mov dx,32768 ; lower part of offset

mov si,0 ; upper part of length

mov di,4096 ; lower part of length

int 21h ; transfer to MS-DOS

jc error ; jump if lock failed

.

.

.