ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
> 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.
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);
}
}
> 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.