; Problem Set 11 (include-book "irun") (in-package "M5") ; Below is the definition of the multi-threaded initial state used in class on ; Thursday, April 12. (defconst *multi-threaded-state* (make state :tt (list (make thread :id 0 :cs (push (make frame :pc 0 :locs nil :stk nil :mloc '("Top" "main" 0)) nil) :stat 'active :ref nil)) :hp nil :ct (make-ct (list (make class :name "Container" :supers '("Object") :fields '("counter") :methods nil) (make class :name "Job" :supers '("Thread" "Object") :fields '("C") :methods (list (make method :name "run" :formals () :sync nil :code ;;; ("Job" "run" 0) '((load 0) ;;; 0 (getfield ("Job" "C")) ;;; 1 (store 1) ;;; 2 (load 1) ;;; 3 loop: (load 1) ;;; 4 (getfield ("Container" "counter")) ;;; 5 (load 1) ;;; 6 (getfield ("Container" "counter")) ;;; 7 (add) ;;; 8 (putfield ("Container" "counter")) ;;; 9 C.counter = C.counter+C.counter; (goto -7)) ;;; 10 goto loop :xtbl nil))) (make class :name "Top" :supers '("Object") :fields nil :methods (list (make method :name "main" :formals () :sync nil :code ;;; ("Top" "main" 0) '((new "Container") ;;; 0 \ (dup) ;;; 1 | Build Container C (const 1) ;;; 2 | C.counter = 1; (putfield ("Container" "counter"));;; 3 | (store 0) ;;; 4 / (new "Job") ;;; 5 \ (store 1) ;;; 6 | (load 1) ;;; 7 | build and start (load 0) ;;; 8 | thread 1 running (putfield ("Job" "C")) ;;; 9 | Job with C (load 1) ;;; 10 | (invokevirtual ("Thread" "start" 0));;11 / (new "Job") ;;; 12 \ (store 1) ;;; 13 | (load 1) ;;; 14 | build and start (load 0) ;;; 15 | thread 2 running (putfield ("Job" "C")) ;;; 16 | Job with C (load 1) ;;; 17 | (invokevirtual ("Thread" "start" 0));;18 / (load 0) ;;; 19 \ (getfield ("Container" "counter"));;; 20 | return C.counter (halt)) ;;; 21 / :xtbl nil))))))) ; As claimed in class, for every positive integer, n, there exists a schedule ; such that running that schedule on this state will produce n, i.e., halt with ; n on the stack. ; This conjecture can be tested be defining the following function: (defun test-sched (n sched) (equal n (top (stk (top-frame 0 (run sched *multi-threaded-state*)))))) ; and trying it for given values of n and an alleged schedule for n. ; Problem 1: Show a schedule that computes 3. That is, ; fill in the blank so that ; (test-sched 3 '...) ; returns t. Hint: If you solve Problem 3 you will get credit for Problem 1. ; Problem 2: Show a schedule that computes 5. That is, ; fill in the blank so that ; (test-sched 5 '...) ; returns t. Hint: If you solve Problem 3 you will get credit for Problem 2. ; Problem 3: ; Define the function schedule to return a suitable schedule for any positive ; integer n. After you've defined schedule you should be able to define (defun test (n) (test-sched n (schedule n))) ; and then see it evaluate to t for (test 3), (test 5), (test 17), (test 12345), ; etc.