>
> there is small doubt
>
> i have one directory which is opened by a process
> When a second process tries to open that same
> directory , it should not allow the process to do
> this.
>

/*
*   lock.c   -   Lock a directory  
*   Author   -   Vijay Kumar R Zanvar
*   Date     -   February 05, 2004
*/


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>

/* Run two instances of this program to see the effect */

int
main ( int argc, char *argv[] )
{
    int     fd, ret;
    struct stat     buf;

    if ( argc < 2 )
    {
        fprintf ( stderr, "Supply a directory name.\n" );
        exit ( EXIT_FAILURE );
    }

   stat ( argv[1], &buf );
   if ( !S_ISDIR ( buf.st_mode ) )
   {
        fprintf ( stderr, "Supply a directory name.\n" );
        exit ( EXIT_FAILURE );
   }

   fd = open ( argv[1], O_RDONLY );
   if ( fd == -1 )
   {
        perror ( argv[1] );
        exit ( EXIT_FAILURE );
   }

   /* fcntl(2) is more portable */
   /* flock(2) doesn't lock the fd, but the inode */
   ret = flock ( fd, LOCK_EX | LOCK_NB );
   if ( ret == -1 )
   {
        perror ( argv[1] );
        exit ( EXIT_FAILURE );
   }

   /* Do the processing here */
   sleep ( 10 );

   ret = flock ( fd, LOCK_UN );
   if ( ret == -1 )
   {
        perror ( argv[1] );
        exit ( EXIT_FAILURE );
   }

   return EXIT_SUCCESS;
}
--
Vijay Kumar R Zanvar