Implementing exclusive file lock
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Implementing exclusive file lock




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Implementing exclusive file lock  
seema


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-08-05 11: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






[ Post a follow-up to this message ]



    Re: Implementing exclusive file lock  
Bit Twister


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-08-05 11: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.





[ Post a follow-up to this message ]



    Re: Implementing exclusive file lock  
Fletcher Glenn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-08-05 11: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






[ Post a follow-up to this message ]



    Re: Implementing exclusive file lock  
Gordon Burditt


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-08-05 11: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





[ Post a follow-up to this message ]



    Re: Implementing exclusive file lock  
Thobias Vakayil


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-08-05 11: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





[ Post a follow-up to this message ]



    Re: Implementing exclusive file lock  
joe@invalid.address


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-08-05 11: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:29 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register