\input{myHead}

\renewcommand{\ans}[2]{#2}	% no answer
\renewcommand{\ans}[2]{#1}	% with answer

\qtitle{Final Review: OS Class}

\section{Introduction}

In this quick review, we focus on material since the
midterm.  But up to 20\% of the points may
pertain to the pre-midterm material.

\section{Pre-Midterm}
\benum
\item We sometimes characterize an OS as an ``extended machine''.
Give a detailed illustration of this characterization.
Write 2-4 sentences to explain.

\ans{
A: For instance, most
OS provides a general read(file-desc, start, num-bytes).
This command is device independent, but depending the
the devices, specific device drivers must be called, etc.
Also, this command may involve several book-keeping steps,
all hidden from users.  This command thus provide an extended
hardware service.
}{HINT: Reading from a file}

\item Name the three main architectural elements that
an OS try to manage.

\ans{
A: CPU, RAM, Disk.
}{}

\item  What exactly are we interrupting in an "interrupt"?
Why do we need them?  There are different kinds
of interrupts, and often they have specific names.
Name them and describe their functions.

\ans{
A:  Normally, "interrupts" interrupts the CPU.  We need them
because software and hardware are running asynchronously,
and they need to communicate.  These are some of the kinds
of interrupts:

TRAP: a software-generated interrupt caused by user or error

System Calls: application programs use these to access OS or kernel service.

Exception: generated by error conditions

I/O Interrupts: generated by I/O devices to communicate to the CPU.
}{}

\item What are signals?

\ans{
A: These can be viewed as interrupts between processes.
}{}

\item What is the Memory Hierarchy, and describe its
main characteristics.

\ans{
A: The hierarchy refers to a sequence of memory devices,
typically: register, cache, RAM, disk, tape.
The main characteristic of this sequence is that
the speed of the devices decreases as its capacity increases.
Thus register (1 $\mu$s/100 B), cache (10 $\mu$s/1 MB), RAM (1 ms/1 GB),
disk (100 ms/1 TB), tape (?/?).
}{}

\item Besides the speed/size difference, there
is a major difference between memory and disk
with regards to how they are accessed by the CPU.

\ans{
A: Memory reference is automatically handled by hardware
while disk reference requires an explicit request by the OS.
}{}

\item Describe the mechanics of a system call.

\ans{
A: Sibers. p.30,32.
}{HINT: Read text p.30,32}

\item 
What is the relation between a process and a program?

A: A process is a program in execution.

\item
Name the three main OS functions in managing multiprocessing.

A:
\\-- Keeping track of processes (maintaining the process table: create/delete)
\\-- Scheduling processes
\\-- Synchronization of processes
\\-- File management
\\-- Memory management
\\-- OTHERS (networking, protection, security, windowing, I/O)

\item
What are the two simplest scheduling algorithms?
What are some things left to be desired?

\ans{
A: FCFS and RR.

}{}

\item
To schedule processes optimally, we want to classify
processes into two types.  What are they, and how
do we use this classification in scheduling?

\ans{
A: CPU-bound and I/O-bound processes.
We should give priority to I/O-bound processes.
}{}

\item
What are scheduling events?

\ans{
A: there are 4 but the last two of them pertain only to
	preemptive scheduling:

	Process terminates,
	Process blocks,
	Process move from blocked to ready,
	Process move from running to ready.
}{}

\item
What are some criteria for evaluating schedulers?

\ans{
A: CPU Utilization,
	throughput,
	wait time,
	turn around time,
	fairness,
	balance,
	predictability.
}{}

\item
	In what sense and under what conditions is SJF optimal?

\ans{
A:	
	Optimality Sense: minimum wait time.
	Condition: all jobs are released at the same time.
	
	Can the condition be removed?
	Yes, suppose we can preempt.
	Then each time a job joins the queue, we
	may preempt the current job with the new job
	if it has a shorter time to completion.
}{}

\eenum

\section{Deadlocks}

\benum
\item
What is a deadlock?  Give an example.

\ans{
A: No process can make progress.

E.g., dining philosopher.
}{}

\item
List the sufficient and necessary conditions
for a deadlock  

\ans{A: mutex, hold-and-wait, non-preemption, circularity
}{}

\item
Describe Holt's Resource graph model, and how deadlock
is represented.  What is the limitation of this model?

\ans{
A: bipartite directed graph. 
$P \to R$ means process P is waiting on resource R
$R \to P$ means resource R is held by process P

Limitation: only 1 copy of each resource.

REMARK: this a run-time graph.
}{}

\item
Describe a program involving two processes P, Q that
can cause a deadlock.  Do the run-time illustration by
Holt's graph to illustrate deadlock.

\ans{
A:

P: repeat(request R; request S; Release R; Release S;)

Q: repeat(request S; request R; Release R; Released S;)

}{}

\item
Describe the general resource-process model,
and describe the parameters in such a model.

\ans{
A:
	\\-- n processes $(p_1 \dd _n)$
	\\-- m resources $(r_1 \dd _m)$
	\\-- number of each resource $E=(e_1 \dd e_m)$
	\\-- max request matrix (nxm) $M$. 
		Let $M[i]$ be the $i$th row of $M$
	\\-- (current) Allocation matrix (nxm) $A$.
		Let $A[i]$ be the $i$th row of $A$
	\\-- free vector $F=(f_1 \dd f_m) \le E$.
		Vector $F$ can be computed from $A$ and $E$.
	\\-- "state" is just $A$ (clearly, $A \le M$ component-wise).
}{}

\item
Let $A$ be the current allocation matrix.
What does it mean for $A$ to be safe, and what is an
algorithm for testing for safely?

\ans{
A:
	Safe state: we can allocate the free resources
		in such a way that eventually
		all processes terminate.   It is assume
		that every process will eventually relinquish
		all its allocated resources if we satisfy
		its maximum requests.
	
	Here is the algorithm for testing if $A$ is safe.
	Let $F$ be the current free vector.
	We find any row $A[i]$ such that $F+A[i] \ge M[i]$.
	Then we mark the $i$th row, and add update $F$ to $F+A[i]$.

	This marking signifies that we allocate all the resources
	that process $i$ may need, and thereby guarantee that
	process $i$ will eventually return all the resources allocated
	to it.  When that happens, $F$ becomes $F+A[i]$.

	We repeat this process until every row is marked.  The
	we declare $A$ to be safe.  But if we get stuck, then
	$A$ is not safe.
}{}

\item
Describe the Banker's algorithm.

\ans{
A:   Suppose $A$ is the allocation matrix.
	Suppose process $i$ requests $W=(w_1\dd w_m)$
	(i.e., it wants $w_j$ units of resource $r_j$).
	Suppose $A'$ is the matrix where we replace $A[i]$
	by $A[i]+W$.
	The bank's algorithm says:
	grant the request iff $A'$ is safe.

	Suggestion: work out this algorithm for some examples.
}{}

\eenum

\section{Memory Management}

This review is less complete, but refer to the relevant notes.

\benum
\item
What are the two kinds of fragmentation?
How do they arise?

\ans{
A: External and Internal.
Former when we allocate contiguous blocks of space
to processes.  The latter when we use fixed
page sizes.
}{}

\eenum

\section{Unix Questions}

\benum
\item
T/F: there is one i-node per file.  Explain.

A:
	True in the sense that each physical file has exactly
	one i-node.  If we also view directories as files, then
	every i-node also corresponds to a file.

	It is not strictly true if we consider links (now,
	each hard link to the same physical file
	will have its own i-node).
	Also, since I/O devices have i-nodes, but
	strictly speaking they are not regular files.

\item
How can unix treat I/O devices as files?

A:	Just like files, we first need to open an I/O device
	for reading or writing.  This gives us a file-descriptor
	(fd).  We henceforth use this fd to read and write to the device.

\item
Describe an i-node.

\ans{}{
A: It contains user/group IDs, permissions, file type, and 15 pointers.
12 of the pointers are to data nodes (so this access 4x12 K bytes of
data.  The next three pointers are called "indirect pointers",
each pointing to a data block of pointers. They are called
"single indirect", "double indirect" and "triple indirect".

N.B. When we open a file, its i-node is kept in main memory.
In other words, there is a cache of i-nodes.
}


\item
Describe a practical technique to lessen the effects
of internal fragmentation?

\ans{
A: Use two sizes for blocks (or pages).
We have regular sizes and fragment sizes.
Each allocation is in whole number of regular sized blocks
and at most ONE fragment sized block.
This is used in FreeBSD file system.
}{}

\eenum

\myTail
