Hi,

I am trying to use mandatory file locks on Solaris, but facing difficulties.
I read that if the file is opened using O_NONBLOCK flag the fcntl call would
not block and return with error. My sole purpose is to check if anyone else
has write lock on the file without being blocked. Also i read that Solaris
doesn't honour O_NONBLOCK as mentioned here.

"

SunOS is so hopeless that it doesn't even honour the O_NONBLOCK flag for
mandatory locks, so reads and writes to locked files always block when they
should return EAGAIN.
"

http://slackware.osuosl.org/slackware-3.3/docs/linux-2.0.30/mandatory.txt



Is there any way to achieve the same ?? Below is the sample code.

#include <stdio.h>
   #include <stdlib.h>
   #include <errno.h>
   #include <fcntl.h>
   #include <unistd.h>

   int main(int argc, char *argv[])
   {
                        /* l_type   l_whence  l_start  l_len  l_pid   */
       struct flock fl = { F_WRLCK, SEEK_SET, 0,       0,     0 };
       int fd;

       fl.l_pid = getpid();

       if (argc > 1)
               fl.l_type = F_RDLCK;

       if ((fd = open("lock.c", O_RDWR|O_NONBLOCK)) == -1) {
               perror("open");
               exit(1);
       }

       printf("Press <RETURN> to try to get lock: ");
       getchar();
       printf("Trying to get lock...\n");

       if (fcntl(fd, F_SETLKW, &fl) <0){
               perror("fcntl");
               exit(1);
       }

       printf("got lock\n");
       printf("Press <RETURN> to release lock: ");
       getchar();

       fl.l_type = F_UNLCK;  /* set to unlock same region */

       if (fcntl(fd, F_SETLK, &fl) == -1) {
               perror("fcntl");
               exit(1);
       }

       printf("Unlocked.\n");

       close(fd);
   }




--
Thanks & Regards,
********************************************
Manish Katiyar
Ozone 2, SP Infocity (Software Park),
New Survey #208 Manjari Stud Farms Ltd.,
Phursungi Village, Haveli Taluka, Saswad Road,
Hadapsar, Pune - 412308, India
***********************************************
_______________________________________________
opensolaris-code mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code

Reply via email to