LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2012, 02:11 AM   #1
aruns06@gmail.com
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Rep: Reputation: Disabled
Unhappy File lock using fcntl


Hi all,
Please let me know anybody know about the reason for the below program.
The first program indicates the write lock on a file and is as follows.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
struct flock obj;
int ret,w;
int fd=open("data123.c",O_WRONLY);
write(fd,"Hello world\n",11);
obj.l_type=F_WRLCK;
obj.l_whence=SEEK_SET;
obj.l_start=0;
obj.l_len=20;
ret=fcntl(fd,F_SETLK,&obj);
if(ret==-1)
perror("Not locked :");
else
{
printf("File is locked for writing \n");
w=write(fd,"Hello",5);
sleep(20);
}
}



I am compiling and ruuning the program in the first terminal. In the second terminal I am running another program which is given below.


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
int fd,ret;
char ch[10];
fd=open("data123.c",O_CREAT|O_RDWR,0666);
if(fd==-1)
perror("");
else
{
struct flock obj;
obj.l_type=F_RDLCK;
obj.l_whence=SEEK_SET;
obj.l_start=20;
obj.l_len=20;
ret=fcntl(fd,F_SETLK,&obj);
if(ret ==-1)
perror("");
else
{
read(fd,&ch,5);
printf("the content of the file is : %s\n",ch);
}

}
}

Steps:
1 First I am compiling and running the first program in terminal.
2.The very next moment I am compiling and running the second program in the second terminal.
3. Since a sleep on 20 is given to the first terminal, the terminal 1 is still in the runing mode.
4.But the compile and execute the second program in the second terminal, that moment itselt o/p will be displayed is resource is temporarly unavailable. The reason is, the first file is in read lock.


My question is, I have applied the lock in the first program which
I am supposed to do. But in the second program also I have to do the read lock
My doubt is
1 Why I have to enable the structure flock and its parameters in the second program also.
2.Without enabling the struct flock, I am not able to utlize the file locking feature.
Kindly some one explain me about this.


Regards.
Arun
 
Old 07-13-2012, 02:22 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Use tags [code] and [/code] when quoting source code.
Modification to the first program:
Code:
--- first.cold  2012-07-13 09:25:12.000000000 +0200
+++ first.c     2012-07-13 09:25:38.000000000 +0200
@@ -1,0 +2 @@
+#include <stdlib.h>
@@ -10 +11,2 @@
-int fd=open("data123.c",O_WRONLY);
+int fd=open("data123.c",O_WRONLY|O_CREAT,0666);
+if (fd==EOF) exit (12);
> Why I have to enable the structure flock and its parameters in the second program also.

You cannot and shouldn't 'enable' struct flock, only use it as parameter for fcntl. In this case the two programs lock different areas of file, so they can run in the same time.

> Without enabling the struct flock, I am not able to utlize the file locking feature.

Very true. Locking doesn't prevent anything (open/close/read/write/unlink etc) only locking.

Note: .c extension is a very bad idea for a data-file.

Last edited by NevemTeve; 07-13-2012 at 03:02 AM.
 
Old 07-13-2012, 03:31 AM   #3
aruns06@gmail.com
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Unhappy Fcntl lock

Ok.............
Fcntl-file lock. Does it means that, if I lock a file using struct file (write lock), no one will able to read the content of that file using other process with the help of the second terminal? is it like that or file lock has some some other meaning as you mentioned in the second answer( read,write etc possible evenif the lock is active) ???

Also why struct flock is required in the second program??????? If I remove the struct lock and the components even though the lock is active I am able to access the content of the file
for example:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
int fd,ret;
char ch[10];
fd=open("data123.c",O_CREAT|O_RDWR,0666);
if(fd==-1)
perror("");
else
{
read(fd,&ch,5);
printf("the content of the file is : %s\n",ch);
}
}


Why is this so?????
 
Old 07-13-2012, 03:44 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> Does it means that, if I lock a file using struct file (write lock), no one will able to read the content of that file using other process with the help of the second terminal?

No, absolutely not. I've already answered this question: locking doesn't prevent anything (open/close/read/write/unlink etc) only locking.

Note: I won't answer any more question if you don't use tags [code] and [/code] when quoting source code.
 
Old 07-13-2012, 04:02 AM   #5
aruns06@gmail.com
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Unhappy fcntl.......

Quote:
Ok
If locking will not prevet read,write etc Then why we have to use the locking and what is the application.......


Note: I am new in this kind of forumns......please forgive me if I made any mistake.. :-)

Regards,
Arun.S
!!!!!!!!!!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
open lock file /var/lib/postfix/master.lock: cannot open file: Permission denied gabsik Linux - Server 6 08-30-2012 09:39 PM
[SOLVED] Can't read lock file tmp .x0-lock xinit: stale nfs file handle everal Slackware 2 10-31-2011 07:11 AM
How to lock files using fcntl() and to work between threads of the same process mistretzu Programming 6 11-05-2007 12:45 PM
Cannot find a file : fcntl.c ankit4u1 Red Hat 6 08-06-2007 06:15 PM
Can't read lock file /tmp/.X0-lock squinn Linux - Newbie 3 07-13-2004 03:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration