[Q] locking in CGI script

Hi,

I’m now writing a CGI script (a simple message board system).
I want to know how to lock a file.
File#flock()? Dir.mkdir()?
Please show me a sample script.

···


regards,
kwatch

Hi,

Please show me a sample script.

Here is a test script I wrote a few days ago. Do not expect good code,
it was just a test :wink:

← CUT HERE

FILENAME = “testfile”
system(“touch #{FILENAME}”) or raise

processes = Array.new
3.times do |n|
n += 1
processes << fork do
f = File.new(FILENAME)
puts “Process: #{n}: getting lock”
f.flock(File::LOCK_EX);
puts “Process: #{n}: sleeping”
sleep 3
f.flock(File::LOCK_UN);
puts “Process: #{n}: released lock”
end
end

puts “Father: waiting for children”
processes.each { |pid| Process.waitpid(pid) }
puts “Father: all children are terminated”

CUT HERE →

I hope it will help you.

Cheers,

···


Laurent

Thank you Laurent,

A method File#flock() is available on Windows?
I wonder it may be available only on *nix.

kwatch

Laurent Sansonetti laurent@datarescue.be wrote in message news:3EAE2C6C.7020700@datarescue.be

···

Hi,

Please show me a sample script.

Here is a test script I wrote a few days ago. Do not expect good code,
it was just a test :wink:

← CUT HERE

FILENAME = “testfile”
system(“touch #{FILENAME}”) or raise

processes = Array.new
3.times do |n|
n += 1
processes << fork do
f = File.new(FILENAME)
puts “Process: #{n}: getting lock”
f.flock(File::LOCK_EX);
puts “Process: #{n}: sleeping”
sleep 3
f.flock(File::LOCK_UN);
puts “Process: #{n}: released lock”
end
end

puts “Father: waiting for children”
processes.each { |pid| Process.waitpid(pid) }
puts “Father: all children are terminated”

CUT HERE →

I hope it will help you.

Cheers,

Hello,

kwatch wrote:

A method File#flock() is available on Windows?
I wonder it may be available only on *nix.

It seems that the File#flock method is weirdly implemented on Windows:

Z:>irb --simple-prompt

f = File.open(“testfile”)
=> #<File:0x2a4b030>
f.flock(File::LOCK_EX|File::LOCK_NB)
=> 0
f.flock(File::LOCK_EX|File::LOCK_NB)
Errno::ENOENT: No such file or directory - “testfile”
from (irb):3:in `flock’
from (irb):3

According to pickaxe, Ruby should return ‘false’ for the second flock()
call.

Z:>ruby -v
ruby 1.6.8 (2002-12-24) [i586-mswin32]

···


Laurent

Great.
Thank you for good advice, Laurent.

Finally, I use the following code.

…--------------------
filename = ‘sample.data’

reader lock

File.open(filename) do |file|
file.flock(File::LOCK_SH)
… operation to file …
#file.flock(File::LOCK_UN) # no need
end

writer lock

File.open(filename, ‘r+’) do |file|
file.flock(File::LOCK_EX)
… operation to file …
#file.flock(File::LOCK_UN) # no need
end
…--------------------

···


regards,
kwatch

Laurent Sansonetti laurent@datarescue.be wrote in message news:3EAF7FAE.9030501@datarescue.be

Hello,

kwatch wrote:

A method File#flock() is available on Windows?
I wonder it may be available only on *nix.

It seems that the File#flock method is weirdly implemented on Windows:

Z:>irb --simple-prompt

f = File.open(“testfile”)
=> #<File:0x2a4b030>
f.flock(File::LOCK_EX|File::LOCK_NB)
=> 0
f.flock(File::LOCK_EX|File::LOCK_NB)
Errno::ENOENT: No such file or directory - “testfile”
from (irb):3:in `flock’
from (irb):3

According to pickaxe, Ruby should return ‘false’ for the second flock()
call.

Z:>ruby -v
ruby 1.6.8 (2002-12-24) [i586-mswin32]