\documentclass[twocolumn]{article}

\setlength{\oddsidemargin}{-0.5in}
\setlength{\evensidemargin}{-0.5in}
\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{7.0in}
\setlength{\textheight}{9.5in}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.05in}
\setlength{\columnseprule}{0.3pt}
\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{hyperref}

\begin{document}

Name: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

Directions: {\bf Work only on this sheet} (on both sides, if needed); do not
turn in any supplementary sheets of paper. There is actually plenty of room
for your answers, as long as you organize yourself BEFORE starting writing.

{\bf IMPORTANT NOTE:}  In all problems that ask you to modify an
existing program, your answer should consist of statements of the form
``Delete line 23,'' ``Replace line 168 by the following code,'' and
``Between lines 8888 and 8889 insert the following code.''  Also, if one
part of a multipart problem asks you to modify code, the modifications
apply to that part only.

{\bf 1.} (10) Consider the program {\bf SMP.py}.  As we run a {\bf
Processor} thread, what variable stores the ID number of the memory
unit, if any, currently being used by this thread?

{\bf 2.}  This problem concerns our example program {\bf CallCtr.py}
(the ``nurse pool'').

\begin{itemize}

\item [(a)] (15) Add code that computes the mean lifetime of all
timebombs.

\item [(b)] (15) Add code, using {\bf Monitor.timeAverage()}, that
computes and prints the average number of nurses online.

\item [(c)] (15) Give an expression, in terms of program variables, for
the number of patients currently talking to a nurse.

\end{itemize}

{\bf 3.} Consider our example program {\bf QoS.py}, which simulates a
video/data transmission channel.

\begin{itemize}

\item [(a)] (15) Suppose that we had inadvertently omitted the {\bf return}
statement on line 46.  Then which one of the following would occur?

   \begin{itemize}

   \item [(i)] The program would deadlock.

   \item [(ii)] The program would halt from an execution error due to an
   empty event list.

   \item [(iii)] The program would go into an infinite loop.

   \item [(iv)] The program would run to completion, but the reported
   mean delay would be too small.

   \item [(v)] The program would run to completion, but the reported
   mean delay would be too large.

   \end{itemize}

\item [(b)] (15) Add code to {\bf main()} (nowhere else) that will compute
and print out the mean number of data packets in the system.  

\end{itemize}

{\bf 4.} (15) Add a function {\bf PropLess()} to the {\bf Monitor} class
that computes and returns the fraction of recorded observations that are
less than {\bf Cutoff}, where the latter variable is the sole argument
to {\bf PropLess()}.

{\bf Solutions:}

{\bf 1.} {\bf self.Module}

{\bf 2a.}

\begin{Verbatim}[fontsize=\relsize{-2}]
41.5:      self.TBStarted = None
           self.TBMon = Monitor()
51.5:      self.TBStarted = now()
        def EndTimeBomb(self):
           self.cancel(self.TB)
           self.TBMon.observe(now()-self.TBStarted)
           self.TB = None
           self.TBStarted = None
63,64:     self.EndTimeBomb()
77.5:      self.EndTimeBomb()
153.5:     print 'mean lifetime of timebombs =',G.NrsPl.TBMon.mean()
\end{Verbatim}

{\bf 2b.}

\begin{Verbatim}[fontsize=\relsize{-2}]
41.5:      NPtsTalking = 0
           PtsTalkingMon = Monitor()
111.5:     G.NrsPl.NPtsTalking += 1
           G.NrsPl.PtsTalkingMon.observe(G.NrsPl.NPtsTalking)
112.5:     G.NrsPl.NPtsTalking -= 1
           G.NrsPl.PtsTalkingMon.observe(G.NrsPl.NPtsTalking)
153.5:     print 'mean number of patients talking =', \
              G.NrsPl.PtsTalkingMon.timeAverage()
\end{Verbatim}

{\bf 2c.} 

{\bf PtClass.NPtsInSystem-len(G.NrsPl.Rsrc.waitQ)} 

{\bf 3a.} The video packets which should get discarded are retained,
making the queue, and queuing time, longer, so the answer is (v).

{\bf 3b.}  Use Little's Rule:

\begin{Verbatim}[fontsize=\relsize{-2}]
MeanDataDelay = DataArrivals.TotWait/DataArrivals.NSent
print 'mean number data packets in system:', DataArrivals.DArrRate * MeanDataDelay
\end{Verbatim}

Note that it doesn't matter that the data packets are mixed in with the
video packets.  Little's Rule is solely an issue of flow.  So, if we view
the data packets in isolation, ignoring video, and still get the correct
mean count.

{\bf 4.}

\begin{Verbatim}[fontsize=\relsize{-2}]
def PropLess(self,Cutoff):
   count = 0
   for tup in self:
      if tup[1] < Cutoff: count += 1
   return float(count)/len(self)
\end{Verbatim}

This could be made faster and more compact by using Python's {\bf
reduce()} function.

\end{document}


