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++ 3.0r4 - <b>locking</b> 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. 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 <io.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <sys\locking.h>
   #include <sys\types.h>

   int main()
   {
       int fd;

       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(fd, 0L, SEEK_SET);
           locking(fd, LK_UNLCK, 20);
           }
       close(fd);
       return EXIT_SUCCESS;
   }

Return Value

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

See Also

   creat, open



See Also: creat open

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