processes.tex 9.1 KB
Newer Older
jkvis's avatar
jkvis committed
1 2 3 4
\documentclass{beamer}

\usepackage{color}
\usepackage{graphicx}
jkvis's avatar
jkvis committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
\usepackage{minted}
\usepackage{url}

\definecolor{pms280}{HTML}{00247d}
\definecolor{pms280_2nd}{HTML}{385499}
\definecolor{pms280_4th}{HTML}{7185b6}
\definecolor{pms280_8th}{HTML}{e2e6F0}
\definecolor{pms280_compl}{HTML}{7d5800}

\usetheme{Boadilla}
\useoutertheme{infolines}
\useinnertheme{circles}
\setbeamercolor{author in head/foot}{bg=pms280,fg=white}
\setbeamercolor{title in head/foot}{bg=pms280_2nd,fg=white}
\setbeamercolor{date in head/foot}{bg=pms280_4th,fg=white}
\setbeamercolor{block title}{bg=pms280,fg=white}
\setbeamercolor{block body}{bg=pms280_8th}
\setbeamercolor{title}{fg=pms280}
\setbeamercolor{titlelike}{fg=pms280}
\setbeamercolor{itemize item}{fg=pms280}
\setbeamercolor{itemize subitem}{fg=pms280}
\setbeamertemplate{enumerate items}[default]
\setbeamercolor{enumerate item}{fg=pms280}

\author{Jonathan~K.~Vis}
\institute[LUMC]{Dept. of Human Genetics, Leiden University Medical Center}
jkvis's avatar
jkvis committed
31
\date{}
jkvis's avatar
jkvis committed
32 33
\title{Processes and Job Control}

jkvis's avatar
jkvis committed
34
\begin{document}
jkvis's avatar
jkvis committed
35
\beamertemplatenavigationsymbolsempty
jkvis's avatar
jkvis committed
36

jkvis's avatar
jkvis committed
37 38 39 40 41
\begin{frame}
\titlepage
\vfill
\hfill \textcolor{pms280_compl}{\href{mailto:j.k.vis@lumc.nl}{j.k.vis@lumc.nl}}
\end{frame}
jkvis's avatar
jkvis committed
42 43

\section{What is a process?}
jkvis's avatar
jkvis committed
44 45
\begin{frame}{``an instance of a computer program that is being executed'' --- \url{wikipedia.org}}
Next to the program in execution a process has \textcolor{pms280_compl}{properties} like:
jkvis's avatar
jkvis committed
46 47 48 49
\begin{itemize}
\item who (user) is running it;
\item its id, $\ldots$;
\end{itemize}
jkvis's avatar
jkvis committed
50
A process can \textcolor{pms280_compl}{interact}:
jkvis's avatar
jkvis committed
51 52 53 54 55 56 57
\begin{itemize}
\item processes can ``talk'' to each other;
\item processes can request resources from the OS.
\end{itemize}
\end{frame}

\section{Inspecting running processes}
jkvis's avatar
jkvis committed
58 59
\begin{frame}[fragile]{Using the \mintinline{bash}{ps} (process status) command}
Lists the \textcolor{pms280_compl}{running processes} for the current user/terminal:
jkvis's avatar
jkvis committed
60 61 62

\bigskip

jkvis's avatar
jkvis committed
63
\begin{minted}{text}
jkvis's avatar
jkvis committed
64 65 66 67 68
  PID TTY          TIME CMD
 6348 pts/24   00:00:06 evince
 7089 pts/24   00:00:00 ps
23323 pts/24   00:00:00 bash
28359 pts/24   00:04:40 evince
jkvis's avatar
jkvis committed
69
\end{minted}
jkvis's avatar
jkvis committed
70 71 72
\end{frame}

\begin{frame}[fragile]{}
jkvis's avatar
jkvis committed
73
To manage the output of \mintinline{bash}{ps} we can use the \mintinline{bash}{-o} option.
jkvis's avatar
jkvis committed
74 75 76

\bigskip

jkvis's avatar
jkvis committed
77
\mintinline{bash}{ps -o pid,ppid,tty,uid,args}
jkvis's avatar
jkvis committed
78 79 80

\bigskip

jkvis's avatar
jkvis committed
81
\begin{minted}{text}
jkvis's avatar
jkvis committed
82 83 84 85 86
  PID  PPID TTY        UID COMMAND
 6348 23323 pts/24   89604 evince processes.pdf
 7237 23323 pts/24   89604 ps -o pid,ppid,tty,uid,args
23323  2727 pts/24   89604 bash
28359 23323 pts/24   89604 evince text.pdf
jkvis's avatar
jkvis committed
87
\end{minted}
jkvis's avatar
jkvis committed
88 89 90

\bigskip

jkvis's avatar
jkvis committed
91
For all the output options use: \mintinline{bash}{man ps}.
jkvis's avatar
jkvis committed
92 93 94 95
\end{frame}

\begin{frame}{What does it all mean?}
\begin{itemize}
jkvis's avatar
jkvis committed
96 97
\item \texttt{PID} --- Every process has an \textcolor{pms280_compl}{id} associated to it. It is an unique identifier, and that is how we can reference a specific process;
\item \texttt{PPID} --- The parent's \texttt{PID}. Every (almost) process has a \textcolor{pms280_compl}{parent process}, the process that was responsible for its creation;
jkvis's avatar
jkvis committed
98
\item \texttt{TTY} --- This is a identifier of the terminal session that triggered this process. That is called the \emph{controlling terminal};
jkvis's avatar
jkvis committed
99
\item \texttt{UID} --- This is the user id. It is the identifier for the \textcolor{pms280_compl}{user} that’s the owner of this process, and that’s what will define the permissions this process will have.
jkvis's avatar
jkvis committed
100 101 102 103 104
\item \texttt{ARGS} --- The command (followed by its arguments) that is running in this process.
\end{itemize}
\end{frame}

\section{How processes are born}
jkvis's avatar
jkvis committed
105
\begin{frame}{\mintinline{c}{fork()} and \mintinline{c}{exec()}}
jkvis's avatar
jkvis committed
106 107 108 109
Every process is created in two stages (system calls):

\bigskip

jkvis's avatar
jkvis committed
110 111
\mintinline{c}{fork()} \\
Create a copy of the calling process. The newly created process is called the child, and the caller is the parent. This child process inherits everything that the parent has in memory, it is an almost exact copy (\texttt{PID} and \texttt{PPID} are different, for instance).
jkvis's avatar
jkvis committed
112 113 114

\bigskip

jkvis's avatar
jkvis committed
115
\mintinline{c}{exec()} \\
jkvis's avatar
jkvis committed
116 117 118
Replace the current process with a new one. The caller process is gone forever, and the new process takes its place.
\end{frame}

jkvis's avatar
jkvis committed
119
\begin{frame}{Example: \mintinline{bash}{ls} from \mintinline{bash}{bash}}
jkvis's avatar
jkvis committed
120
\begin{enumerate}
jkvis's avatar
jkvis committed
121 122 123 124
\item \mintinline{bash}{ls}
\item \mintinline{bash}{bash} creates an exact copy of itself using \mintinline{c}{fork()}
\item \mintinline{c}{exec()} to replace this copy with the \mintinline{bash}{ls} process
\item when the \mintinline{bash}{ls} is finished you are back to the parent process, that is \mintinline{bash}{bash}.
jkvis's avatar
jkvis committed
125 126 127 128 129
\end{enumerate}
\end{frame}

\section{Process termination}
\begin{frame}[fragile]{Exit codes}
jkvis's avatar
jkvis committed
130
Every process exits with an \textcolor{pms280_compl}{exit code}, that is between 0 and 255.
jkvis's avatar
jkvis committed
131 132 133 134 135 136 137

\bigskip

Usually 0 means successful termination, i.e., without errors.

\bigskip

jkvis's avatar
jkvis committed
138
\begin{minted}{bash}
jkvis's avatar
jkvis committed
139
$ cd
jkvis's avatar
jkvis committed
140
$ echo ${?}
jkvis's avatar
jkvis committed
141
0
jkvis's avatar
jkvis committed
142
\end{minted}
jkvis's avatar
jkvis committed
143 144 145 146

\pause
\bigskip

jkvis's avatar
jkvis committed
147
\begin{minted}{bash}
jkvis's avatar
jkvis committed
148 149
$ cd nop
bash: cd: nop: No such file or directory
jkvis's avatar
jkvis committed
150
$ echo ${?}
jkvis's avatar
jkvis committed
151
1
jkvis's avatar
jkvis committed
152
\end{minted}
jkvis's avatar
jkvis committed
153 154 155
\end{frame}

\begin{frame}{Killing a running process}
jkvis's avatar
jkvis committed
156
Sometimes it is useful to \textcolor{pms280_compl}{terminate} a running process.
jkvis's avatar
jkvis committed
157 158 159 160 161 162 163

\bigskip

\texttt{Ctrl+C} in the controlling terminal (remember: not copy)

\bigskip

jkvis's avatar
jkvis committed
164 165
\mintinline{bash}{kill -9 PID} where \texttt{PID} is a valid process id \\
\mintinline{bash}{kill -KILL PID}
jkvis's avatar
jkvis committed
166 167 168 169
\end{frame}

\section{Inspecting resource usage}
\begin{frame}[fragile]{See the CPU and memory usage}
jkvis's avatar
jkvis committed
170 171
\mintinline{bash}{ps -o \%cpu,\%mem,cmd} \\
\mintinline{bash}{ps -p PID -o \%cpu,\%mem,cmd}
jkvis's avatar
jkvis committed
172 173 174

\bigskip

jkvis's avatar
jkvis committed
175
\begin{minted}{text}
jkvis's avatar
jkvis committed
176 177 178 179
%CPU %MEM CMD
 0.5  1.1 evince processes.pdf
 0.0  0.0 ps -o %cpu,%mem,cmd
 0.0  0.0 bash
jkvis's avatar
jkvis committed
180
\end{minted}
jkvis's avatar
jkvis committed
181 182 183
\end{frame}

\begin{frame}[fragile]{Live monitoring}
jkvis's avatar
jkvis committed
184
\mintinline{bash}{top} (table of processes) command
jkvis's avatar
jkvis committed
185 186 187 188 189

\bigskip

{
\tiny
jkvis's avatar
jkvis committed
190
\begin{minted}{text}
jkvis's avatar
jkvis committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
Tasks: 297 total,   1 running, 296 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0,8 us,  0,4 sy,  0,1 ni, 97,0 id,  1,7 wa,  0,0 hi,  0,0 si,  0,0 st
KiB Mem:   8082284 total,  7136656 used,   945628 free,   467828 buffers
KiB Swap:  8293372 total,   148028 used,  8145344 free.  2568880 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 2308 jkvis     20   0 2160736 408668  58188 S   2,0  5,1  72:12.73 gnome-shell
 2727 jkvis     20   0  740416  45448  20436 S   2,0  0,6   8:57.47 gnome-terminal
 1142 root      20   0  490452 151152 108720 S   1,3  1,9  74:20.12 Xorg
 8992 jkvis     20   0 4669916 1,622g 159472 S   1,3 21,0   1412:26 firefox
16250 jkvis     20   0 2381724 1,040g 1,002g S   0,7 13,5 752:53.78 VBoxHeadless
 1751 redis     20   0   37016   5200   1060 S   0,3  0,1   8:48.82 redis-server
 2145 jkvis     20   0  391608  48904   4552 S   0,3  0,6   7:19.45 ibus-daemon
24816 jkvis     20   0 1034684  51128  26860 S   0,3  0,6  86:11.84 VirtualBox
...
jkvis's avatar
jkvis committed
206
\end{minted}
jkvis's avatar
jkvis committed
207 208 209 210 211 212 213 214 215 216
}

\bigskip

Use \texttt{<} and \texttt{>} to navigate \\
Use \texttt{u} and a username to show processes for that user \\
\texttt{q} quits
\end{frame}

\section{Timing a process}
jkvis's avatar
jkvis committed
217
\begin{frame}[fragile]{\mintinline{bash}{time} command}
jkvis's avatar
jkvis committed
218

jkvis's avatar
jkvis committed
219
\mintinline{bash}{time du .}
jkvis's avatar
jkvis committed
220 221 222

\bigskip

jkvis's avatar
jkvis committed
223
\begin{minted}{text}
jkvis's avatar
jkvis committed
224 225 226
real    0m8.373s
user    0m0.256s
sys     0m1.744s
jkvis's avatar
jkvis committed
227
\end{minted}
jkvis's avatar
jkvis committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

\bigskip

\begin{itemize}
\item \texttt{real} --- wall clock time;
\item \texttt{user} --- only actual CPU time used in executing the process;
\item \texttt{sys} --- CPU time spent in system calls within the \emph{kernel}.
\end{itemize}
\end{frame}

\section{Job control}
\begin{frame}
A process in UNIX --- that is something doing a particular job --- can run either in \emph{foreground} or \emph{background}.

\bigskip

jkvis's avatar
jkvis committed
244
So far we have been running jobs in \textcolor{pms280_compl}{foreground}:
jkvis's avatar
jkvis committed
245 246 247 248
\begin{itemize}
\item jobs start per default in foreground;
\item the job has control over the terminal;
\item we cannot enter new commands while the job is running;
jkvis's avatar
jkvis committed
249
\item until the job is finished or interrupted (e.g. \mintinline{bash}{kill}).
jkvis's avatar
jkvis committed
250 251 252 253
\end{itemize}

\bigskip

jkvis's avatar
jkvis committed
254
Running a job in the \textcolor{pms280_compl}{background}:
jkvis's avatar
jkvis committed
255
\begin{itemize}
jkvis's avatar
jkvis committed
256
\item use \mintinline{bash}{&} to start a job in background;
jkvis's avatar
jkvis committed
257 258
\item we remain in control of the terminal;
\item the background job can \emph{write} to the terminal;
jkvis's avatar
jkvis committed
259
\item convenient for starting graphical programs: \mintinline{bash}{firefox &}.
jkvis's avatar
jkvis committed
260 261 262 263 264
\end{itemize}
\end{frame}

\begin{frame}{Moving a running job to the background}

jkvis's avatar
jkvis committed
265
We run: \\
jkvis's avatar
jkvis committed
266
\mintinline{bash}{$ yes > /dev/null}
jkvis's avatar
jkvis committed
267 268 269 270

\bigskip

Press \texttt{Ctrl+Z} \\
jkvis's avatar
jkvis committed
271
\mintinline{text}{[1]+  Stopped                 yes > /dev/null}
jkvis's avatar
jkvis committed
272 273 274

\bigskip

jkvis's avatar
jkvis committed
275
Move this job to the background: \\
jkvis's avatar
jkvis committed
276
\mintinline{bash}{$ bg}
jkvis's avatar
jkvis committed
277 278 279

\bigskip

jkvis's avatar
jkvis committed
280
Now this job is running in the background and we can use the terminal.
jkvis's avatar
jkvis committed
281 282 283

\bigskip

jkvis's avatar
jkvis committed
284
To return to the job: \\
jkvis's avatar
jkvis committed
285
\mintinline{bash}{$ fg}
jkvis's avatar
jkvis committed
286 287 288 289
\end{frame}

\begin{frame}[fragile]{Job list}
We can get a job list using: \\
jkvis's avatar
jkvis committed
290
\mintinline{bash}{$ jobs}
jkvis's avatar
jkvis committed
291 292 293

\bigskip

jkvis's avatar
jkvis committed
294
\begin{minted}{text}
jkvis's avatar
jkvis committed
295 296
[1]-  Running                 evince processes.pdf &
[2]+  Stopped                 nano
jkvis's avatar
jkvis committed
297
\end{minted}
jkvis's avatar
jkvis committed
298 299 300

\bigskip

jkvis's avatar
jkvis committed
301
Alternatively, jobs can be found using \mintinline{bash}{ps} or \mintinline{bash}{top}.
jkvis's avatar
jkvis committed
302 303 304 305

\bigskip

We can use the job number to select which job we want to bring to foreground: \\
jkvis's avatar
jkvis committed
306
\mintinline{bash}{$ fg 2}
jkvis's avatar
jkvis committed
307 308 309

\bigskip

jkvis's avatar
jkvis committed
310 311
We can also \textcolor{pms280_compl}{terminate} a job using its job number: \\
\mintinline{bash}{$ kill -9 %2}
jkvis's avatar
jkvis committed
312
\end{frame}
jkvis's avatar
jkvis committed
313 314

\end{document}