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.
Can someone tell me how to lock files while processing them. In a previous Unix version I could use /usr/spool/usr.lock to lock a mail file while processing it.
I'm writing utilities to allow users to change their passwords and read their mail over the web. During the brief moment while the file is being updated, in the case of the shadow file if the user changes it, or the mail file once the user connects to it, I wouldn't want to risk currupting the file during a clash update by more than one program.
I would like to thank everyone who thought about my question about file locking. The key is fcntl() for C and the library module Fcntl ':flock' for Perl. I'm going to have to look around and see what I see for PHP later. It appears the file.lock that I was using, wasn't really doing the trick. I was just kind of luck that my apps had went in did it work and exited before damage from other programs were done.
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,602
Rep:
Sorry Larry - for some reason I did not see this thread. For PHP it would look something like
Code:
<?php
$file_name = "data.dat";
$file_pointer = fopen($file_name, "a");
$lock = flock($file_pointer, LOCK_EX);
// Use 2 instead of LOCK_EX if you use < PHP4.0.2
if ($lock) {
fseek($file_pointer, 0, SEEK_END);
// Use this if < PHP4.0RC1 : fseek($file_pointer, filsize($file_name));
fwrite($file_pointer, "what u wanna write");
flock($file_pointer, LOCK_UN);
// Use 3 instead of LOCK_UN if you use < PHP4.0.2
}
fclose($file_pointer);
print "data written to file successfuly";
?>
Jeremy. Thanks a bunch for the code. I'm gong to work on setting it up and testing it out as soon as I take a break from the Perl project. I'll let you know how it works out. At a glance it seems to contain just about everything I need.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.