Unix Programming - Implementing exclusive file lock

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2005 > Implementing exclusive file lock





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Implementing exclusive file lock
seema

2005-11-08, 6:29 pm

Hi all ,
I am new to unix programming . I have written a sample to
achieve Exclusive file lock. Now if i invoke a.out&;a.out&;a.out&;
2& next instances doesnt print "Cannot Lock".

int main(){
FILE *Fd;
Fd=fopen("/tmp/TeSt","w");
if (!Fd)
printf ("cannot create file \n");
if(0==lockf(Fd,2,0L)){
printf("Cannot Lock \n");
exit(0);
}
sleep(10);
fclose(Fd);
}
Can somebody explain how to achieve it using lockf(),

Thanks,
SeemaRao

Bit Twister

2005-11-08, 6:29 pm

On 7 Nov 2005 08:58:34 -0800, seema wrote:
> Hi all ,
> I am new to unix programming . I have written a sample to
> achieve Exclusive file lock. Now if i invoke a.out&;a.out&;a.out&;
> 2& next instances doesnt print "Cannot Lock".


How do you know, remove the
exit(0);
and try again.

If I had to figure out what is going on, I would spin up debug for two
instances of a.out and single step each to see what happens.
Fletcher Glenn

2005-11-08, 6:29 pm

seema wrote:
> Hi all ,
> I am new to unix programming . I have written a sample to
> achieve Exclusive file lock. Now if i invoke a.out&;a.out&;a.out&;
> 2& next instances doesnt print "Cannot Lock".
>
> int main(){
> FILE *Fd;
> Fd=fopen("/tmp/TeSt","w");
> if (!Fd)
> printf ("cannot create file \n");
> if(0==lockf(Fd,2,0L)){
> printf("Cannot Lock \n");
> exit(0);
> }
> sleep(10);
> fclose(Fd);
> }
> Can somebody explain how to achieve it using lockf(),
>
> Thanks,
> SeemaRao
>


The sense of your test is wrong. According to the man page, lockf
returns zero if the lock succeeded.

--

Fletcher Glenn

Gordon Burditt

2005-11-08, 6:29 pm

> I am new to unix programming . I have written a sample to
>achieve Exclusive file lock. Now if i invoke a.out&;a.out&;a.out&;
>2& next instances doesnt print "Cannot Lock".
>
>int main(){
> FILE *Fd;
> Fd=fopen("/tmp/TeSt","w");
> if (!Fd)
> printf ("cannot create file \n");
> if(0==lockf(Fd,2,0L)){
> printf("Cannot Lock \n");
> exit(0);
> }
> sleep(10);
> fclose(Fd);
>}
>Can somebody explain how to achieve it using lockf(),


Read the lockf() manual page. What does lockf() take as a first
argument? (int) What are you giving it? (FILE *) (fileno() might
be useful here.) What does lockf() return when it succeeds?

Note that you destroy the contents of the file *BEFORE* you get
the lock. If you're using the file only as a lock file and
don't care about the contents, that's fine.

Gordon L. Burditt
Thobias Vakayil

2005-11-08, 6:29 pm

seema wrote:

>Hi all ,
> I am new to unix programming . I have written a sample to
>achieve Exclusive file lock. Now if i invoke a.out&;a.out&;a.out&;
>2& next instances doesnt print "Cannot Lock".
>
>int main(){
> FILE *Fd;
> Fd=fopen("/tmp/TeSt","w");
> if (!Fd)
> printf ("cannot create file \n");
> if(0==lockf(Fd,2,0L)){
> printf("Cannot Lock \n");
> exit(0);
> }
> sleep(10);
> fclose(Fd);
>}
>Can somebody explain how to achieve it using lockf(),
>
>Thanks,
>SeemaRao
>
>
>

Hello,

If you will execute the following program then you can see the results :
#include <stdio.h>
#include <unistd.h>
#include <sys/file.h>
#include <fcntl.h>

int main(){
FILE *Fd;
int c;
Fd=fopen("/tmp/TeSt","w");
if (!Fd)
printf ("cannot create file \n");
c=lockf((int)Fd,1,0);
printf ("lock = %d\n",c);
if(lockf((int)Fd,1,0L)==0){
printf("Cannot Lock \n");
}
sleep(10);
fclose(Fd);
}

Create the file TeSt in /tmp directory
cd /tmp
touch TeSt
chmod 044 TeSt

After this execute the program then you can see the return value of
lockf is 0.
In other cases the return value is -1.

Regards,
Thobias
--
Thobias Vakayil
Alcatel Development India (ADI)
PH: 2349961/72/86 EXTN :7018
joe@invalid.address

2005-11-08, 6:29 pm

Thobias Vakayil <Vakayil.Thobias@alcatel.com> writes:

> seema wrote:


> If you will execute the following program then you can see the results :
> #include <stdio.h>
> #include <unistd.h>
> #include <sys/file.h>
> #include <fcntl.h>
>
> int main(){
> FILE *Fd;
> int c;
> Fd=fopen("/tmp/TeSt","w");
> if (!Fd)
> printf ("cannot create file \n");
> c=lockf((int)Fd,1,0);
> printf ("lock = %d\n",c);
> if(lockf((int)Fd,1,0L)==0){
> printf("Cannot Lock \n");
> }
> sleep(10);
> fclose(Fd);
> }
>
> Create the file TeSt in /tmp directory
> cd /tmp
> touch TeSt
> chmod 044 TeSt
>
> After this execute the program then you can see the return value of
> lockf is 0.
> In other cases the return value is -1.


Actually it prints

lock = -1

This is because you can't get the file descriptor associated with Fd
simply by casting it to an int. That only gives you the pointer value
(possibly truncated or otherwise mangled) as an integer.

As someone else suggested, fileno() is what's needed.

It would also be better to use the #defines rather than integer
constants. F_LOCK isn't guaranteed to always be 1. The OP probably
also wants LOCK_EX, given the title of this thread.


joe
--
Gort, klatu barada nikto
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com