\documentclass{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}

\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.
In order to get full credit, {\bf SHOW YOUR WORK}.

{\bf 1.} (25) Write a single line of code which could be added to the main
{\bf while} loop in {\bf DSMSrvr.pl}, which would print to the screen
the names and values of all the shared variables.  Note that different
elements of the same array are considered different variables.

{\bf Solution:}

\begin{Verbatim}[fontsize=\relsize{-2}]
print %SharedVars
\end{Verbatim}

{\bf 2.} (25) The following function calculates the minimum of a variable
number of arguments.  Fill in the blanks.

\begin{Verbatim}[fontsize=\relsize{-2}]
sub min {
   $m = _____________
   for $x (___________) {
      if ($x < $m) {$m = $x};
   }
   return $m
}
\end{Verbatim}

{\bf Solution:}

\begin{Verbatim}[fontsize=\relsize{-2}]
sub min {
   $m = shift;
   for $x (@_) {
      if ($x < $m) {$m = $x};
   }
   return $m
}
\end{Verbatim}

{\bf 3.} (25) Add code to {\bf pth.py} which will do profiling, i.e. keep
track of how many times each of the action {\bf 'pause'}, {\bf 'wait'},
{\bf 'set'}, {\bf 'set\_all'}, {\bf 'set\_but\_stay'} and {\bf 'quit'}
are executed, over all threads.  You will have class variables {\bf prf}
and {\bf profiling}.  The former is a dictionary with keys equal to the
action names and values equal to the counts, while the latter is a
boolean, with True meaning that profiling is on.  Add two contiguous
lines for data definition/initialization, and at most three contiguous
lines for the maintaining of the counts.  In both cases, state in
between which two consecutive lines you will insert your code.

{\bf Solution:}

\begin{Verbatim}[fontsize=\relsize{-2}]
# near the beginning of thrd:
   prof = {'pause':0, 'wait':0,'set':0, 'set_but_stay':0, \
           'set_all':0, 'quit':0}
   profiling = False
...
# after setting yv1
         if thrd.profiling:
            thrd.prof[yv1] += 1
\end{Verbatim}

{\bf 4.} (25) Use Perl's {\bf tie()} function to create read-only scalars,
which also keep track of how many times such a scalar is read, in a
variable named {\bf accesscount}.  

In {\bf readonlyscalar.pm}, each blank may be filled in by between zero
and four lines (right-semicolons) of code.  In {\bf testro.pl}, fill in
blanks within a line by an expression, and fill in the fully blank line
with zero or one line of code.

{\bf readonlyscalar.pm}:

\begin{Verbatim}[fontsize=\relsize{-2}]
package readonlyscalar;
___________________________________
sub TIESCALAR {
}
sub FETCH {
   ___________________________________
}
sub STORE {
   ___________________________________
}
1;
\end{Verbatim}

{\bf testro.pl}:

\begin{Verbatim}[fontsize=\relsize{-2}]
tie $x,'ReadOnlyScalar','$x',8;
$x = 168;  # prints out an error message
$y = $x;  
print $x,"\n";  # prints 8
______________________;  # just one ";"
print _______________, "accesses so far\n";  # prints 2
\end{Verbatim}

{\bf Solution:}

\begin{Verbatim}[fontsize=\relsize{-2}]
sub TIESCALAR {
   my $class = @_[0];  # we'll create a class to be named after
                       # our tied variable
   my $r = {Name=>@_[1],Value=>@_[2],AccessCount=>0};
   bless $r, $class;
   return $r;
}

sub FETCH {
   my $r = shift;
   $r->{AccessCount}++;
   return $r->{Value};
}
sub STORE {
   print "write attempt for ",$r->{Name},"\n";
}
...
tie $x,'ReadOnlyScalar','$x',8;

$x = 168;  # prints out an error message
print $xobj->{AccessCount},"\n";  # prints 0
$y = $x;
print $x,"\n";  # prints 8
$xobj = tied $x;
print $xobj->{AccessCount},"\n";  # prints 2


\end{Verbatim}

\end{document}

