% core systems exam, feb 2008
%   Ben Goldberg, Allan Gottlieb, Robert Grimm, Amir Pnueli, Chee Yap
\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
\input{amssym}

\setlength{\parskip}{1ex}
\setlength{\textheight}{8.9in}


\newboolean{withAnswer}
% CHOOSE ONE (with answer or no answer)
% THIS IS THE ONLY CHOICE YOU NEED TO DO IN THIS FILE!
\setboolean{withAnswer}{false}
\setboolean{withAnswer}{true}
%
% 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{4.5in}{ {\bf Answer:} #1 }}
		\end{center}
	}
\end{ans}
\begin{noans}
	\newcommand{\answer}[2]{ #2 }
\end{noans}


%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}


\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 28, 2007 \\
\hspace*{2em}}
\end{center}

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

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
PL\&C1, PL\&C2, PLC\&C3, OS1, OS2, ALGS1 and ALGS2.
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 Programming Languages, Compilers and Operating Systems Questions}
\end{center}

%\vspace*{-4ex}
\hrule

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection*{Question 1 --
please use the Exam Booklet labeled PL\&C1}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SAMPLE! 
{\bf Concurrency: }
For this question, you need to know either Java or Ada95, but not both.

\begin{enumerate} 

\item
What is meant by ``concurrency'' in this context, particularly 
if programs are executed on a single-processor machine?

\answer{
In this context, concurrency means the simultaneous execution -- from a
logical perspective, not necessarily actual simultaneity -- of multiple
portions of a program. Each portion is typically referred to as a
``task'' (as in Ada) or ``thread'' (as in Java). Because tasks or
threads might not actually execute simultaneously, as is the case
on a single-processor machine, concurrency means that the relative order of
events (e.g. instructions) in two concurrent tasks or threads is left
unspecified.
}{}

\item 
Write a simple example in Java or Ada of a program that exhibits
concurrency.

\begin{ans}
		\begin{center}
		\hrule
		  % \fbox{ \parbox{5in}{
		  {\bf Answer:}
Java:\begin{verbatim} 
class MyThread extends Thread {

    int n;
    MyThread(int j) {
        n = j;
    }
}

\end{verbatim} 

% }}
\hfill$\Box$ \hrule
\end{center}
\end{ans}

\end{enumerate} 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\subsection*{Question 2 --
please use the Exam Booklet labeled PL\&C2}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

{\bf Fun with Functional Lists:}
Remember that both Scheme and ML have a built-in list data structure,
where a list
% is a sequence of pairs with the heads pointing to the
% actual elements and the tails pointing to more pairs or the null/nil
% value to terminate the list.  
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\subsection*{Question 3 --
please use the Exam Booklet labeled PL\&C3}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Consider the following five context-free grammars 
(A)---(E) below. For each grammar, we have corresponding string.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\subsection*{Question 4 --
please use the Exam Booklet labeled OS1}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

{\bf Demand Paging:} 
Recall that one millisecond (1ms) is 1/1000 seconds,
one microsecond (1$\mu$s) is $10^{-6}$ seconds and
one nanosecond (1ns) is $10^{-9}$ seconds.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\subsection*{Question 5 --
please use the Exam Booklet labeled OS2}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

{\bf File Organization:}
Consider the physical organization of files using
disk blocks.
For this question, the block size is $\beta$ byte, and
each block number is $32$-bits (or $4$ bytes).
Also, assume a separate file directory for the logical
organization of files.

\begin{enumerate}
\item (2 Point)
	Describe the FAT method for physical file organization.

	\answer{
	We use a table called FAT (for File Allocation Table) which
	normally resides in main memory.  There is an entry in FAT for
	each disk block (the block number is an index into FAT).
	Each entry, if it represents a block of some file,
	stores the block number of the next block in the file.  
	The entry is $-1$ if the block is the last block of its file.
	Thus FAT represents the linked list structure of
	all files in the disk.  
	}{}

\item (3 Points)
	Give a high-level step-by-step account for
	how to find the $n$th byte in file ``foobar'' under
	the FAT method.  Indicate how the file directory
	and the block size $\beta$ are used, and how to
	detect error when $n$ exceeds file size.

	\answer{
	\\1. Use the file directory structure to find the first block number
	of file ``foobar''.   
	\\2. Use FAT to find the block number of
	$\lceil n/\beta\rceil$-th block of ``foobar'' (i.e., follow
	the links in FAT for $\lceil n/\beta\rceil -1$ times).
	If we encounter $-1$ while doing this, return error
	(``file has less than $n$ bytes'').
	\\3. Read this block into memory.  
	The number of bytes used by the last block
	ought to be computable (e.g., it might be
	stored in the header of this block.)
	\\4.
	If $(n\%\beta)$ is more than this number, return error.
	Otherwise, return the $(n \% \beta)$-th byte of this block.
	}{}

\item (2 Point)
	Describe the I-node method for physical file organization,
	emphasizing the information in I-nodes that supports
	efficient file access.

	\answer{
	The directory containing a given file will store
	a pointer to an I-node.  This I-node
	stores ownership information (user ID, group ID, etc)
	protection information (file permissions),
	and bookkeeping information (time of creation, etc).

	For access to file data, it stores 12 block numbers, corresponding
	to the first 12 data blocks for the file.
	Thus, access to the first $12\beta$ bytes is very efficient.  
	Then, it has 3 additional
	indirect block pointers (single indirect, double indirect
	and triple indirect).  The first indirect
	block holds $\beta/4$ block numbers, and hence they
	can access $\beta^2/4$ additional bytes.  
	The double indirect pointers points to a block that
	holds $\beta/4$ single indirect pointers.
	Using the these indirect pointers, we
	can acess up to $\beta^3/16$ additional bytes.
	The third indirect is not needed in 32-bit address space
	for typical values of $\beta$. E.g., $\beta=4K$.
	}{}

\item (3 Points)
	Give a high-level step-by-step account for
	how to find the $n$th byte in file ``foobar'' under
	the I-Node method.  Indicate how the file directory
	and the block size $\beta$ are used.

	\answer{
	\\1.  Use the file directory to get to the $I$-node for ``foobar''.
	\\2.  Let $b=\lceil n/\beta\rceil$. 
	\\3.  If $b < 12$, then we can directly use the address of
		the $b$th block from the I-node and read into memory.
	\\4.  Otherwise, if $b<12+\beta/4$, we can read the first
		block given by single indirect pointer, and obtain
		the desired block number.  This can be read into memory.
	\\5.  Otherwise,if $b<12+\beta/4+\beta^2/16$, we can read the
		use the double indirect pointer to read a suitable
		single indirect pointer, which will contain the
		block containing the $n$th byte.
	\\6.  Once the block from steps 3, 4 or 5 is in memory,
		we can return the $(n\mod \beta)$th byte of this block.
	}{}

\end{enumerate}

\end{document}
