Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <b>lock() set file sharing locks</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 lock()                  Set File Sharing Locks

 #include   <io.h>

 int        lock(handle,offset,length);
 int        handle;                      Handle associated with file
 long       offset;                      Location of area to be locked
 long       length;                      Length of area to be locked

    lock() locks an area of the file associated with 'handle', starting
    at the position 'offset' and proceeding for 'length' bytes.  Locking
    bytes in a file prevents subsequent reading and writing of those
    bytes by other processes.  No overlapping regions can be locked.  A
    program trying to read or write into a locked region will retry the
    operation three times.  If all three retries fail, the call fails
    with an error.

    lock() must be removed before a file is closed, and the program must
    release all locks before completing.

       Returns:     0, if successful; -1 on error

         Notes:     lock() works only with MS-DOS versions 3.0 and later.
                    There is no support for file locks in older versions.

   Portability:     MS-DOS 3.0 and later, only.

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

    The following statements open a file, lock the first 1000 bytes of
    the file, do some operation on it and then unlock the file.

           #include <stdio.h>   /* for printf */
           #include <io.h>         /* for lock, unlock, _open and close */
           #include <fcntl.h>      /* for constants */

           main()
           {
               int handle;

               if ((handle =
                    _open("\network\shared.dat",O_RDWR|O_DENYNONE)) == -1) {
                   printf("Error opening file.\n");
                   exit(1);
               }
               if (lock(handle,0L,1000L) == -1) {
                   printf("Unable to lock file.\n");
                   exit(1);
               }
                .
                .
                /* do some file operations of first 1000 bytes */
                .
                .
               unlock(handle,0L,1000L);
               closes(handle);
           }


See Also: open() unlock()

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