# Suggestion File#lock

**URL:** <https://rubytalk.org/t/suggestion-file-lock/4405>\
**Category:** ruby-talk\
**Created:** [29 January 2003 22:40 UTC](https://rubytalk.org/t/suggestion-file-lock/4405 "2003-01-29T22:40:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ara.T.Howard1](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Ara.T.Howard1](https://rubytalk.org/u/Ara.T.Howard1)\
**Post date:** [29 January 2003 22:40 UTC](https://rubytalk.org/t/suggestion-file-lock/4405/1 "2003-01-29T22:40:16Z")

</div>

rubyists-

due to whacky behavior exhibited on various platforms regarding flock, i would  
like to suggest an implementation of a File#lock method similar the following  
one. any suggestions to improve it welcome.

file File\_lock.rb  
----CUT----

# File#lock - a method for safely locking a file between processes and/or

# threads

class File

require ‘thread’

private  
attr\_writer :sem

public  
attr\_reader :sem

def lock  
sem = Mutex.new unless sem  
sem.synchronize do  
catch (:eintr){  
begin  
loop { flock (LOCK\_EX | LOCK\_NB) and break or Thread.pass}  
rescue Errno::EINTR  
throw :eintr  
end  
}

```
  yield self

  catch (:eintr){
begin
  flock LOCK_UN
rescue Errno::EINTR
  throw :eintr
end
  }
end

```

end

end

if $0 == **FILE**

# this program coordinates access to an output file for the purpose of

# inserting a timestamp between multiple threads spawned from within multiple

# forked processes. the coordination is considered successful if no

# timestamps are inserted non-sequentially

require ‘thread’  
require ‘ftools’

pid = Process.pid

n\_child = (ARGV.shift or 4).to\_i  
n\_thread = (ARGV.shift or 4).to\_i  
n\_mark = (ARGV.shift or 2 \<\< 9).to\_i  
outpath = (ARGV.shift or ‘/tmp/out’)  
sortedpath = (ARGV.shift or ‘/tmp/sorted’)

File.rm\_f outpath  
File.rm\_f sortedpath

n\_mark\_per\_thread = n\_mark / n\_child / n\_thread  
cids =

$stdout.sync = true  
$stdout.puts “================”  
$stdout.puts “N\_CHILD : #{n\_child}”  
$stdout.puts “N\_THREAD : #{n\_thread}”  
$stdout.puts “N\_MARK : #{n\_mark}”  
$stdout.puts “OUTPATH : #{outpath}”  
$stdout.puts “================”  
$stdout.puts “\n#{pid} STARTED”

n\_child.times {  
cids \<\<  
Process.fork {  
ppid = Process.ppid  
pid = Process.pid  
sem = Mutex.new  
output = “\t#{ppid}.#{pid} STARTED\n”  
File.open (outpath, ‘a+’) { |file|  
threads =   
n\_thread.times {  
threads \<\<  
Thread.new (threads.size) { |tid|  
sem.synchronize {  
output \<\< “\t\t#{ppid}.#{pid}.#{tid} STARTED\n”  
}  
n\_mark\_per\_thread.times {  
file.lock { |f|  
f.seek IO::SEEK\_END  
f.syswrite “%10.5f\n” % Time.now.to\_f  
}  
Thread.pass  
}  
}  
}  
threads.map {|t| t.join}  
}  
$stdout.puts output  
exit!  
}  
}

cids.map { |cid| Process.waitpid cid }  
cids.map { |cid| $stdout.puts “\t#{pid}.#{cid} FINISHED” }

output = IO.readlines outpath  
sorted = output.sort {|a,b| a.to\_f \<=\> b.to\_f}

if output != sorted  
File.open (sortedpath, ‘w’) {|f| f.puts sorted }  
$stdout.puts “\nERROR!”  
$stdout.puts “SUGGEST ‘diff -c #{outpath} #{sortedpath}’”  
else  
$stdout.puts “\nSUCCESS!”  
end

end  
----CUT----

-a

> **···**
>
> –
> 
> ====================================
> 
> > Ara Howard  
> > NOAA Forecast Systems Laboratory  
> > Information and Technology Services  
> > Data Systems Group  
> > R/FST 325 Broadway  
> > Boulder, CO 80305-3328  
> > Email: [ahoward@fsl.noaa.gov](mailto:ahoward@fsl.noaa.gov)  
> > Phone: 303-497-7238  
> > Fax: 303-497-7259  
> > ====================================
