-
Notifications
You must be signed in to change notification settings - Fork 23
/
Cheat Sheet.tex
461 lines (417 loc) · 21.5 KB
/
Cheat Sheet.tex
1
2
3
4
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
% Created by Jacob Strieb
% Spring 2019
%\documentclass{article}
\documentclass[twocolumn]{article}
% Formatting
\usepackage[letterpaper, margin=0.5in]{geometry}
\usepackage{microtype}
\usepackage{titlesec}
\pagenumbering{gobble}
% Sections with no numbers and minimal vertical spacing
\titleformat{\section}{\normalfont\Large\bfseries}{}{0pt}{}[\vspace{-8pt}]
% Misc. Imports
\usepackage{enumerate}
\usepackage{enumitem}
\usepackage{multicol}
% Document title and metadata
\usepackage[pdftex,
pdfauthor={Jacob Strieb},
pdftitle={Unix Systems Programming Cheat Sheet}]{hyperref}
% Custom commands
\renewcommand{\tt}[1]{\texttt{#1}}
\begin{document}
\section{Integers}
\begin{tabular}{| c || c | c | c ||| c | c |}
\hline
$n$ & $2^n$ & Hex & Binary & $n$ & $2^n$\\
\hline \hline
0 & 1 & \tt{0x00} & 00000 & 17 & 131072 \\
\hline
1 & 2 & \tt{0x01} & 00001 & 18 & 262144 \\
\hline
2 & 4 & \tt{0x02} & 00010 & 19 & 524288 \\
\hline
3 & 8 & \tt{0x03} & 00011 & 20 & 1048576 \\
\hline
4 & 16 & \tt{0x04} & 00100 & 21 & 2097152 \\
\hline
5 & 32 & \tt{0x05} & 00101 & 22 & 4194304 \\
\hline
6 & 64 & \tt{0x06} & 00110 & 23 & 8388608 \\
\hline
7 & 128 & \tt{0x07} & 00111 & 24 & 16777216 \\
\hline
8 & 256 & \tt{0x08} & 01000 & 25 & 33554432 \\
\hline
9 & 512 & \tt{0x09} & 01001 & 26 & 67108864 \\
\hline
10 & 1024 & \tt{0x0A} & 01010 & 27 & 134217728 \\
\hline
11 & 2048 & \tt{0x0B} & 01011 & 28 & 268435456 \\
\hline
12 & 4096 & \tt{0x0C} & 01100 & 29 & 536870912 \\
\hline
13 & 8192 & \tt{0x0D} & 01101 & 30 & 1073741824 \\
\hline
14 & 16384 & \tt{0x0E} & 01110 & 31 & 2147483648 \\
\hline
15 & 32768 & \tt{0x0F} & 01111 & 32 & 4294967296 \\
\hline
16 & 65536 & \tt{0x10} & 10000 & 33 & 8589934592 \\
\hline
\end{tabular}
\begin{itemize}[noitemsep]
\item \textbf{Word size} -- nominal size of pointer data
\item Addresses go up to $2^w - 1$ for $w$-bit word size
\item \textbf{Little Endian} -- least significant byte comes first
\item \textbf{Big Endian} -- most significant byte comes first
\item Example of storing \tt{0x1234567} at \tt{0x100} \\
\begin{tabular}{| r || c | c | c | c |}
\hline
& \tt{0x100} & \tt{0x101} & \tt{0x102} & \tt{0x103} \\ \hline
Big endian & \tt{01} & \tt{23} & \tt{45} & \tt{67} \\ \hline
Little endian & \tt{67} & \tt{45} & \tt{23} & \tt{01} \\ \hline
\end{tabular} \\
\item Arithmetic between signed and unsigned values automatically casts all signed values to unsigned
\item $-TMin_w = TMin_w$
\item $\lceil x/2^k \rceil$ is given by \tt{(x + (1 << k) - 1) >> k}
\item $x/2^k$ is given by \tt{(x<0 ? x+(1<<k)-1 : x) >> k}
\end{itemize}
\begin{tabular}{| c || c | c | c |}
\hline
& \multicolumn{3}{c|}{Word size $w$} \\ \cline{2-4}
Value & 8 & 16 & 32 \\ \hline
$UMax_w$ & \tt{0xFF} & \tt{0xFFFF} & \tt{0xFFFFFFFF} \\
& 255 & 65,535 & 4,294,967,295 \\ \hline
$TMin_w$ & \tt{0x80} & \tt{0x8000} & \tt{0x80000000} \\
& $-128$ & $-32,768$ & $-2,147,483,648$ \\ \hline
$TMax_w$ & \tt{0x7F} & \tt{0x7FFF} & \tt{0x7FFFFFFF} \\
& $127$ & $32,767$ & $2,147,483,647$ \\ \hline
$-1$ & \tt{0xFF} & \tt{0xFFFF} & \tt{0xFFFFFFFF} \\ \hline
\end{tabular}
\section{Bitwise Operations}
\begin{itemize}[noitemsep]
\item \textbf{Logical shift} -- Fills left end with zeros
\item \textbf{Arithmetic shift} -- Sign-extends left end
\end{itemize}
\begin{multicols}{4}
\begin{tabular}{r | c}
\tt{\~} & \\ \hline
0 & 1 \\
1 & 0
\end{tabular}
\begin{tabular}{r | c c}
\tt{\&} & 0 & 1 \\ \hline
0 & 0 & 0 \\
1 & 0 & 1
\end{tabular}
\begin{tabular}{r | c c}
\tt{|} & 0 & 1 \\ \hline
0 & 0 & 1 \\
1 & 1 & 1
\end{tabular}
\begin{tabular}{r | c c}
\tt{\^} & 0 & 1 \\ \hline
0 & 0 & 1 \\
1 & 1 & 0
\end{tabular}
\end{multicols}
\vfill
\section{Floating Point}
\begin{itemize}[noitemsep]
\item Floating point lacks associativity
\item $V = (-1)^s \times M \times 2^E$
\item Sign bit $s$ -- whether the number is positive or negative, represented by 1-bit field
\item Exponent $E$ weights the value by a possibly negative power of 2, represented by $k$-bit \tt{exp} field
\item Significand (mantissa) $M$ -- fractional binary number between 1 and $2 - \varepsilon$ or between 0 and $1 - \varepsilon$, represented by $n$-bit \tt{frac} field ($f_{n - 1}\cdots f_1 f_0$)
\item Normalized values
\begin{itemize}[noitemsep]
\item Most common case
\item \tt{exp} is neither all zeros nor all ones
\item Exponent field represents biased signed integer
\item $E = e - $Bias where $e$ is the unsigned number in \tt{exp} and Bias $= 2^{k - 1} - 1$
\item \tt{frac} represents $0 \leq f < 1$ with $0.f_{n - 1}\cdots f_1 f_0$ and $M = 1 + f$ -- implied leading 1
\end{itemize}
\item Denormalized values
\begin{itemize}[noitemsep]
\item Exponent field all zeros
\item Exponent value is $E = 1 - $Bias, significand value is $M = f$ (no leading 1)
\item Numbers close to zero (inclusive), evenly spaced near 0.0
\end{itemize}
\item Special values
\begin{itemize}[noitemsep]
\item Exponent field is all ones
\item Fraction field all zeros can represent $\pm \infty$, depending on sign bit
\item Nonzero fraction field is NaN
\end{itemize}
\item Rounding
\begin{itemize}[noitemsep]
\item Rounds to the nearest even
\item \tt{BBGRXXXX}
\item \tt{G} -- Guard bit; least significant bit of result
\item \tt{R} -- Round bit; first bit removed
\item \tt{XXXX} -- Sticky bit; OR of remaining bits
\item Round up conditions:
\begin{itemize}[noitemsep]
\item Round = 1, Sticky = 1 $\to\ > 0.5$
\item Guard = 1, Round = 1, Sticky = 0 $\to$ Round to even
\end{itemize}
\end{itemize}
\item Multiplication
\begin{itemize}[noitemsep]
\item $(-1)^{s_1}M_12^{E_1} \times (-1)^{s_2}M_22^{E_2}$ \\
\null \qquad \qquad \qquad \qquad $= (-1)^{s_1 \string^ s_2}(M_1 \times M_2)2^{E_1 + E_2}$
\item If $M_1 \times M_2 = M \geq 2$ shift $M$ right, increment $E = E_1 + E_2$
\item If $E$ out of range, overflow
\item Round $M$ to fit \tt{frac} precision
\end{itemize}
\end{itemize}
% TODO: Add table here from page 116
\vfill
\pagebreak
\section{x86-64 Data Alignment}
\begin{itemize}[noitemsep]
\item Internal padding -- added between struct elements
\item External padding -- added after struct elements
\item The entire struct is externally padded to align to its largest element
\end{itemize}
\begin{tabular}{| c || c |} \hline
$K$ & Types \\ \hline
1 & \tt{char} \\ \hline
2 & \tt{short} \\ \hline
4 & \tt{int, float} \\ \hline
8 & \tt{long, double, char *} \\ \hline
\end{tabular}
\vfill
%\pagebreak
\section{Caches}
\begin{itemize}[noitemsep]
\item $M = 2^m$ unique addresses of $m$ bits
\item $S = 2^s$ cache sets
\item Each set consists of $E$ cache lines
\item Each line consists of a data block of $B = 2^b$ bytes, a valid bit and $t = m - (b + s)$ tag bits
\item Capacity of a cache is $C = S \times E \times B$
\item Address \\
\begin{tabular}{| c | c | c |}
$t$ bits & $s$ bits & $b$ bits \\ \hline
$\leftarrow m - 1$ && $0 \rightarrow$ \\ \hline
Tag & Set index & Block offset
\end{tabular} \\
\item Direct-mapped cache has one line per set ($E = 1$)
\item Non-direct caches sometimes referred to as $E$-way set associative cache
\item Fully-associative cache has one set ($E = C/B$).
\end{itemize}
\section{Conditional Control}
\begin{itemize}[noitemsep]
\item Carry flag (\tt{CF}) -- most recent op generated carry of most significant bit, detects overflow for unsigned
\item Zero flag (\tt{ZF}) -- most recent op yielded zero
\item Sign flag (\tt{SF}) -- most recent op yielded negative value
\item Overflow flag (\tt{OF}) -- most recent op caused two's complement overflow
\item \tt{test} instruction behaves like \tt{and} instructions but sets condition codes without altering source or destination often see \tt{testq \%rax,\%rax} to check if return val is neg, zero, or pos
\end{itemize}
\tt{set D} and \tt{jmp} suffixes \\
\begin{tabular}{| c || c | c | c |}
\hline
\textbf{Instruction} & \textbf{Syn.} & \textbf{Cond.} & \textbf{Desc.} \\ \hline
\tt{-e} & \tt{-z} & \verb|ZF| & $=/0$ \\ \hline
\tt{-ne} & \tt{-nz} & \verb|~ZF| & $!=$/not zero \\ \hline
\tt{-s} & & \verb|SF| & Neg \\ \hline
\tt{-ns} & & \verb|~SF| & Nonneg \\ \hline
\tt{-g} & \tt{-nle} & \verb|~(SF^OF)&~ZF| & signed $>$ \\ \hline
\tt{-ge} & \tt{-nl} & \verb|~(SF^OF)| & signed $=>$ \\ \hline
\tt{-l} & \tt{-nge} & \verb|SF^OF| & signed $<$ \\ \hline
\tt{-le} & \tt{-ng} & \verb=(SF^OF)|ZF= & signed $<=$ \\ \hline
\tt{-a} & \tt{-nbe} & \verb|~CF&~ZF| & unsigned $>$ \\ \hline
\tt{-ae} & \tt{-nb} & \verb|~CF| & unsigned $>=$ \\ \hline
\tt{-b} & \tt{-nae} & \verb|CF| & unsigned $<$ \\ \hline
\tt{-be} & \tt{-na} & \verb=CF|ZF= & unsigned $<=$ \\ \hline
\end{tabular}
\vfill
\pagebreak
\section{Assembly Basics}
\begin{itemize}[noitemsep]
\item ``word'' refers to 16-bit data type, with ``double word'' referring to 32-bit (int) and 64-bit quantities referred to as ``quad words''
\item On 64-bit machines pointers are 8-byte quad words
\item 16 general purpose registers storing 64-bit values (register file)
\item In operands, scaling factor $s$ must be either 1, 2, 4, or 8
\item \tt{mov S, D} has the effect of $S \to D$
\item \tt{movzbq} moves from byte to quad with zero-extended whereas \tt{movsbq} does the same but sign-extended
\item Stack grows down if increasing addresses grow up -- ``top'' of the stack at the bottom
\item \tt{leaq S, D} has the effect of $\&S \to D$
\end{itemize}
\begin{tabular}{| c || c | c |}
\hline
\textbf{Type} & \textbf{Form} & \textbf{Operand value} \\ \hline
Immediate & $\$Imm$ & $Imm$ \\ \hline
Register & $r_a$ & $R[r_a]$ \\ \hline
Memory & $Imm(r_b, r_i, s)$ & M[$Imm + R[r_b] + R[r_i] \cdot s$] \\ \hline
\end{tabular} \\ \ \\
\begin{tabular}{| c || c | c | c | c |}
\hline
\textbf{Type} & \textbf{64-bits} & \textbf{32-bits} & \textbf{16-bits} & \textbf{8-bits} \\ \hline
Return val & \tt{\%rax} & \tt{\%eax} & \tt{\%ax} & \tt{\%al} \\ \hline
Callee & \tt{\%rbx} & \tt{\%ebx} & \tt{\%bx} & \tt{\%bl} \\ \hline
1st arg & \tt{\%rdi} & \tt{\%edi} & \tt{\%di} & \tt{\%dil} \\ \hline
2nd arg & \tt{\%rsi} & \tt{\%esi} & \tt{\%si} & \tt{\%sil} \\ \hline
3rd arg & \tt{\%rdx} & \tt{\%edx} & \tt{\%dx} & \tt{\%dl} \\ \hline
4th arg & \tt{\%rcx} & \tt{\%ecx} & \tt{\%cx} & \tt{\%cl} \\ \hline
5th arg & \tt{\%r8} & \tt{\%r8d} & \tt{\%r8w} & \tt{\%r8b} \\ \hline
6th arg & \tt{\%r9} & \tt{\%r9d} & \tt{\%r9w} & \tt{\%r9b} \\ \hline
Callee & \tt{\%rbp} & \tt{\%ebp} & \tt{\%bp} & \tt{\%bpl} \\ \hline
Stack ptr & \tt{\%rsp} & \tt{\%esp} & \tt{\%sp} & \tt{\%spl} \\ \hline
Caller & \tt{\%r10} & \tt{\%r10d} & \tt{\%r10w} & \tt{\%r10b} \\ \hline
Caller & \tt{\%r11} & \tt{\%r11d} & \tt{\%r11w} & \tt{\%r11b} \\ \hline
Callee & \tt{\%r12} & \tt{\%r12d} & \tt{\%r12w} & \tt{\%r12b} \\ \hline
Callee & \tt{\%r13} & \tt{\%r13d} & \tt{\%r13w} & \tt{\%r13b} \\ \hline
Callee & \tt{\%r14} & \tt{\%r14d} & \tt{\%r14w} & \tt{\%r14b} \\ \hline
Callee & \tt{\%r15} & \tt{\%r15d} & \tt{\%r15w} & \tt{\%r15b} \\ \hline
\end{tabular}
\section{Linking}
\begin{itemize}[noitemsep]
\item Relocatable object files -- combine with other relocatables at compile time to create executables, made by compiler and assembler
\item Executable object files -- contain binary data that can be directly copied to memory and executed, made by linker
\item Shared object files -- special relocatable objects that can be linked dynamically at load or run time
\item Static symbols -- defined locally to an object file (module)
\item Global symbols -- defined locally and referred to elsewhere
\item Externals -- Global symbols referenced locally but defined elsewhere
\item Local linker symbols are different from local program variables
\item Functions and initialized global variables are exported to the assembler as strong by the compiler
\item Uninitialized global variables are weak
\item Linkers error on multiple same-name strong symbols, pick strong over weak, and randomly choose from weak symbols
\end{itemize}
\section{Processes}
\begin{itemize}[noitemsep]
\item \tt{getpid(void)} -- \tt{pid\_t} of the process
\item \tt{getppid(void)} -- \tt{pid\_t} of the parent process
\item Processes are running, stopped, or terminated
\item \tt{exit(int status)} -- called once, never returns
\item \tt{fork(void)} -- \tt{pid\_t} of child in parent or 0 if in child; called once, returns twice
\item Open file table and vnode table are managed by OS and shared across processes, file descriptor table is process-specific
\item \tt{waitpid(pid\_t pid, int *statusp, int options)} -- returns \tt{pid\_t} of child, 0 if not waiting (\tt{WNOHANG}) or error
\begin{itemize}[noitemsep]
\item \tt{WNOHANG} -- return (0) immediately and don't wait for child
\item \tt{WUNTRACED} -- check for terminated and stop children
\item \tt{WCONTINUED} -- check for waiting (child) process to be continued
\item OR (\tt{|}) flags together to form a bit vector
\item \tt{WIFEXITED(status)} -- whether child exited normally
\item \tt{WEXITSTATUS(status)} -- returns exit status of terminated child if \tt{WIFEXITED} is true
\item \tt{WIFSIGNALED \& WTERMSIG} -- as above but for signals
\item \tt{WIFSTOPPED \& WSTOPSIG \& WIFCONTINUED} -- as above for stopped/continued processes
\end{itemize}
\item \tt{wait(int *statusp)} $\cong$ \tt{waitpid(-1, \&status, 0)}
\item \tt{sleep(unsigned int secs)} -- returns short counts
\item \tt{pause(void)} -- sleeps until a signal is received
\item \tt{execve(char *filename, char *argv[], char *envp[])} -- does not return unless error; last arg in each array is \tt{NULL}
\end{itemize}
\section{Virtual Memory}
\begin{itemize}[noitemsep]
\item $N = 2^n$ addresses in $n$-bit virtual address space
\item $M = 2^m$ addresses in $m$-bit physical address space (not necessarily power of 2)
\item $P = 2^p$ bytes per virtual/physical page
\item Virtual pages are either unallocated, cached (allocated in PM), or uncached (allocated, not in PM)
\item DRAM caches are often fully associative
\item Page table maps virtual pages to physical pages
\item Valid bit in PTE set indicates cached in PM, rest indicates virtual or physical address (depending on valid bit)
\item Page fault is DRAM cache miss; triggers exception
\item Translation lookaside buffer (TLB) is a cache of PTEs; each line holds a block with one PTE; highly associative
\item TLB has $T = 2^t$ sets
\item Virtual address \\
\begin{tabular}{| c | c | c |}
\multicolumn{2}{|c|}{Virtual Page Number} & \\ \hline
$\leftarrow n - 1$ & $\leftarrow p + t \ |\ p \rightarrow$ & $0 \rightarrow$ \\ \hline
TLB Tag & TLB index & Virtual page offset
\end{tabular}
\end{itemize}
\section{Dynamic Memory Allocation}
\begin{itemize}[noitemsep]
\item \tt{sbrk(intptr\_t incr)} -- extend the heap by \tt{incr} (basically just an \tt{int}) and return the old break pointer
\end{itemize}
\vfill
\section{Signals}
\begin{itemize}[noitemsep]
\item Default action of \tt{SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP} is to ignore and stop (respectively per pair)
\item Signals can be sent to process groups; child inherits parent process group by default; \tt{setpgid(0, 0)} sets the process group ID to the current process id
\item \tt{kill(pid\_t pid, int sig)} -- send a \tt{sig} to \tt{pid}, unless \tt{pid} is 0 then send it to every process in the process group of the calling process; if \tt{pid} $< 0$, send to every process in process group $|$\tt{pid}$|$
\item \tt{signal(int sig, void *hndlr\_t (int) handler)} -- handle \tt{sig} with \tt{handler} function pointer; \tt{handler} can be \tt{SIG\_IGN} to ignore or \tt{SIG\_DFL} for default
\item \tt{sigprocmask(int how, sigset\_t *set, sigset\_t *oldset)}
\begin{itemize}[noitemsep]
\item \tt{SIG\_BLOCK} -- \tt{blocked = blocked | set}
\item \tt{SIG\_UNBLOCK} -- \tt{blocked = blocked \& ~set}
\item \tt{SIG\_SETMASK} -- \tt{blocked = set}
\item Old bit vector stored in \tt{oldset} (a.k.a. \tt{prev})
\end{itemize}
\item \tt{sigemptyset(sigset\_t *set)}
\item \tt{sigfillset(sigset\_t *set)}
\item \tt{sigaddset(sigset\_t *set, int sig)} -- add \tt{sig}
\item \tt{sigdelset(sigset\_t *set, int sig)} -- delete \tt{sig}
\item Rules for signal handlers
\begin{enumerate}
\item Keep them simple
\item Only call async-signal-safe functions (reentrant or uninterruptible)
\item Save and restore \tt{errno}
\item Block all signals
\item Declare global variables with \tt{volatile} -- force memory read each time (no storage in registers)
\item Declare flags with \tt{sig\_atomic\_t} -- atomic r/w
\end{enumerate}
\item Signals are \textbf{not} queued
\item \tt{sigsuspend(sigset\_t *mask)} -- atomically replace \tt{blocked} with \tt{mask} and suspend until handler returns after receipt of a signal, then restore \tt{blocked}
\end{itemize}
\section{Concurrency with Threads}
\begin{itemize}[noitemsep]
\item \tt{pthread\_create(pthread\_t *tid, NULL, void *func(void *), void *arg)} -- run \tt{func} with \tt{arg} in a new thread, joinable by default
\item \tt{pthread\_self(void)} -- return current thread id
\item \tt{pthread\_exit(void *return)} -- exit the current thread
\item \tt{pthread\_cancel(pthread\_t tid)} -- terminate another thread without waiting
\item \tt{pthread\_join(pthread\_t tid, NULL)} -- block and wait for the thread with \tt{tid} to terminate
\item \tt{pthread\_detach(pthread\_t tid)} -- make thread \tt{tid} detached (not joinable), often called on self
\item Threads share everything in memory except for registers and stack, though they can access addresses in other thread stacks
\end{itemize}
\vfill
\section{System I/O}
\begin{itemize}[noitemsep]
\item \tt{open(char *filename, int flags, mode\_t mode)} -- returns file descriptor
\begin{itemize}[noitemsep]
\item \tt{O\_RDONLY}, \tt{O\_WRDONLY}, \tt{O\_RDWR} flags
\item \tt{O\_CREAT} -- create a new file if it doesn't exist
\item \tt{O\_TRUNC} -- truncate the file if it exists
\item \tt{O\_APPEND} -- before write, set file pos. to end of file
\item Mode given by OR ($|$) combination of \\ \tt{S\_I\{R, W, X\}\{USR, GRP, OTH\}}
\item Each call creates new open file table entry
\end{itemize}
\item \tt{read(int fd, void *buf, size\_t n)} -- read up to \tt{n} bytes into \tt{buf} and return the number of bytes actually read, update file descriptor table position by return value
\item \tt{write(int fd, void *buf, size\_t n)} -- write up to \tt{n} bytes from \tt{buf}, return the number of bytes actually written, update file descriptor table position by return value
\item Each process has unique descriptor table pointing to entries in global file table
\item OS maintains open file table shared by all processes, each entry has file position, ref count, and pointer to v-node table
\item OS maintains v-node table with information about each file
\item Parent and child process must both close file descriptors for kernel to remove file table entry
\item \tt{dup2(int oldfd, int newfd)} -- copies descriptor entry \tt{oldfd} to \tt{newfd}, overwriting \tt{newfd} (closes \tt{newfd} if open); \textit{i.e.}, \tt{newfd} entry points to \tt{oldfd} entry
\end{itemize}
\section{Thread Synchronization}
\begin{itemize}[noitemsep]
\item \tt{sem\_init(sem\_t *sem, 0, int val)} -- initialize lock
\item \tt{sem\_wait(sem\_t *sem)} -- \tt{P(sem)}; block until get lock
\item \tt{sem\_post(sem\_t *sem)} -- \tt{V(sem)}; release lock
\item In producer-consumer solution, producer produces whenever there is space in buffer, and consumer consumes whatever is there as fast as it can
\item First readers-writers problem favors readers
\item Second readers-writers problem favors writers
\item Readers-writers solutions can result in starvation
\item Deadlock -- threads are waiting for condition that will never be true
\end{itemize}
\section{Network Programming}
\begin{itemize}[noitemsep]
\item \tt{socket(int domain, int type, int protocol)} -- usually \tt{AF\_INET, SOCK\_STREAM, 0}, respectively; create endpoint of connection
\item \tt{connect(int clientfd, struct sockadd *addr, socklen\_t addrlen)} -- establish connection with a server at \tt{addr} (client)
\item \tt{bind(int sockfd, struct sockaddr *adrr, socklen\_t addrlen)} -- associate server socket address with given socket descriptor (server)
\item \tt{listen(int sockfd, int backlog)} -- set \tt{sockfd} to actively listen (server)
\item \tt{accept(int listenfd, struct sockaddr *addr, int *addrlen)} -- block until a connection is made then return file descriptor for connection
\end{itemize}
\vfill
Scratch work:
\vfill
\begin{center}
Created for 15-213 at Carnegie Mellon University \\
in Spring 2019 by Jacob Strieb. \\
\href{mailto:[email protected]}{\tt{[email protected]}} \\
\url{https://git.io/JcZ29}
\end{center}
\end{document}