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 6.0 - <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() enables a process (program) to lock or unlock 'nbyte' bytes
    of the file associated with 'handle'.  Locking prevents other
    processes from reading from or writing to the locked portion.
    Unlocking permits other processes to read from or write to a
    previously locked portion.  All locking and unlocking begins at the
    current position of the file pointer and continues for '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 'nbyte' bytes; return an error if unable to do
                    so.

        LK_LOCK     Lock 'nbyte' bytes. If unsuccessful, retry at one
                    second intervals; if unsuccessful after 10 attempts,
                    return an error.

       LK_UNLCK     Unlock the previously locked file region specified by
                    'nbyte' bytes.

    Returns:    Zero if the call is successful.  On failure, -1 is
                returned and '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
                    EINVAL        invalid argument

      Notes:    locking() is effective only under MS-DOS 3.0 and later.

                Unlock all locked regions before closing the file or
                exiting the program.

                Multiple regions of a file can be locked, but locked
                regions cannot overlap.  In addition, locking adjacent
                parts of a file does not cause the regions to be joined.
                Each region must be unlocked with a separate call.

                Only one region can be locked or unlocked per call, and a
                file region that is being unlocked must correspond to a
                region that was previously locked.

 Portability:   Not supported by ANSI standard.

   -------------------------------- 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