\documentclass[12pt]{article}
\usepackage{fancyheadings,fullpage}

\lhead{COMP3000}
\rhead{Lab 2: \textit{exec} and \textit{fork}}
\pagestyle{fancy}

\renewcommand{\headheight}{.2in}
\renewcommand{\headsep}{.1in}
\setlength{\topmargin}{0pt}
%\setlength{\voffset}{-.6in}
%\setlength{\textheight}{9.5in}

\parindent=0in
\parskip=0pt

\begin{document}

For this lab, you are not required to submit your answers to the
questions.  The questions, however, may be asked on subsequent quzzes
and you are required to remember roughly what you did in completing
this lab.

\section{Installing Debian Linux}

In order to get linux running on the lab computers, we are going to
need to install it on a computer.  Installing is actually a relatively
simple task, but is completely documented below.

\begin{enumerate}

\item Boot the computer with the install CD in the drive.  It should
boot from the CD and ask you to press F1 for help or enter to boot.
We want to run with a 2.6 kernel, so type ``linux26'' and then press
enter.

\item Choose an install language of english.

\item We live in Canada.  Choose Canada as a country.

\item Choose an American English keymap.  The keymap is used to
determine how to interpret keys pressed on the keyboard into actual
letters.

\item The install will fail to detect the network card in the
computers.  This is ok, as we won't be using the network card for any
of the labs.  When it asks you if you wish to load network drivers off
a floppy, choose no.  It will give you a few errors and then you will
be at a menu from which you can continue the install.

\item Choose the \textit{Detect Hardware} option from the install menu.

\item The computers in the lab do not have a PCMCIA interface.  Choose
no when asked whether PC card services should be started.

\item Debian will start the partitioner in order to partition the hard
drive.  You can safely erase the entire disk.

\item When asked how to partition the disks, choose \textit{Desktop Machine}

\item The install will display the partition setup it has
automatically calculated for install.  Since we do not have ext3
available as a filesystem type, you need to manually select each
partition that has ext3 as a filesystem type and change the filesystem
type to ext2.  While you are in the menu for each partition, make sure
that all partitions are set to be formatted as part of the install.

\item When you are finished changing the filesystem types for all
partitions that have ext3 as a filesystem type, choose \textit{Finish
partitioning and write changes to disk}

\item The installer will set up the partitions, format the
filesystems, and install the base system at this point.

\item After installing the base system, you will be asked about GRUB.
GRUB is the bootloader designed to boot the debian installation from
the hard disk. There are no othe operating systems on the disk, so it
is safe to say that GRUB can be installed on the master boot record.
Recall from class that the MBR contains a small program designed to
boot the operating system during the boot process.

\item The installer will eject the CD.  Remove it and acknowledge the
attempt by the installer to reboot the system.

\item The system will reboot and you will be presented with a series
of configuration screens.  After the system has booted off of the hard
drive, put the CD back into the drive.  You will need it later on in
the install process.

\item The clock on these systems is not set to GMT.

\item We are in the eastern time zone.

\item For these systems, the root password should be set to \texttt{comp3000}

\item An additional user called \texttt{Student} should be created with a
password of \texttt{student}

\item The system name can be left at debian

\item You are not connected to the internet.  Do not use PPP to
install the system.

\item There is not a second Debian CD.  Choose no when asked if you
wish to scan another CD.

\item Debian will inform you it can't access security updates.
Usually, these security updates contain patches to software.  Since
these computers are not on the internet, it is not critical that the
security updates be installed.  If you were installing Debian outside
the lab, however, this would be a different story.

\item For the collections of software currently installed, leave the
selection with none of them selected and choose ok.

\item Exim, the mail server, should be configured for local delivery
only.

\item All mail should be delivered to \texttt{student}.

\item At this point, the base install is finished.  You will be
presented with a login prompt.  Log in as \texttt{root} with a password of
\texttt{comp3000}.

\item The base install did not install all of the elements we are
going to need for our system.  at the prompt type \texttt{apt-get install
kde-devel}.  This will install the software development programs from
the KDE project.  For the most part, you can accept the default
options, but note that we use letter size paper in Canada and not a4.

\item Install the manpages for development.  \texttt{apt-get install
manpages-dev}.  These contain the documentation on \textit{fork} and
\textit{exec} that you will need in order to complete the lab.

\item Install the X server (\texttt{apt-get install x-window-system}).
This is used as a graphical interface under Linux.  Allow it to
autodetect the video hardware.  The defaults on all options can be
used.  When it comes to asking what video modes you wish to use,
select 1280x960 and all lower resolution modes.

\item If you wish to install vim or emacs21, use the same process of
\texttt{apt-get install ...} to install these editors.  This is an optional
step, and if you don't know what these editors are, then don't worry.

\item We want to play with two utility programs, strace and ltrace, so
  install them with \texttt{apt-get install strace ltrace}.

\item Install KDM (\texttt{apt-get install kdm}), the K display manager.
This will present you with a gui where you can enter your username and
password.  You should use the KDM and not XDM when presented with the
option.

\item Copy over the files required for lab 2.  To do this, you must
first mount the CD (using \texttt{mount /cdrom}).  The lab 2 files are
located on the CD in a tar file.  Untar the file into the
\texttt{/usr/src} directory by using the \texttt{cd} command to change
to the \texttt{/usr/src} directory and then typing \texttt{tar -xvf
/cdrom/lab2.tar} to extract the files.  Finish off by unmounting the
CD (using \texttt{umount /cdrom}).

\item Start KDM.  To do this, type \texttt{/etc/init.d/kdm start} at the
prompt.  This will result a graphical login prompt being displayed on
the screen.

\item We still need to log out of the console.  Switch back to the
console by using \textit{CTRL-ALT-F1}, then log out by typing exit at the
prompt.  To get back to the graphical login screen, type \textit{CTRL-ALT-F7}

\end{enumerate}

\section{Fork}

In this section of the lab, you will be introduced to the fork system call.  

\begin{enumerate}

\item Log in as \texttt{student} (password \texttt{student})

\item Pull up a terminal window.  This can be done by clicking on the
icon depicting a monitor with \textit{$>$\_} in it.  From the terminal
window, use the manpages to gain an understanding of how the fork
system call works.  Do this by using the command \texttt{man fork}.
Later on you will use \texttt{man} to learn about other Linux commands.

\item Copy over fork.c from \texttt{/usr/src/lab2/fork.c} into your
home directory.

\item Compile fork.c using gcc (\texttt{gcc fork.c}).  It will produce
an executable called a.out.  To run a.out, prepend a \textit{./}
telling the shell that you wish to run the file located in the current
directory.  What does running \textit{a.out} display?

\item Make a Zombie process using fork.c.  To see if you have
successfully created a zombie process, use the \texttt{ps a} command.
You will have to examine the process listing after the child has
finished its processing, but before the parent completes.  Use
appropriate programming techniques to make this gap in finishing times
large enough that another terminal window can be used to run the
\texttt{ps a} command.  What is displayed after \textit{[a.out]} on
the process listing?  What is listed under the STAT column?

\item Modify the program so that the parent prints its text first.  To
do this, you will probably need to use the \texttt{man} command to
learn the meaning of \textit{wait}.  You'll also need some way to
force a context switch---try using \textit{sleep} (you'll have to run
\texttt{man 3 sleep} to see the right manual page).

\item While there are a number of ways of making a process give up its
CPU time, an alternate method of ensuring correct execution flow is
through the use of interprocess communication.  Pipes (\texttt{man
pipe}) are one method of communicating between a parent and child
process.  Using pipes as well as \texttt{read} and \texttt{write},
re-implement the previous step.  The parent should wait until a byte
has been written to the pipe before printing to the terminal, and the
child should wait until after it prints before sending a byte on the
pipe.

\item What are the values of x, y, and z when the parent and child
  processes exit?  Add \texttt{printf} statements to see if you are
  right.

\item Run \texttt{strace -fo fork-syscalls.log ./a.out}.  Look at
  \textit{fork-syscalls.log} using \textit{more}.  Are there more
  system calls than you would expect?

\item Recompile \texttt{fork.c} using the command \texttt{gcc -static
  fork.c -o fork-static}.  Run \textit{fork-static} using
  \textit{strace}.  How does the output compare with the previous
  strace output?

\item Look at the manual page for \textit{ltrace}.  Run it on both
  compiled versions of \texttt{fork.c}.  On one of the versions the
  command doesn't work---why?  Hint: check out the \texttt{ldd}
  command.

\end{enumerate}

\section{Exec}

In the previous section, we concentrated on the fork system call.  Now
we will focus on exec.

\begin{enumerate}

\item Copy over the exec.c file from \texttt{/usr/src/lab2/exec.c}

\item Build the sample exec.c file using gcc and run it.  The program
requires the name of an alternate executable to run with the exec
system call.  For our purposes, use echo.  Try running \texttt{./a.out
echo Hello}.  What is printed on the screen?

\item Examine the exec.c file in a text editor.  Is the line ``Is this
line printed?'' printed on the screen when the program is run?  Why or
why not?

\item As an example of how we can use exec, modify argv[2] so that
every character in the string is incremented by one.  Recompile the
exec.c file and run the result with \texttt{./a.out echo Hello}.  Now
run \texttt{echo Hello}.  Verify that the input to the echo command
has indeed been changed.

\item Modify the exec.c file so that the program specified by
\textit{argv[1]} is run on both the incremented and non-incremented
forms of \textit{argv[2]} (hint, in order to do this, you are going to
have to \textit{exec argv[1]} twice, requiring a fork).  Display the
non-incremented output first.  How would you modify your program to
display the incremented output first?

\item Optional task: modify the program to use just \texttt{execve}.
  Pay special attention to the somewhat unusual argument declaration!

\end{enumerate}

\end{document}
