```Hi,

I'm trying to write a model of an elevator with one button. Elevator can
take passengers along the way when going down.

But in my code some error. Because passengers press a button only when
the elevator carries passengers. And no passengers push the button when
the elevator is free.

I took a producer-consumer model.

require 'semaphore'
t0 = Time.now

max=5#the maximum number of passengers
c = 10#number of iterations

tp = []#producer
tc = []#consumer

ffrom=Array.new(max)#floor, where the button is pressed
fto=Array.new(max)#floor, which requires the passenger

prod_from=[]#producer: floor where the passenger has been taken
prod_to=[]#producer:floor where the passenger has arrived
cons_from=[]#consumer: floor where the passenger has been taken
cons_to=[]#floor where the passenger has arrived

buf = Array.new(max)#buffer of lift
s1 = Semaphore.new(buf.size)#overflow protection
s2 = Semaphore.new(0)
m  = Semaphore.new(1)
first=true

i = 0
j=0
c.times do
#sleep(rand(5)%5)
good_pas=false
m.P
d = rand(8)+1
puts "button is pressed on the #{d}th floor" unless(first)
unless(first)then
if((d>fto[j])and(d<=ffrom[j]))then
good_pas=true
puts "passenger can be allowed\n"
else
puts "passenger can't be allowed\n"
end
end
j = ( j + 1 ) % buf.size unless(first)
m.V unless(good_pas)
ffrom[i]=d
fto[i]=rand(8)+1;
first=false
while(fto[i]==ffrom[i])do fto[i]=rand(8)+1;  end
prod_from<<ffrom[i]
prod_to<<fto[i]
tp << d
s1.P
m.P unless(good_pas)
puts "\nfrom the #{ffrom[i]}th floor passenger came and asked for
#{fto[i]}th floor"
buf[i] = d
m.V
s2.V
i = ( i + 1 ) % buf.size
end
end

i = 0
c.times do
s2.P
m.P
d = buf[i]
puts "\npassenger arrived from the #{ffrom[i]}th floor to the
#{fto[i]}th floor"
cons_from<<ffrom[i]
cons_to<<fto[i]
m.V
s1.V
tc << d
i = ( i + 1 ) % buf.size
end
end

producer.join
consumer.join

puts "\nproducer - floors where the passenger went into the
lift:\n\t["+prod_from.join(',')+"]"
puts "produser - floor where the passenger came out of the
lift:\n\t["+prod_to.join(',')+"]"
puts "consumer - floor where the passenger went into the
lift:\n\t["+cons_from.join(',')+"]"
puts "consumer - floor where the passenger came out of the
lift:\n\t["+cons_to.join(',')+"]"

puts "\n\n:prod_from==cons_from: #{prod_from==cons_from}"
puts "\nprod_to==cons_to: #{prod_to==cons_to}"
puts "time: #{Time.now - t0}"