\documentclass[table, usenames]{beamer}
\usetheme{CambridgeUS}

\usepackage{soul}
\usepackage{xcolor}

\usepackage{amsmath}
\usepackage{amssymb}

\usepackage{datetime}
\usepackage{enumerate}
\usepackage{fontspec}

\usepackage{graphicx}
\usepackage{listings}
\usepackage{makecell}
\usepackage{multicol}
\usepackage{multirow}

\usepackage{soul}
\usepackage{tabularx}
\usepackage{textcomp}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,decorations.pathmorphing,backgrounds,positioning,fit}

\usepackage{drawstack}
\usepackage{xltxtra}

% define setlistspacing that works w/beamer (would love enumitem...)
\makeatletter
\newcommand{\setlistspacing}[2]{\def\@ld{#1}\expandafter\def\csname
@list\romannumeral\@ld \endcsname{\leftmargin\csname
leftmargin\romannumeral\@ld \endcsname
              \topsep    #2
              \parsep    0\p@   \@plus\p@
              \itemsep   #2}}
\makeatother

\defaultfontfeatures{Scale=MatchLowercase}
\setmainfont{Gentium Book Basic}
\setmonofont{JetBrainsMono Nerd Font} [
  Scale = 0.85,
  ]

\definecolor{commandlinebackground}{gray}{0.35}
\definecolor{commandlineforeground}{gray}{1.00}
\definecolor{commandpromptforeground}{gray}{0.75}

\lstset{language=c++,
  numbers=none,
  numberstyle=\tiny,
  stepnumber=1,
  numbersep=7pt,
  frame=none,
  framerule=0.8pt,
  framesep=5pt,
  captionpos=b,
  showstringspaces=false,
  showlines=true,
  literate={-}{{\texttt{-}}}1{--}{{\texttt{-{}-}}}2{---}{{\texttt{-{}-{}-}}}3,
  basicstyle=\ttfamily,
  rulecolor=\color{black}
}

% ----- Command-line "language" definition for listings -----
\lstdefinelanguage{cline}
{
}
\lstnewenvironment{commandline}[1][]
{\lstset{language=cline,numbers=none,frame=none,backgroundcolor=\color{commandlinebackground},basicstyle=\color{commandlineforeground}\ttfamily,nolol,#1}}
  {}

\title{CIS 310 Operating Systems}
\subtitle{Week 1:  Introduction to OS and \lstinline{C++}}
\author{Dr. Brian C. Ladd}

\begin{document}
\begin{frame}
  \titlepage
\end{frame}

\begin{frame}
  \frametitle{Outline}
  \tableofcontents
\end{frame}


\section{\texttt{C++} and \texttt{make}}
\subsection{Welcome}

\begin{frame}
  \frametitle{}
  \begin{itemize}
  \item
  \end{itemize}
\end{frame}

\subsection{Operating Systems from 50,000 Feet}

\begin{frame}
  \frametitle{What is an \emph{Operating System}?}
  \begin{itemize}
  \item<+-> Name two or three different \alert<1>{operating systems}.
  \item<+-> What are the \alert<2>{parts} of an operating system?
  \item<+-> What is the \alert<3>{purpose} of each of the parts?
  \item<+-> What \alert<4>{questions} come to mind about the
    \textbf{complexities} and \textbf{interactions} between the
    various parts?
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Three Easy Pieces}
  \begin{itemize}
  \item<+-> Persistence
  \item<+-> Virtualization
  \item<+-> Concurrency
  \end{itemize}
\end{frame}

\subsection{\texttt{C++}}

\begin{frame}
  \frametitle{Projects}
  \begin{itemize}
  \item<+-> Persistence : simulated disk drive; mountable file system
    using simulated disk drive.
  \item<+-> Virtualization : simple shell.
  \item<+-> Concurrency : spin lock; ticket lock.
  \item<+-> Other : array manipulation; bit-map; program launcher;
    path follower.
  \end{itemize}
\end{frame}
oooooooooooo
\begin{frame}
  \frametitle{Language}
  \begin{itemize}
  \item<+-> \lstinline|C++| Object-oriented C
    \begin{itemize}
    \item About 40 years old
    \item Model for \lstinline|Java|
    \item Native compilation.
    \end{itemize}
  \item<+-> Age shows in modules, search pathes, etc.
  \item<+-> Multiple compilers available : \lstinline|g++ 11|.
  \end{itemize}
\end{frame}

\lstset{numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt}

\begin{frame}[fragile,fragile]
  \frametitle{Hello, World!}

\begin{lstlisting}
// helloWorld.cpp
#include <iostream>
int main() {
  std::cout << "Hello, World!" << std::endl;
  return 0;
}
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Compiling and then Linking}
  \begin{itemize}
  \item<+-> Compiling generates an \emph{object file}
    (\lstinline|.o|).
  \item<+-> Linking combines multiple object files into an
    \emph{executable}.
  \item<+-> Typically, \emph{library files} (especially the C/C++
    run-time library (\lstinline|crt|)) must also be part of the link.
  \item<+-> \lstinline|C++| compilers must be explicitly told where to
    search for files to compile and to link.
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Compiling Hello, World!}

  The \lstinline|-c| compiles to \lstinline|.o| file.

\begin{commandline}
  $ g++ -c helloWorld.cpp
  $ ls
  helloWorld.cpp helloWorld.o
  $ g++ -o helloWorld helloWorld.o
  $ ls
  helloWorld helloWorld.cpp helloWorld.o
\end{commandline}

\lstinline|g++| with \lstinline|helloWorld.o| calls the linker to
produce file named with \lstinline|-o| parameter (default:
\lstinline|a.out|).
\end{frame}

\subsection{\texttt{make} Demonstration}

\begin{frame}
  \frametitle{Build Tools}
  \begin{itemize}
  \item Hard to remember all commandline parameters.
  \item Hard to remember exactly \emph{which} files changed.
  \item Much harder to remember exactly which files must be recompiled
    due to the changes.
  \item Counting on human memory (even \emph{yours}) is error prone.
  \item \textbf{Build tools} to the rescue!
  \end{itemize}

  \begin{center}
    \lstinline|make|
  \end{center}
\end{frame}

\begin{frame}[fragile]
  \frametitle{A \texttt{Makefile}}

\begin{lstlisting}[language=make]
helloWorld: helloWorld.o
    g++ -o helloWorld helloWorld.o
helloWorld.o: helloWorld.cpp
    g++ -c helloWorld.cpp
\end{lstlisting}
  \begin{itemize}
  \item Outdented lines - dependency lists: \lstinline|helloWorld|
    \emph{depends on} \lstinline|helloWorld.o|

    If \lstinline|HelloWorld| is out of date, compared to
    \lstinline|helloWorld.o|, run the rules.


  \item Indented lines - rebuilding instructions.

    There can be multiple instructions (all indented) after any given
    dependency list.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Our Complicated Make}
  \begin{itemize}
  \item Use the \lstinline|templateCPP| project as a start.
  \item In the \lstinline|src| folder (and its \lstinline|main| and
    \lstinline|util| subfolders) there are files ending with
    \lstinline|.mk|; these are included into the \lstinline|Makefile|
  \item \lstinline|src/allModules.mk| has two lists of subfolders:\\
    \lstinline|MODULES| - lists subfolders with C++ code but w/o any
    main functions.\\
    \lstinline|EXECUTABLE_MODULES| - lists subfolders containing
    \emph{only} C++ \lstinline|main| files.
  \item C++ files in modules are built to \lstinline|.o|; headers are
    made visible to all compile steps.
  \item C++ files in executable modules are built to \lstinline|.o|
    and then linked, including all object files from all the
    modules. Each file yields an executable matching its name.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Adding a C++ File to a Module}
  \begin{itemize}
  \item Put the \lstinline|.cpp| (and, most likely, \lstinline|.h|)
    file in the subfolder below \lstinline|src|.
  \item The module subfolder should already contain
    \lstinline|module.mk| and be listed in the \lstinline|MODULES|
    list.
  \item On the next \lstinline|make| (at the root of the project), the
    new file will be picked up, compiled, and linked in.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Adding an Executable}
  \begin{itemize}
  \item Put the \lstinline|.cpp| file in the subfolder.
  \item The module subfolder should already contain
    \lstinline|executable.mk| and be listed in the
    \lstinline|EXECUTABLE_MODULES| list.
  \item On the next \lstinline|make|, the new file will be compiled
    and then linked into an executable with the name of the file in
    the \lstinline|build| folder.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Adding a Module}
  \begin{itemize}
  \item To add a module, make a new folder below \lstinline|src| and
    add the name of that folder to the \lstinline|MODULES| list in
    \lstinline|src/allModules.mk|.
  \item To add an executable module, make a new folder below
    \lstinline|src| and add the name of that folder to the
    \lstinline|EXECUTABLE_MODULES| list in
    \lstinline|src/allModules.mk|.
  \end{itemize}
\end{frame}

\section{Using \texttt{git}}

\begin{frame}[fragile]
  \frametitle{Version Control}
  \begin{itemize}
  \item Version control: a \emph{chronological} database of
    \emph{snapshots} of the state of files.
  \item \lstinline|git| is a distributed version control system.
  \item Class source code is provided in a set of \lstinline|git|
    \emph{repositories} (separate databases)\\
    \lstinline|https:\\cs-devel.potdsam.edu\| is the Gitea server\\
    \lstinline|202109-CIS310-OperatingSystems| is the class organization.
  \item Our server can be accessed with the \lstinline|https| protocol
    (using the password you set when registering on it) or, after you
    upload an \lstinline|ssh| public key, the \lstinline|ssh|
    protocol.
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Cloning a repository:}
  Remember, \lstinline|\| at the end of the line escapes the <eoln>
  and continues the line.
\begin{commandline}
laddbc@cs:~/tmp$ git clone\
  https://cs-devel.potsdam.edu/\
  202109-CIS310-OperatingSystems/templateCPP.git

  Cloning into 'templateCPP'...
  Username for 'https://cs-devel.potsdam.edu': laddbc
  Password for 'https://laddbc@cs-devel.potsdam.edu':
  ...
  Resolving deltas: 100% (1/1), done.
laddbc@cs:~/tmp$ _
\end{commandline}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Cloning a repository:}
  \framesubtitle{(continued)}
\begin{commandline}
laddbc@cs:~/tmp$ cd templateCPP
laddbc@cs:~/tmp/templateCPP$ ls -AF
  .git/ .gitignore include/  lib/  Makefile  Readme.org  src/
laddbc@cs:~/tmp/templateCPP$ _
\end{commandline}

\lstinline|.git| is the database. \lstinline|.gitignore| lists
filenames \lstinline|git| should ignore. \lstinline|Makefile| is the
file that \lstinline|make| uses, \lstinline|Readme.org| explains the
repo, and \lstinline|src| has the source folders and files.
\end{frame}

\begin{frame}
  \frametitle{New Organization on Gitea}
  \begin{itemize}
  \item Create a new organization on Gitea:
    \lstinline|<username>-CIS310|
    \begin{itemize}
    \item This is for you to turn in programs.
    \item Add the instructor to a team in the organization that has
      read only rights.
    \item There will be one organization per \emph{class} you are in.
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{New Repository on Gitea}
  \begin{itemize}
  \item When starting a new assignment:
    \begin{itemize}
    \item Go to the organization you created for the course.
    \item Create a new, \textbf{private} repository using the class
      template project as a template.
    \end{itemize}
  \item Clone the new, empty but for the template, project onto your
    local machine.
  \item Solve the problem. Commit changes.
  \item Push the repository.
  \end{itemize}
\end{frame}

\section{Test Driven Devolpment in \texttt{C++}}

\begin{frame}
  \frametitle{Catch2}
  \begin{itemize}
  \item \lstinline|Catch2| is a \emph{header-only} TDD framework in
    C++.
  \item The \lstinline|stringEntry| repo demonstrates how to use it.
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{A Test}
  The test documents what \lstinline|string_util::trim_left| should
  return.
\begin{lstlisting}
SCENARIO("Test trim functions", "string_util") {
  GIVEN("A string w/o spaces") {
    string start = "NoSpacesAtAll";
    string expectedLeft = "NoSpacesAtAll";
    string expectedRight = "NoSpacesAtAll";

    WHEN("trimmed left") {
      THEN("string is unchanged") {
        REQUIRE(str_util::trim_left(start) == expectedLeft);
      }
    }
  }
}
\end{lstlisting}
\end{frame}
\begin{frame}
  \frametitle{The TDD Dev Cycle}
  \begin{itemize}
  \item Write a \textbf{failing} \emph{test}.
  \item Add the least code to make the test \textbf{pass}.
  \item Examine your code and \textbf{refactor} if necessary.\\
    - Keep all the tests passing as you go.
  \item Commit working, refactored code to \lstinline|git|.
  \end{itemize}
\end{frame}

\subsection{What is an \textbf{array}?}

\begin{frame}[fragile]
  \frametitle{An Array of \texttt{int}}
  \begin{tikzpicture}[scale=0.5]
    \cell[draw=none]{\texttt{A}}\cellcomL{Address}
    \cell{} \cellcomL{\texttt{0x1000}} \coordinate (A0) at (currentcell.east) ;
    \draw node [align=left, right=of A0] {\texttt{A[0] = A + 0 $\times$ sizeof(int)}};

    \cell{}\cellcomL{\texttt{0x1004}} \coordinate(A1) at (currentcell.east);
    \draw node [align=left, right=of A1] {\texttt{A[1] = A + 1 $\times$ sizeof(int)}};
    \cell{}\cellcomL{\texttt{0x1008}} \coordinate(A2) at (currentcell.east);
    \draw node [align=left, right=of A2] {\texttt{A[2] = A + 2 $\times$ sizeof(int)}};
    \cell{}\cellcomL{\texttt{0x100C}} \coordinate(A3) at (currentcell.east);
    \draw node [align=left, right=of A3] {\texttt{A[3] = ...}};
    \cell{}\cellcomL{\texttt{0x1010}} \coordinate(A4) at (currentcell.east);
    \draw node [align=left, right=of A4] {\texttt{A[4]}};
    \cell{}\cellcomL{\texttt{0x1014}} \coordinate(A5) at (currentcell.east);
    \draw node [align=left, right=of A5] {\texttt{A[5]}};
    \cell{}\cellcomL{\texttt{0x1018}} \coordinate(A6) at (currentcell.east);
    \draw node [align=left, right=of A6] {\texttt{A[6]}};
    \cell{}\cellcomL{\texttt{0x101C}} \coordinate(A7) at (currentcell.east);
    \draw node [align=left, right=of A7] {\texttt{A[7]}};
    \cell{}\cellcomL{\texttt{0x1020}} \coordinate(A8) at (currentcell.east);
    \draw node [align=left, right=of A8] {\texttt{A[8]}};
    \cell{}\cellcomL{\texttt{0x1024}} \coordinate(A9) at (currentcell.east);
    \draw node [align=left, right=of A9] {\texttt{A[9]}};
  \end{tikzpicture}
\end{frame}

\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
