NetSoft Computer Networks
Last Updated
March 10th
1997

Mirror Sites
NetSoft LAN
Download the
Software

Network Card
Drivers

Secure
Features
User's Guide
Help
VeriSign

Search
Distribution
Rights

Listserv
Shareware Links
Order Now
Order the
Software

Help Us
Promote
NetSoft LAN

Dealer
Enquires
Welcome

Home Page
http://www.netsoft.ie/
CHAPTER 16

Developing Multi-User Applications



File Sharing and Locking Tips for System Programmers

This section should only be read by system programmers. It explains the DOS file sharing modes in detail and the DOS function calls to implement file and record locking.

Lock and unlock function calls

The DOS INTERRUPT 21H Function AH=5CH lock and unlock file access functions, denies or permits access to a specified region of a file. This call is available in DOS version's 3.1 or above. The DOS SHARE.EXE command must be loaded on the server machine.

The interpretation of the error code is very important. Do not assume that if an error is returned (i.e. carry flag is set) the region or record is locked. You must interpret the actual error code returned, and take the appropriate action.


  On Entry   Lock and Unlock Function Calls

    AH=      5Ch Lock/Unlock Function

    AL=      00h - Lock a region
             01h - Unlock a region

    BX=      File handle

    CX=      Offset (high) of region

    DX=      Offset (low) of region

    SI=      Length (high) of region

    DI=      Length (low) of region

  On Exit

             If carry set (cy=1) then AX= Error
             code
             If carry clear (cy=0) function was
             successful

    01h      Invalid function call. AL is not
             00h or 01h,
             or SHARE.EXE is not loaded on file
             server

    06h      Invalid Handle

    21h      File locking violation
             (Specified region is already
             locked)

    24h      Sharing buffer overflow
             Increase the /L:Locks and /F:Space
             parameters on the SHARE.EXE command
             line

Locking

This function is used to lock or unlock a region of a file, preventing or allowing access to that region by other processes in a multi-tasking or networking environment.

The CX and DX registers together make up a 4-byte integer that specifies the byte offset into the file of the region to be locked or unlocked. The SI and DI registers make up a 4-byte integer specifying the length of the region.

Terminating a process that has an open file with a locked region, or closing a file with a locked region, have undefined results. If your program can be aborted through DOS INTERRUPTS 23H or 24H, you should ensure that the locks are released before terminating the program.

Unlocking

The unlocking operation must specify exactly the same file region as the locking operation. A child process (i.e. a process created through the DOS EXEC system call), does not inherit access to regions of a file that were locked by its parent.

File sharing modes

The lock and unlock function calls should be used when a file is opened in any of the following modes:

Deny read sharing mode

Deny none sharing mode

Deny write sharing mode, and file opened for read/write

Deny write sharing mode, and file opened for write only

Open file function calls

DOS INTERRUPT 21H function 3DH opens a file with the specified access code.
  On Entry   Open file function call

    AH=      3Dh Open file function

    AL=      Open Mode (see below)

    DS=      Segment of ASCIIZ string pathname

    DX=      Offset of ASCIIZ string pathname

  On Exit                                         

             If carry set (cy=1) then AX= Error
             code.
             If carry clear (cy=0) function was
             successful

    01h      Invalid function call

    02h      File not found

    03h      Path not found

    04h      No handle available. Check the
             FILES= parameter in CONFIG.SYS
             file. Load the NETFILES.COM program
             on the server machine to open more
             than 255 files

    05h      Access denied

    0Ch      Open mode invalid

This function opens the file whose pathname is specified in the ASCIIZ string at DS:DX using the Open Mode byte to determine how the file may be accessed. The function opens any existing file, including hidden files, and sets the record size to 1 byte.

Open mode

The open mode is made up of a combination of three sub modes. The structure of the sub modes is shown in the table below:
 7   6   5   4   3   2   1   0   Access Mode    DOS 2.x

                     A   A   A   Access Mode    Read/Write
                                                  access

                 R                Reserved      Always 0

     S   S   S                  Sharing Mode    Must be 0
                                                for DOS 2.x

 I                               Inheritance    Must be 0
                                     Bit        for DOS 2.x

Access mode

The low-order three bits of the access mode are used in the same manner in DOS version 2 and 3. These bits indicate allowable access (read, write, or read/write).
 2   1   0   Hex    Access Mode

 0   0   0   00h     Read only

 0   0   1   01h     Write only

 0   1   1   02h     Read/Write

Reserved

Bit 3 is reserved (must always be zero).


 3   Hex    Reserved

 R   00h   Must be 0

Sharing mode

In DOS 3.0 and above, bits 4, 5 and 6 specify a sharing mode (must be set to 0 in DOS 2.x). These bits govern the manner (if any) in which other users on a network or multi-tasking environment may open and use the file, after you have opened it. The following settings (and no others) are valid:


 6   5   4   Hex         Sharing Mode

 0   0   0   00h      Compatibility mode

 0   0   1   10h       Deny read/write
                       (exclusive mode)

 0   1   0   20h       Deny write mode

 0   1   1   30h        Deny read mode

 1   0   0   40h        Deny none mode

The inheritance flag

The inheritance flag (bit 7), specifies whether child processes will inherit the use of this file. If bit 7 is 0, child processes automatically have access to the file. If bit 7 is 1, the file is not automatically available to child processes. Like any other process, however, a child process can request access to the file on a shared basis.


 7   Hex         Inheritance Flag Bit

 0   00h      File is inherited by child
                       processes

 1   80h         File is not inherited

Opening files

Once a file has been opened by a process, the availability of that file to other processes is determined by both the sharing mode, and access mode of the original process, as well as by the sharing mode and access mode specified by the subsequent process. Here is a case-by-case summary of these interactions:

The file is first opened in compatibility sharing mode. A file is considered to be in compatibility mode if it is opened using any of the following:

1. An FCB function.

2. Any of the CREATE functions.

3. Any handle function call in which compatibility mode is specified.

A file opened in compatibility mode may be re-opened any number of times by a single process, unless the file is already open under one of the other four sharing modes. If the file has the read-only attribute, however, and is currently open in deny-write sharing mode with read-access, it may be opened in compatibility mode with read-access.

The file is opened in one of the other sharing modes.


A file first opened in    Mode   May be re-opened    Mode
                                        in

Deny read/write mode,   x0010000 May not be
read only                        re-opened

Deny read/write mode,   x0010001 May not be
write only                       re-opened

Deny read/write mode,   x0010010 May not be
read/write                       re-opened

Deny write mode, read   x0100000 Deny write, read  x0100000
only                             only              x1000000
                                 Deny none, read
                                 only

Deny write mode, write  x0100001 Deny read, read   x0110000
only                             only              x1000000
                                 Deny none, read
                                 only

Deny write mode,        x0100010 Deny none, read   x1000000
read/write                       only

Deny read mode, read    x0110000 Deny write,       x0100001
only                             write only        x1000001
                                 Deny none, write
                                 only

Deny read mode, write   x0110001 Deny read, write  x0110001
only                             only              x1000001
                                 Deny none, write
                                 only

Deny read mode,         x0110010 Deny none, write  x1000001
read/write                       only

Deny none mode, read    x1000000 Deny write, read  x0100000
only                             only              x0100001
                                 Deny write,       x0100010
                                 write only        x1000000
                                 Deny write,       x1000001
                                 read/write        x1000010
                                 Deny none, read
                                 only
                                 Deny none, write
                                 only
                                 Deny none,
                                 read/write

Deny none mode, write   x1000001 Deny read, read   x0110000
only                             only              x0110001
                                 Deny read, write  x0110010
                                 only              x1000000
                                 Deny read,        x1000001
                                 read/write        x1000010
                                 Deny none, read
                                 only
                                 Deny none, write
                                 only
                                 Deny none,
                                 read/write

Deny none mode,         x1000010 Deny none, read   x1000000
read/write                       only              x1000001
                                 Deny none, write  x1000010
                                 only
                                 Deny none,
                                 read/write

Additional tips

Files that end with a colon (devices) are not opened with this function call. To open devices, you must not specify the colon, e.g. to open LPT: use LPT instead.

The DOS SHARE.EXE program must be loaded on the server for sharing and file and record locking.

Child processes which inherit files, also inherit their sharing and access restrictions.

The CONFIG.SYS FILES= parameter must specify the number of files you are going to open simultaneously. Remember, unless you explicitly close the PRN, CON, and AUX devices, they remain open and thus use a file handle.

The DOS-NET NETFILES.COM program must be loaded on the server machine if you wish to open more than 255 files on a server. In addition, you must set the DOSNET.INI file parameter NetFiles= to the number of files above 255 that you wish to open.

With some software, it is necessary to load SHARE.EXE on the client machine, as well as the server.


[Chapter 17] [Chapter 15]
[Table of Contents] [Home Page]

Prices and World Wide Ordering Information

Copyright © 1997 NetSoft Computer Networks. All Rights Reserved.
20 Sea Road, Galway, Ireland.
Tel (353)+91-529607.