LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-02-2000, 06:48 PM   #1
Larry James
Member
 
Registered: Jun 2000
Location: Buffalo, New York
Distribution: Ubuntu, Raspbian
Posts: 381

Rep: Reputation: 40

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.

Thanks in advance for any comments.

-- L. James
 
Old 12-03-2000, 07:32 PM   #2
Larry James
Member
 
Registered: Jun 2000
Location: Buffalo, New York
Distribution: Ubuntu, Raspbian
Posts: 381

Original Poster
Rep: Reputation: 40
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.

-- L. James
 
Old 12-03-2000, 07:45 PM   #3
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,600

Rep: Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083
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"; 

?>
 
Old 12-04-2000, 11:06 AM   #4
Larry James
Member
 
Registered: Jun 2000
Location: Buffalo, New York
Distribution: Ubuntu, Raspbian
Posts: 381

Original Poster
Rep: Reputation: 40
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.

-- L. James
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Samba/Cups print sharing problems (/var/spool/samba files) Gates1026 Linux - General 9 06-23-2013 01:33 AM
Mail going to /var/spool/mail/user and not to Thunderbird pracslipkerm Linux - Newbie 4 06-21-2005 05:58 AM
sendmail error Fetching mail could not lock /var/spool/mail/username sukhdev50 Linux - Networking 0 05-04-2005 03:41 AM
Printing from Mozilla creates 200Mb files in /var/spool/cups mikeyt_333 Linux - Software 3 05-23-2004 12:47 PM
Failed to lock file /var/spool/cron/root after 5 minutes teeno Linux - Software 0 09-24-2003 02:49 AM

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

All times are GMT -5. The time now is 02:41 AM.

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