Model "lift-passengers" and concurrent programming

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

producer = Thread.new do#producer
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

consumer = Thread.new do#consumer
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 “\nproducer - floor where the passenger came out of the
lift:\n\t[”+prod_to.join(’,’)+"]"
puts “\nconsumer - 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}”

Can someone give me some advice about this problem?

On 14/11/11 08:51, Roman F. wrote:

s2 = Semaphore.new(0)
d = rand(8)+1
m.V unless(good_pas)
#{fto[i]}th floor"
s2.P
end
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}”

Can someone give me some advice about rhis problem?

Can you post your semaphore implementation?

Sam

$Id: semaphore.rb,v 1.2 2003/03/15 20:10:10 fukumoto Exp $

class CountingSemaphore

def initialize(initvalue = 0)
@counter = initvalue
@waiting_list = []
end

def wait
Thread.critical = true
if (@counter -= 1) < 0
@waiting_list.push(Thread.current)
Thread.stop
end
self
ensure
Thread.critical = false
end

def signal
Thread.critical = true
begin
if (@counter += 1) <= 0
t = @waiting_list.shift
t.wakeup if t
end
rescue ThreadError
retry
end
self
ensure
Thread.critical = false
end

alias down wait
alias up signal
alias P wait
alias V signal

def exclusive
wait
yield
ensure
signal
end

alias synchronize exclusive

end

Semaphore = CountingSemaphore

I took the implementation from this site
http://www.imasy.or.jp/~fukumoto/ruby/

On Sun, Nov 13, 2011 at 9:35 PM, Roman F. [email protected]
wrote:

I took the implementation from this site
http://www.imasy.or.jp/~fukumoto/ruby/

That uses a lot of MRI internals (Thread.stop etc.). I’d rather use
other concurrency constructs to implement this, e.g.:

Kind regards

robert

On Sun, Nov 13, 2011 at 8:51 PM, Roman F. [email protected]
wrote:

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

What about the way up?

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.

Somehow I have the feeling I am missing information. Can you give a
bit more description of the problem you are trying to solve?

How do passengers decide when to enter? What’s the producer and
consumer? How do you record the current floor?

Btw, please try to properly indent code and use empty lines to
separate section. This goes a long way making your code more readable
which in turn increases the likeliness to get help. :slight_smile:

Kind regards

robert

-----Messaggio originale-----
Da: Robert K. [mailto:[email protected]]
Inviato: luned 14 novembre 2011 09:27
A: ruby-talk ML
Oggetto: Re: model “lift-passengers” and concurrent programming

On Sun, Nov 13, 2011 at 8:51 PM, Roman F. [email protected]
wrote:

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

What about the way up?

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.

Somehow I have the feeling I am missing information. Can you give a bit
more description of the problem you are trying to solve?

How do passengers decide when to enter? What’s the producer and
consumer?
How do you record the current floor?

Btw, please try to properly indent code and use empty lines to separate
section. This goes a long way making your code more readable which in
turn
increases the likeliness to get help. :slight_smile:

Kind regards

robert


remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Capodanno a Riccione, Pacchetto Relax: Mezza Pensione + bagno turco +
solarium + massaggio. Wifi e parcheggio gratis. 2 giorni euro 199 a
persona
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid978&d)-12

-----Messaggio originale-----
Da: Roman F. [mailto:[email protected]]
Inviato: domenica 13 novembre 2011 20:51
A: ruby-talk ML
Oggetto: model “lift-passengers” and concurrent programming

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

producer = Thread.new do#producer
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

consumer = Thread.new do#consumer
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}”

Can someone give me some advice about rhis problem?


Posted via http://www.ruby-forum.com/.


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Capodanno al parco Oltremare Riccione: Pacchetto hotel 3 stelle in
centro + ingresso al parco.