processes.tex 9.1 KB
 jkvis committed Sep 08, 2017 1 2 3 4 \documentclass{beamer} \usepackage{color} \usepackage{graphicx}  jkvis committed May 23, 2018 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 committed May 23, 2018 31 \date{}  jkvis committed Sep 08, 2017 32 33 \title{Processes and Job Control}  jkvis committed Sep 08, 2017 34 \begin{document}  jkvis committed May 23, 2018 35 \beamertemplatenavigationsymbolsempty  jkvis committed Sep 08, 2017 36   jkvis committed May 23, 2018 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 committed Sep 08, 2017 42 43  \section{What is a process?}  jkvis committed May 23, 2018 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 committed Sep 08, 2017 46 47 48 49 \begin{itemize} \item who (user) is running it; \item its id, $\ldots$; \end{itemize}  jkvis committed May 23, 2018 50 A process can \textcolor{pms280_compl}{interact}:  jkvis committed Sep 08, 2017 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 committed May 23, 2018 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 committed Sep 08, 2017 60 61 62  \bigskip  jkvis committed May 23, 2018 63 \begin{minted}{text}  jkvis committed Sep 08, 2017 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 committed May 23, 2018 69 \end{minted}  jkvis committed Sep 08, 2017 70 71 72 \end{frame} \begin{frame}[fragile]{}  jkvis committed May 23, 2018 73 To manage the output of \mintinline{bash}{ps} we can use the \mintinline{bash}{-o} option.  jkvis committed Sep 08, 2017 74 75 76  \bigskip  jkvis committed May 23, 2018 77 \mintinline{bash}{ps -o pid,ppid,tty,uid,args}  jkvis committed Sep 08, 2017 78 79 80  \bigskip  jkvis committed May 23, 2018 81 \begin{minted}{text}  jkvis committed Sep 08, 2017 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 committed May 23, 2018 87 \end{minted}  jkvis committed Sep 08, 2017 88 89 90  \bigskip  jkvis committed May 23, 2018 91 For all the output options use: \mintinline{bash}{man ps}.  jkvis committed Sep 08, 2017 92 93 94 95 \end{frame} \begin{frame}{What does it all mean?} \begin{itemize}  jkvis committed May 23, 2018 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 committed Sep 08, 2017 98 \item \texttt{TTY} --- This is a identifier of the terminal session that triggered this process. That is called the \emph{controlling terminal};  jkvis committed May 23, 2018 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 committed Sep 08, 2017 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 committed May 23, 2018 105 \begin{frame}{\mintinline{c}{fork()} and \mintinline{c}{exec()}}  jkvis committed Sep 08, 2017 106 107 108 109 Every process is created in two stages (system calls): \bigskip  jkvis committed May 23, 2018 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 committed Sep 08, 2017 112 113 114  \bigskip  jkvis committed May 23, 2018 115 \mintinline{c}{exec()} \\  jkvis committed Sep 08, 2017 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 committed May 24, 2018 119 \begin{frame}{Example: \mintinline{bash}{ls} from \mintinline{bash}{bash}}  jkvis committed Sep 08, 2017 120 \begin{enumerate}  jkvis committed May 23, 2018 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 committed Sep 08, 2017 125 126 127 128 129 \end{enumerate} \end{frame} \section{Process termination} \begin{frame}[fragile]{Exit codes}  jkvis committed May 23, 2018 130 Every process exits with an \textcolor{pms280_compl}{exit code}, that is between 0 and 255.  jkvis committed Sep 08, 2017 131 132 133 134 135 136 137  \bigskip Usually 0 means successful termination, i.e., without errors. \bigskip  jkvis committed May 23, 2018 138 \begin{minted}{bash}  jkvis committed Sep 08, 2017 139 $cd  jkvis committed May 23, 2018 140 $ echo ${?}  jkvis committed Sep 08, 2017 141 0  jkvis committed May 23, 2018 142 \end{minted}  jkvis committed Sep 08, 2017 143 144 145 146  \pause \bigskip  jkvis committed May 23, 2018 147 \begin{minted}{bash}  jkvis committed Sep 08, 2017 148 149 $ cd nop bash: cd: nop: No such file or directory  jkvis committed May 23, 2018 150 $echo${?}  jkvis committed Sep 08, 2017 151 1  jkvis committed May 23, 2018 152 \end{minted}  jkvis committed Sep 08, 2017 153 154 155 \end{frame} \begin{frame}{Killing a running process}  jkvis committed May 23, 2018 156 Sometimes it is useful to \textcolor{pms280_compl}{terminate} a running process.  jkvis committed Sep 08, 2017 157 158 159 160 161 162 163  \bigskip \texttt{Ctrl+C} in the controlling terminal (remember: not copy) \bigskip  jkvis committed May 23, 2018 164 165 \mintinline{bash}{kill -9 PID} where \texttt{PID} is a valid process id \\ \mintinline{bash}{kill -KILL PID}  jkvis committed Sep 08, 2017 166 167 168 169 \end{frame} \section{Inspecting resource usage} \begin{frame}[fragile]{See the CPU and memory usage}  jkvis committed May 23, 2018 170 171 \mintinline{bash}{ps -o \%cpu,\%mem,cmd} \\ \mintinline{bash}{ps -p PID -o \%cpu,\%mem,cmd}  jkvis committed Sep 08, 2017 172 173 174  \bigskip  jkvis committed May 23, 2018 175 \begin{minted}{text}  jkvis committed Sep 08, 2017 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 committed May 23, 2018 180 \end{minted}  jkvis committed Sep 08, 2017 181 182 183 \end{frame} \begin{frame}[fragile]{Live monitoring}  jkvis committed May 23, 2018 184 \mintinline{bash}{top} (table of processes) command  jkvis committed Sep 08, 2017 185 186 187 188 189  \bigskip { \tiny  jkvis committed May 23, 2018 190 \begin{minted}{text}  jkvis committed Sep 08, 2017 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 committed May 23, 2018 206 \end{minted}  jkvis committed Sep 08, 2017 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 committed May 24, 2018 217 \begin{frame}[fragile]{\mintinline{bash}{time} command}  jkvis committed Sep 08, 2017 218   jkvis committed May 23, 2018 219 \mintinline{bash}{time du .}  jkvis committed Sep 08, 2017 220 221 222  \bigskip  jkvis committed May 23, 2018 223 \begin{minted}{text}  jkvis committed Sep 08, 2017 224 225 226 real 0m8.373s user 0m0.256s sys 0m1.744s  jkvis committed May 23, 2018 227 \end{minted}  jkvis committed Sep 08, 2017 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 committed May 23, 2018 244 So far we have been running jobs in \textcolor{pms280_compl}{foreground}:  jkvis committed Sep 08, 2017 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 committed May 23, 2018 249 \item until the job is finished or interrupted (e.g. \mintinline{bash}{kill}).  jkvis committed Sep 08, 2017 250 251 252 253 \end{itemize} \bigskip  jkvis committed May 23, 2018 254 Running a job in the \textcolor{pms280_compl}{background}:  jkvis committed Sep 08, 2017 255 \begin{itemize}  jkvis committed May 30, 2018 256 \item use \mintinline{bash}{&} to start a job in background;  jkvis committed Sep 08, 2017 257 258 \item we remain in control of the terminal; \item the background job can \emph{write} to the terminal;  jkvis committed May 30, 2018 259 \item convenient for starting graphical programs: \mintinline{bash}{firefox &}.  jkvis committed Sep 08, 2017 260 261 262 263 264 \end{itemize} \end{frame} \begin{frame}{Moving a running job to the background}  jkvis committed Sep 14, 2017 265 We run: \\  jkvis committed May 23, 2018 266 \mintinline{bash}{$yes > /dev/null}  jkvis committed Sep 08, 2017 267 268 269 270  \bigskip Press \texttt{Ctrl+Z} \\  jkvis committed May 23, 2018 271 \mintinline{text}{[1]+ Stopped yes > /dev/null}  jkvis committed Sep 08, 2017 272 273 274  \bigskip  jkvis committed Sep 14, 2017 275 Move this job to the background: \\  jkvis committed May 23, 2018 276 \mintinline{bash}{$ bg}  jkvis committed Sep 08, 2017 277 278 279  \bigskip  jkvis committed Sep 14, 2017 280 Now this job is running in the background and we can use the terminal.  jkvis committed Sep 08, 2017 281 282 283  \bigskip  jkvis committed Sep 14, 2017 284 To return to the job: \\  jkvis committed May 23, 2018 285 \mintinline{bash}{$fg}  jkvis committed Sep 08, 2017 286 287 288 289 \end{frame} \begin{frame}[fragile]{Job list} We can get a job list using: \\  jkvis committed May 24, 2018 290 \mintinline{bash}{$ jobs}  jkvis committed Sep 08, 2017 291 292 293  \bigskip  jkvis committed May 23, 2018 294 \begin{minted}{text}  jkvis committed Sep 08, 2017 295 296 [1]- Running evince processes.pdf & [2]+ Stopped nano  jkvis committed May 23, 2018 297 \end{minted}  jkvis committed Sep 08, 2017 298 299 300  \bigskip  jkvis committed May 23, 2018 301 Alternatively, jobs can be found using \mintinline{bash}{ps} or \mintinline{bash}{top}.  jkvis committed Sep 08, 2017 302 303 304 305  \bigskip We can use the job number to select which job we want to bring to foreground: \\  jkvis committed May 23, 2018 306 \mintinline{bash}{$fg 2}  jkvis committed Sep 08, 2017 307 308 309  \bigskip  jkvis committed May 23, 2018 310 311 We can also \textcolor{pms280_compl}{terminate} a job using its job number: \\ \mintinline{bash}{$ kill -9 %2}  jkvis committed Sep 08, 2017 312 \end{frame}  jkvis committed Sep 08, 2017 313 314  \end{document}