Lab Module J -- Data-Directed Programming

CISC 280 Program Development Techniques

Reading: SICP (Structure and Interpretation of Computer Programs), Section 3.4

The necessary code for this homework is at
http://cis.udel.edu/~caputo/CISC280/labJ_code.html
You can access it here

Homework exercises (Due at start of lab 07 May 01):

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 the sales 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 the total number of available tickets.

Below is make-ticket-seller

(define (make-ticket-seller)
  (let ((total-tickets-sold 0))
    (define (sell)
      (sleep (random 2)) ;sleep up to two seconds until customer arrives
      (if (> *SEATS* 0)
          (begin (customer-order)
                 (set! *SEATS* (- *SEATS* 1)) ;sell a ticket 
                 (set! total-tickets-sold (+ total-tickets-sold 1))
                 (sell)) ;continue selling
          (print-tickets-sold total-tickets-sold)
          ))
    sell))

Before you modify make-ticket-seller you should run the downloaded code several times to verify that the total tickets sold generally exceeds 100 tickets. To accomplish this run (test) then (total).