Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C - <b>locking() lock areas of file</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
locking()                Lock Areas of File

 #include   <io.h>                       Required for declarations only
 #include   <sys\locking.h>

 int        locking(handle,mode,nbyte);
 int        handle;                      File handle
 int        mode;                        File locking mode
 long       nbyte;                       Number of bytes to lock

    locking() locks or unlocks 'nbyte' bytes of the file specified by
    'handle'.  Locking bytes in a file prevents the locked portion from
    being read or written to by other processes (other programs).
    Unlocking a file permits those previously locked portions to be read
    or written to by other processes.  All locking and unlocking begins
    at the current position of the file pointer and includes the next
    'nbyte' bytes, or to the end of the file.

    'mode' specifies the locking/unlocking action to be performed, and
    can be any of the following:

        LK_NBLCK    Lock specified bytes. If unable, return an error.

        LK_LOCK     Lock specified bytes. If unable, continue trying at
                    one second intervals. If unable after 10 attempts,
                    return an error.

        LK_UNLCK    Unlock the specified bytes. (The bytes must have been
                    previously locked.)

    Returns:    Zero if the call is successful, or -1 if a failure. On
                failure, 'errno' is set to one of the following:

                    EACCES        file already locked or unlocked
                    EABDF         invalid file handle
                    EDEADLOCK     file cannot be locked after 10 attempts

      Notes:    locking() should only be used under MS-DOS 3.0 and later.

                All locks should be removed before closing the file or
                exiting the program.

                Although more than one region of a file can be locked,
                overlapping regions are not allowed.  Only one region can
                be locked or unlocked per call.

                When unlocking a file, make sure that the region you're
                unlocking corresponds to a region that was previously
                locked. Since locking() does not join adjacent locked
                regions, each region must be unlocked separately.

  -------------------------------- Example ---------------------------------

    In this example, after checking that MS-DOS version 3.0 or later is
    being used, the current file position is saved. The pointer is placed
    at the beginning of the file and the area between the two is locked.

                #include <io.h>
                #include <sys\locking.h>
                #include <stdlib.h>

                extern unsigned char _osmajor;
                int fhndl;
                long position

                main()
                {
                   .
                   .
                      if (_osmajor >= 3) {
                         position = tell(fhndl);
                         lseek(fhndl,0L,0);
                         if ((locking(fhndl,LK_LOCK,position))== 0) {
                            lseek(fhndl,0L,0);
                            locking(fhndl,LK_LOCK,position);
                         }
                      }
                }

See Also: creat() open()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson