Reading: SICP (Structure and Interpretation of Computer Programs), Section 3.4
The necessary code for this homework is at
http://udel.edu/~saunders/280/labJ_code.scm
Homework exercise (Due at start of lab on 29 Apr 02): i
Exercise 1
Implement a ticket selling program with multiple ticket sellers. There will be
multiple threads of execution that are independently selling the tickets to a new
movie. There is one shared database variable that represents the tickets available
to sell. Assume all tickets are equal and the only real issue is that you do not sell
more tickets than there are seats. Each thread of execution will take a random
amount of time to sell a seat, thus ensuring a random order of the threads of
execution. For simplicity we will limit each sale to one ticket. While a particular
seller is selling, all other sellers must be excluded from access to the shared
database variable. When all of the tickets are gone, each seller should terminate
with a message indicating how many tickets were sold by that seller. Obviously
these messages should add up to no more than the total number of available tickets.
Thus your task is to modify this make-ticket-seller so that the key parts are serialized.
(define (make-ticket-seller name) (let ((total-tickets-sold 0)) (define (sell) (sleep (random 2)) ;sleep up to two seconds until customer arrives (if (customer-order *SEATS-LEFT*) (begin (set! *SEATS-LEFT* (- *SEATS-LEFT* 1)) ;record the sale globally (set! total-tickets-sold (+ total-tickets-sold 1)) ; local record (sell)) ;continue selling (print-tickets-sold name total-tickets-sold) )) sell))
Before you modify make-ticket-seller you should load the downloaded code and run (test) several times to verify that the total tickets sold generally exceeds 100 tickets. Then make sure that your code solves the problem, while serializing as little of the code as you can.