AKB Forums

Go Back   AKB Forums > Technical sections > Languages, Compilers and Interpreters > Web Development
Home Register Blogs FAQ Members List Calendar Downloads Arcade Mark Forums Read

Web Development Java/PHP/Perl/ASP and more

Troubles when posting message? Click here! :: Проблемы с отправлением сообщения? Нажмите сюда!

Reply
 
LinkBack Thread Tools Display Modes
Old Feb 23, 2002, 17:38   #1
Administrator
 
acid's Avatar
 
Join Date: Sep 2001
Location: South Korea, Gumi
Posts: 7,277
Blog Entries: 15
Rep Power: 10
Reputation: 372
Question Locking a file

How do I lock a certain file so other instances of my perl script inquire that it is locked and wait until it will be released. Is there any common way to do that. Example would be great.
__________________

http://big-acid.blogspot.com/
acid is offline   Reply With Quote Quote selected
Old Feb 23, 2002, 17:55   #2
Консервативный Демагог
 
VX's Avatar
 
Join Date: Jan 2002
Location: Кавказская Албания
Posts: 889
Rep Power: 7
Reputation: 10
Post

use Fcntl qw(:flock);#importiruem constatti dlya flock()-a

open(FH, ">/path/to/file")
or die "Can't open file: $!\n";
#otkrivaem dlya zapisi, vsya prejnyaya informaciya budet poterena

flock(FH, LOCK_EX); #DELAEM EXLUSIVE LOCK, ctob bol'she nikto ne trogalb@ etot file

..your staff here ..

(btw ne obyazatel'no otkrivat' dlya zapisi, mojno takje dlya cteniya , ctob sluchainno vo promejutok cteniya fila tam nicego ne izmenilos'bi)
close(FH); # pri vizove close, file avtpmatocheski razblokiruetsa, (mojno bilo takje zdelat' tak flock(FH, LOCK_UN);close(FH); )

.. that's it <img src="smooch-1.gif" border="0">

no k sojalenu blockirovka filov ne realizoavanna na vsex platformax v chasnist' na win9x, dos etc..


a teper' primer:
<blockquote><font size="1" face="MS Sans Serif, Verdana, Helvetica, sans-serif">code:</font><hr><pre>
#!/usr/bin/perl

#useradd pl, prostenk' script dlya dobavleniya #userov v unix, bez ispol'zovaniya tenevix paroley
#usage useradd.pl user cryptpass shell
#(c) VX

use Fcntl qw (:flock);
use strict;

my $uid = &getfreeid('u');
my $gid = &getfreeid('g');

open(PASSWD, ">>/etc/passwd") or die;
flock(PASSWD, LOCK_EX);
echo $ARGV[0].':'.$ARGV[1].":$uid:$gid:".$ARGV[2].":$homedir\n";

close(PASSWD);


sub getfreeid{

my ($mode) = @_;

open(PW,"/etc/passwd");
flock(PW, LOCK_EX);
my $id = undef;
my $f;
if ($mode eq 'g'){
$f = 3;
}elsif( $mode eq 'u')
$f = 2;
}
my $max = 0;
while(<PW&gt <img src="wink-5.gif" border="0"> {
my $id = (split(/:/,$_))[$f]
if ($max < $id){
$max = $id;
}
}
close (PW);
return $max+1;
}

</pre><hr></blockquote>
__________________
Праздник к нам приходит...

|^^^^^^^^^'''^\| ||\__
| ВОДКА-ВОДКА | ||','''|'''''''\_____,_
| _..... _ | ||_ _|'__|_____||.........| |
'(@)'(@)'(@)''''''''''''''''''''''*|(@)""""|(@)*
VX is offline   Reply With Quote Quote selected
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 12:21.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
This board was founded on September 29, 2001
Powered by Viper Internet

Affordable Web Hosting | ParevNet

Buy text link