Thursday, 4 December 2014

program to lock a file


#include"myHeader.h"
#include<sys/stat.h>
#include<sys/ipc.h>
main()
{
struct flock lockbuf;
int fd;
fd=open("datafile",O_RDWR);
getchar();
printf("enter to lock file...\n");
lockbuf.l_type=F_WRLCK;
lockbuf.l_whence=0;
lockbuf.l_start=0;
lockbuf.l_len=0;
if(fcntl(fd,F_SETLKW,&lockbuf)<0)
perror("fcntl");
printf("LOCKED");
close(fd);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out

enter to lock file...
fcntl: Bad file descriptor
LOCKED