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 ]
|