Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - locking http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   locking

   Usage
   #include <sys\locking.h>
   #include <io.h>
   int locking(int fd, int mode, long size);

   Description
   The locking function locks or unlocks a section of the file associated
   with the file handle fd. This operation is used for record locking  in
   files  that are shared by multiple applications in a multi-tasking  or
   network  environment.  Locking a section of the  file  prevents  other
   applications  from reading or writing that section while it  is  being
   updated.  The section is then unlocked to allow other applications  to
   read and write to it. The file is locked from the current position  of
   the file pointer for the next size bytes.

   This function is only available with MSDOS 3.0 and above and  requires
   that the DOS SHARE program is installed. It will also work under OS/2.

   The  mode argument specifies which of several locking functions is  to
   be applied:

   LK_LOCK, LK_RLCK
   Locks the specified number of bytes. If they cannot be locked it  will
   retry  after 1 second. This is repeated until 10 attempts have  failed
   at which point the function will return an error.

   LK_NBLCK, LK_NBRLCK
   Locks the specified number of bytes. If they cannot be locked, returns
   an error.

   LK_UNLCK
   Unlocks the specified number of bytes. The file pointer and size  must
   have exactly the same values as when the section was locked.

   Multiple regions of a file can be locked, but no regions may  overlap.
   No more than one region can be unlocked with one call to locking.

   Example
   #include <stdio.h>
   #include <stdlib.h>
   #include<io.h>
   #include <sys\types.h>
   #include <sys\locking.h>

   extern unsigned char _osmajor;

   main()
   {
   int fd;

        if(_osmajor < 3)
        {
             printf("Locking requires MSDOS 3.0 or higher\n");
             exit(EXIT_FAILURE);
        }

        if((fd = open("temp.fil",O_RDONLY)) == -1)
        {
             perror("Error opening file");
             exit(EXIT_FAILURE);
        }

        lseek(fd, 0L, SEEK_SET);
        if((locking(fd, LK_NBLCK, 20)) == -1)
        {
             perror("Locking operation failed");
             exit(EXIT_FAILURE);
        }
        else
        {
             printf("Locking operation successful\n");
             lseek(fh, OL, SEEK_SET);
             locking(fd, LK_UNLCK, 20);
        }
        close(fd);
   }

   Return Value
   Returns  0 if successful, otherwise -1 if an error occurred and  errno
   is set.


See Also: creat open

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