% core systems exam, sep 2008
%
%   Clark Barrett (PL1)
%   Ben Goldberg (PL2),
%   Allan Gottlieb (OS1),
%   Vijay Karamcheti (OS2)
%
\documentclass[11pt]{article}
%\usepackage{psfig}{}
\usepackage{amsmath}
\usepackage{comment}
\usepackage{enumerate}
\usepackage{latexsym} % for all the original latex symbols (does not work?)
\usepackage{epsfig}
\usepackage{ifthen}
\usepackage{color}    % new epsfig needs color
\usepackage{alltt}	% used by  gottlieb's question

\input{amssym}

\setlength{\pdfpagewidth}{8.5in}
\setlength{\pdfpageheight}{11in}
\setlength{\itemsep}{.1ex}
\setlength{\textheight}{8.55in}
\setlength{\textwidth}{6.55in}
\setlength{\topmargin}{0in}
%\setlength{\headheight}{0in}
\setlength{\headsep}{5ex}
\setlength{\parindent}{0ex}
\setlength{\parskip}{1.3ex}
\setlength{\oddsidemargin}{-.25in}
\setlength{\evensidemargin}{-.25in}
%\setlength{\oddsidemargin}{-.304in}
%\setlength{\evensidemargin}{-.304in}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% answer/no answer 
% THIS IS THE ONLY CHOICE YOU NEED TO MAKE TO RUN THIS FILE!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newboolean{withAnswer}
% CHOOSE HERE (answer or no answer)
\setboolean{withAnswer}{true}
\setboolean{withAnswer}{false}
%
% setup comment environments:
\ifthenelse{\boolean{withAnswer}}{
	  \includecomment{ans}
	  \excludecomment{noans}
	}{
	  \includecomment{noans}
	  \excludecomment{ans}
	}
% define \answer depending on comment environments:
\begin{ans}
	\newcommand{\answer}[2]{
		\begin{center}
		  \fbox{ \parbox{5.5in}{ {\bf Answer:} #1 }}
		\end{center}
	}
\end{ans}
\begin{noans}
	\newcommand{\answer}[2]{ #2 }
\end{noans}

% answers that uses verbatim mode needs special treatment 
%	NOTE: to use this, enclose it with the {ans} environment:
%	E.g., \begin{ans}\begin{ansVerbatim}...\end{ansVerbatim}\end{ans}
%	Actually, does not work for all verbatim, so need another option
%	using \ansStart,\ansEnd.
\newenvironment{ansVerbatim}{
  	%\begin{center}
  	\hrule width 4in
  	{\bf Answer:}
	%\begin{verbatim} 
}{
	%\end{verbatim} 
	\hfill$\Box$
	\hrule width 4in
	%\end{center}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\ansStart and \ansEnd are
%	separator used for answers with Verbatim
% Usage: 	\begin{ans}\ansStart ... \ansEnd\end{ans}
\newcommand{\ansStart}{
	\medskip
	\begin{center}
	\null\hspace*{.5in}\downbracefill\\
	{\bf Answer:}
	\end{center}
}
\newcommand{\ansEnd}{
	%\hfill $\Box$ \hspace*{.3in} \hfill\hrule width 4in
	\begin{center}
	\null\hspace*{.5in}\upbracefill
	\end{center}
	\medskip
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Some General MACROS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\BoxItem{\item[\fbox{$\phantom{A2}$}]}
\newcommand{\dt}[1]{{\bf #1}}
\newcommand{\dd}{ ,\ldots , }
\newcommand{\beql}[1]{\begin{equation}\label{eq:#1}}
\newcommand{\eeql}{\end{equation}}
\newcommand\ignore[1]{}
\newcommand\lder{\Longrightarrow}

%MACROS for Question 2
%\newcommand\Sig{\Sigma}
%\newcommand\emm[1]{{\em#1\/}}   %%%%%% to be corrected (\em#1)
\newcommand\nin{\noindent}
\newcommand\alt{\mathop{\vert}}
\newcommand\Figr[1]{Fig.~\ref{fig:#1}}
\newcommand\Figl[1]{\label{fig:#1}}
\newcommand\eps{\epsilon}
%\newcommand\op{\mbox{\it op\/}}
\renewcommand{\$}[1]{$#1$}
\renewcommand\lor{\quad\mathop{\vert}\quad}
\newcommand\derives{\quad\to\quad}
\newcommand\yields{\derives}
\newcommand\mvert{\lor}
\newcommand\stder{\buildrel *\over{\Longrightarrow}}
\newcommand\bd[1]{\mbox{\bf #1}}
\newcommand\Rd[1]{\mbox{$#1$}}
\newcommand\lan{\langle}
\newcommand\ran{\rangle}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% prog environment
\newenvironment{progb}[1]{
                \begin{small}
                \begin{tabbing}
                mmm\=mmm\=mmm\=mmm\=mmm\=mmm\= \kill
                #1 \end{tabbing}
                \end{small}
                }{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% prog environment
\newenvironment{prog}{\begin{tabbing}
 xxxx\=xxxx\=xxxx\=xxxx\=xxxx\=xxxx\=xxxx\=xxxx\=xxxx\=xxxx\=xxxx\=xxxx\=xxxx\=
\kill\\}{
    \end{tabbing}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% boxed version of prog environment
%%%%% Synopsis: \begin{progb}{...}\end{progb}
\newenvironment{progc}[1]{\begin{center}
      \fbox{\begin{minipage}{0.75\textwidth}
            \vspace*{-\abovedisplayskip}
        \begin{prog}#1\end{prog}
%       \vspace*{-\belowdisplayskip}
          \end{minipage}}
        \end{center}}{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{center}
{\Large  \bf
\begin{ans}
(VERSION WITH ANSWERS)\\ 
\end{ans}
CORE EXAMINATION\\
Department of Computer Science\\
New York University\\
September 26, 2008 \\
\hspace*{2em}}
\end{center}

This is the common examination for the M.S. program in CS. It covers three
core computer science topics: Programming Languages, Operating
Systems, and Algorithms. The exam has two parts. The Systems Part
(PL and OS) lasts two and one half hours and covers the first two topics.
The Algorithms Part (Algo),
given this afternoon, lasts one and one-half hours, and covers
the last topic.

You will be assigned a seat in the examination room.

Use the proper booklet or answer sheet for each question. Each
booklet is marked with the Area and Question number, in the form
PL1, PL2, OS1, OS2, ALG1, ALG2 and ALG3.
%But ALGS3 question has an answer sheet and not a booklet.
DO NOT put your name on the exam booklet or answer sheet.
Instead, your exam number must be on every booklet.

You will be graded according to your exam number, shown on the
envelope containing the booklets. Remember your exam number: when
grades are given out, they will be published according to this
number, not by name.

Make sure your name and signature are on the envelope. This is the
only place where your name appears. Please include all the booklets
inside the envelope. You can keep the exam.

Good luck!

\newpage
\vspace*{-0.3in}

\begin{center}
{\Large \bf SYSTEMS PART:\\
Programming Languages and Operating Systems}
\end{center}

%\vspace*{-4ex}
\hrule

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\subsection*{Question 3---Please
use the Exam Booklet labeled OS1}
% allan gottlieb
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\setlength{\parindent}{0pt}
\setlength{\parskip}{3pt}
\newcounter{i}

\newenvironment{ajg-compact-numbered-list}
{
  \begin{list}
    {\arabic{i}.}
    { \usecounter{i}
      \setlength{\parsep}{-3pt}
      \setlength{\parskip}{0pt}
    }
  }
  {\end{list}}


\textbf{Process Coordination and I/O}

\medskip

Consider the following three processes P1, P2, and P3 written in
pseudocode.

\begin{alltt}
Process P1              Process P2            Process P3

P(S)                    P(T)                  P(U)
Compute(10)             Compute(20)           Compute(30)
Disk(10,100)            Disk(100,10)          Disk(1,1000)
V(T)                    V(U)                  V(S)
\end{alltt}

P(X) and V(X) are binary semaphores that do not busy-wait.
Specifically:
If the (binary) semaphore X is open, V(X) is a nop and P(X) closes X.
If X is closed, P(X) blocks the process and V(X) opens X (thus
allowing \textbf{one} process blocked on X to proceed and re-close X).

When execution begins the U semaphore is open and the S and T
semaphores are closed.

We make several simplifying assumptions
\begin{ajg-compact-numbered-list}
\item Context switching takes \textbf{zero} CPU time.
\item Disk(M,N) takes \textbf{zero} CPU time.
\item P(X) and V(X) take \textbf{zero} CPU time.
\item Compute(Y) takes Y ms of CPU time
\end{ajg-compact-numbered-list}

Thus we see that the processes require 10ms, 20ms, and 30ms of CPU
time and hence the entire job requires 60ms of CPU time.
(Recall that ms abbreviates millisecond and 1ms=$10^{-3}$seconds.)

Another simplification is that
this system uses a \textbf{non}-preemptive scheduler.
So a process is \textbf{NOT} involuntarily moved from the running to
the ready state.
Of course it can be blocked.
Two important reasons why a process would be blocked are
\begin{ajg-compact-numbered-list}
\item The process initiates I/O or
\item The process executes P(X) on a closed semaphore X.
\end{ajg-compact-numbered-list}

Assume that at time zero, P1, P2, and P3 are all created and are the
only processes in the system.

\subsection*{Part A (5 Points)}

For Part A assume that Disk(M,N) is a nop.
When does each of the three Compute(Y) procedures start and when do
they end?
When does each of the three processes terminate?

\begin{ans}
\textbf{Answer}

\begin{ajg-compact-numbered-list}
  \item Initially, only semaphore U is open so only process P3 can run;
  \item P(U) takes 0ms; Compute(30) begins at 0ms.
  \item Compute(30) ends at 30ms.
  \item Disk(1,1000) and V(S) take 0ms; P3 terminates at 30ms.
  \item Only S is open; P(S) takes 0ms; Compute(10) begins at 30ms.
  \item Compute(10) ends at 40ms.
  \item Disk(10,100) and V(T) take 0ms; P1 terminates at 40ms;
  \item Only T is open; P(T) takes 0ms; Compute(20) begins at 40ms.
  \item Compute(20) ends at 60ms.
  \item Disk(100,10) and V(U) take 0ms; P2 terminates at 60ms.
\end{ajg-compact-numbered-list}
\end{ans}


\subsection*{Part B (4 Points)}

For Part B, Disk(M,N) is no longer a nop, but it still is assumed to
require zero CPU time.
Specifically, Disk(M,N) performs M disk read operations.
These M reads are directed at disk locations that are far apart
from each other; however, note that each single read itself is
directed at a contiguous disk block of N KB
(recall that KB abbreviates Kilobyte and 1KB=$10^{3}$Bytes).
Note that each of the processes reads a total of 1MB
(recall that MB abbreviates megabyte and that 1MB=$10^{6}$Bytes).

To determine the I/O time required for Disk(M,N) assume all M reads are
directed at a single disk with the following characteristics
\begin{ajg-compact-numbered-list}
  \item The block size is N.
  \item The seek time is 5ms.
  \item The rotation rate is 6000 RPM (RPM abbreviates revolutions per
    minute).
  \item The transfer rate is 10MB/second (after the head has reached the
    beginning of the block).
\end{ajg-compact-numbered-list}

\smallskip

What is the total I/O time required for Disk(1,100), Disk(1,10), and
Disk(1,1000), i.e. for \textbf{ONE} I/O operation of each of the three
sizes.

\begin{ans}
\textbf{Answer}

6000 Revolutions per minute = 100 revolutions per second.
So one revolution = 1/100 seconds = 10ms.
The (average) rotational latency is 1/2 a revolution or 5ms.

Thus each of the operations has a seek of 5ms and a rotational latency of
5ms.

The transfer time for an n KB operation is n KB / (10MB/sec).

For Disk(1,100): each transfer takes 100KB / (10MB/sec) = 10ms, \\
so the total time for Disk(1,100) is \\
\hspace*{.2in}seek + rot latency + transfer = 5ms+5ms+10ms = 20ms.

For Disk(1,10): each transfer takes 10KB / (10MB/sec) = 1ms, \\
so the total time for Disk(1,10) is \\
\hspace*{.2in}seek + rot latency + transfer = 5ms+5ms+1ms = 11ms.

For Disk(1,1000): each transfer takes 1000KB / (10MB/sec) = 100ms, \\
so the total time for Disk(1,1000) is \\
\hspace*{.2in}seek + rot latency + transfer = 5ms+5ms+100ms = 110ms.
\end{ans}


\subsection*{Part C (1 Point)}

Using the assumptions of part B and the processes of part A, when does
each of the three Compute(Y) procedures start and end; when does each
of the three Disk(M,N) procedures start and end; and when does each
process terminate?

\begin{ans}
  \textbf{Answer}

  \smallskip

  Now we repeat the analysis of part A plugging in the values for
  Disk(N,M) that we just computed.

  \begin{ajg-compact-numbered-list}
  \item Initially, only semaphore U is open so only process P3 can
    run;
  \item P(U) takes 0ms; Compute(30) begins at 0ms.
  \item Compute(30) ends at 30ms.
  \item Disk(1,1000) starts at 30ms.
  \item Disk(1,1000) takes 110ms (Part B) so ends at 140ms.
  \item V(S) takes 0ms; P3 terminates at 140ms.
  \item Only S is open so P1 runs; P(S) takes 0ms; Compute(10) begins
    at 140ms.
  \item Compute(10) ends at 150ms.
  \item Disk(10,100) starts at 150ms.
  \item Disk(10,100) takes $10 \times 20ms = 200ms$ so ends at 350ms.
  \item V(T) take 0ms; P1 terminates at 350ms;
  \item Only T is open so P2 runs; P(T) takes 0ms; Compute(20) begins
    at 350ms.
  \item Compute(20) ends at 370ms.
  \item Disk(100,10) starts at 370ms.
  \item Disk(100,10) takes $100 \times 11ms = 1100ms$ so ends at
    1470ms.
  \item V(U) take 0ms; P2 terminates at 1470ms.
  \end{ajg-compact-numbered-list}
\end{ans}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\subsection*{Question 4---Please
use the Exam Booklet labeled OS2}
% vijay karamcheti
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

{\bf Virtual Memory:}
\medskip

A user process executes a program that begins as follows:

\begin{alltt}
   int  fd;                   // file descriptor
   char buf[4];               // buffer to hold data from the file

   fd = Open("fileX", "r");   // open fileX for reading
   Read(fd, buf, 4);          // read 4 bytes from fileX into buf
\end{alltt}

{\tt Open} and {\tt Read} are system calls for opening a file, and
reading information from it respectively. Recall that system calls in
most modern operating systems (OS) are implemented using a trap
(exception) mechanism, which transfers control to the operating
system. For the {\tt Read} system call, the OS code
retrieves the call arguments, performs the requested operation, and
passes back the response by writing into memory locations that are
accessible by the user process.

When the OS handles the exception caused by the {\tt
Read} system call above, it receives the parameter values {\bf 3},
{\bf 998}, and {\bf 4}, corresponding to the parameters {\tt fd}, {\tt
buf} (the address of the buffer), and the number of bytes that need to
be read.  Assume that the first four bytes of the file {\tt fileX} are
the characters {\tt a}, {\tt b}, {\tt c}, and {\tt d} (in that
order). The virtual memory system uses a page size of {\bf 1000}
bytes. 

At the time of the {\tt Read} call, the page table for the user
process looks like this:

\smallskip
\begin{tabular}{|c|c|c|c|c|} \hline
{\em Page Number} & {\em Frame Number} & {\em Valid} & {\em Use} & {\em Dirty} \\ \hline
0 & 3 & 1 & 1 & 0 \\ \hline
1 & 1 & 1 & 0 & 0 \\ \hline
2 & 0 & 1 & 1 & 1 \\ \hline
3 & 0 & 0 & 0 & 0 \\ \hline
4 & 2 & 1 & 0 & 1 \\ \hline
\end{tabular}

Note that the {\em Use} and {\em Dirty} bits (also called the {\em
Referenced} and {\em Modified} bits) keep track of page usage.
%
%% When a page is written to, the hardware sets the {\em Dirty} bit.  The
%% {\em Use} bit is set when a page is referenced, either for reading or
%% writing.
%
These bits are set by hardware and unset in specific ways by page
replacement algorithms to control which pages to evict.


% \subsubsection*{Part A (2 points)}
\subsubsection*{Part A (3 points)}
%
The page table above controls how virtual addresses generated by the user
process are translated into physical addresses.

To which {\bf physical addresses} will the OS write the characters
{\tt a}, {\tt b}, {\tt c}, and {\tt d}, so that they will be placed
properly in the process' buffer when the {\tt Read} call returns?


\begin{ans}
{\bf Answer:}

Note that the characters {\tt a}, {\tt b}, {\tt c}, and {\tt d} need
to be written to the locations corresponding to {\tt buf[0]}, {\tt
buf[1]}, {\tt buf[2]} and {\tt buf[3]} in the process' virtual address
space. These locations correspond to virtual addresses {\bf 998}, {\bf
999}, {\bf 1000}, and {\bf 1001} respectively.

Of these, addresses {\bf 998} and {\bf 999} belong to page 0 and
addresses {\bf 1000} and {\bf 1001} to page 1. Since page 0 maps to
frame 3 and page 1 to frame 1, the physical addresses corresponding to
these virtual addresses are:

\smallskip
\begin{tabular}{|c|c|} \hline
Virtual Address & Physical Address \\ \hline
998 & $(3\times 1000 + 998) = 3998$ \\ \hline
999 & $(3\times 1000 + 999) = 3999$ \\ \hline
1000 & $(1\times 1000 + 0) = 1000$ \\ \hline
1001 & $(1\times 1000 + 1) = 1001$ \\ \hline
\end{tabular}

\end{ans}

\subsubsection*{Part B (3 points)}

For the system as in question A above, show the state of the process
page table {\bf after} the {\tt Read} call completes.

(Hint: Consider which fields of the page table entry would
change as a result of the {\tt Read} system call).

\begin{ans}
{\bf Answer:}

The process table after the completion of the {\tt Read} call differs
from that shown as part of question A only in the settings of the {\em
  Use} and {\em Dirty} bits associated with the pages hosting the
locations of {\tt buf}: (the changes are shown highlighted in bold)

\smallskip
\begin{tabular}{|c|c|c|c|c|} \hline
{\em Page Number} & {\em Frame Number} & {\em Valid} & {\em Use} & {\em Dirty} \\ \hline
0 & 3 & 1 & 1 & {\bf 1} \\ \hline
1 & 1 & 1 & {\bf 1} & {\bf 1} \\ \hline
2 & 0 & 1 & 1 & 1 \\ \hline
3 & 0 & 0 & 0 & 0 \\ \hline
4 & 2 & 1 & 0 & 1 \\ \hline
\end{tabular}

\end{ans}


%% \subsubsection*{Part D (4 points)}
\subsubsection*{Part C (4 points)}
%
Consider the system and process in question A above, where the virtual
memory system employs a local page replacement policy using the CLOCK
(Second Chance) algorithm. 

The process is statically allocated four physical frames 0-3 (as shown
in the first page table): page replacement is restricted to one of
these frames.  

Assume that the circular list used by the CLOCK algorithm consists
of frames inspected in increasing numeric order, i.e., in the order 0,
1, 2, 3. The CLOCK pointer currently points at frame 0.

Starting from the process page table after the {\tt Read} call (which
you computed as your answer to question B), give a sequence of three
virtual address references in the range 0 - 4999, such that if these
are the next three addresses referenced (used) by the process, each
reference will cause a {\bf page fault}. Please show all your work.


\begin{ans}
{\bf Answer:}

Recall that the CLOCK (Second Chance) algorithm maintains a circular
list of frames allocated to the process. When a request for a
non-resident page is encountered, the algorithm starts inspecting
frames beginning from the one currently pointed to by the CLOCK
pointer. If the frame is currently not in use or it's {\em Use}
(reference) bit is 0, that frame is picked as the container for the
page. Else, the frame's {\em Use} bit is reset to 0 and the CLOCK
pointer is advanced by one. This last feature of giving used pages at
least one additional chance before being evicted, is what gives the
algorithm its `Second Chance' name.

Given the description above, to completely answer the question for
full credit, we need to identify both a virtual address reference that
will trigger a page fault and describe the functioning of the CLOCK
algorithm in sufficient detail to justify which frame is picked by the
page replacement algorithm to satisfy the fault.

We start with the frames and their use bits in the following state
(all frames are initially in use):

\smallskip
\begin{tabular}{|c|c|c|c|} \hline
{\em CLOCK pointer} & {\em Frame Number} & {\em Use} & {\em Page Number} \\ \hline
* & 0 & 1 & 2 \\ \hline
& 1 & 1 & 1 \\ \hline
& 2 & 0 & 4 \\ \hline
& 3 & 1 & 0 \\ \hline
\end{tabular}

Our first reference clearly needs to be to a non-resident page to
trigger a page fault. Therefore, this address needs to be on page 3,
say address {\bf 3001}. To service this fault, we run the CLOCK
algorithm starting from frame 0 to find a frame for holding this
page. The algorithm stops at frame 2, the first frame to not have its
{\em Use} bit set. Page 4, the current occupant of frame 2, is
evicted, and the frame is assigned to the page of the faulting access
- page 3. At this point, the CLOCK pointer points to frame 2, and the
state of the frames and their use bits looks like the following: (note
the resetting of the {\em Use} bits for frames 0 and 1)

\smallskip
\begin{tabular}{|c|c|c|c|} \hline
{\em CLOCK pointer} & {\em Frame Number} & {\em Use} & {\em Page Number} \\ \hline
& 0 & 0 & 2 \\ \hline
& 1 & 0 & 1 \\ \hline
* & 2 & 1 & 3 \\ \hline
& 3 & 1 & 0 \\ \hline
\end{tabular}

Following the same reasoning as above, the next reference needs to be
to an address on page 4 (the only non-resident page) to trigger a page
fault, say address {\bf 4001}. The operation of the CLOCK algorithm
picks frame 0 as the victim, evicting its occupant - page 2. Note that
we make the assumption that there has been no process activity in the
interim outside our control that sets the {\em Use} bits in the
table. The state of the frames after this step is the following:

\smallskip
\begin{tabular}{|c|c|c|c|} \hline
{\em CLOCK pointer} & {\em Frame Number} & {\em Use} & {\em Page Number} \\ \hline
* & 0 & 1 & 4 \\ \hline
& 1 & 0 & 1 \\ \hline
& 2 & 0 & 3 \\ \hline
& 3 & 0 & 0 \\ \hline
\end{tabular}

The third reference therefore needs to be to a location on page 2 (the
only non-resident page), say address {\bf 2001}.

\end{ans}



\end{document}
