Professional Documents
Culture Documents
Evans J. Bite Size Linux 2024
Evans J. Bite Size Linux 2024
not so complicated.
X want to learn
more, nowf J
by 5ulia Evans
hHps: //jvns.ca.
iui+ier. com/bOrk,
v "Table o? contents v
Unix
perm iss ions...H Sockets............... IO virtual memory... 17-
unix. domain
/ proc................5 Sockets............... 11 shared libraries... IB
16
Unix permissions H
There, are. 3 things (jou Is -1 File.txt" shows you permissions.
can do +o a -file Here's how to interpret-the output:
So rw - r— r executables
setg,d
V / user ^roop oJl = I l O 1 OO ioO Sis -I /bin/ping
OQp 11 o 11 o i oo
= 6 H H rw| r-x r-x root" root
sticky r" w x ru x ru >
this means ping always
For Files:
runs as root
r = can read chmod 6MH tile-txt-
W = can write setgid does 3 different
X - c-crn execute means change the
unrelated things far
For directories/A s approximately. permissions -to executables, directories,
and regular -files.
r - can list Files rw- r-- r--
W = can create Files
Simple!
\ - can cd into i access Files
OA cxmoL^in^ direc-torcj: /proc 5
/proc/Ptf)/crndline /proc/PlOfexC
Everc^ process on Linux
cotnmand l'ne Symlink +o +he process's binary
has cl PID CprocessTDl mayic.: works even if +he
like. Hl- arguments the process
binany has been de let ad f
Was started with
. % t t m ,,
/proc /PlD /st-ack oind : more 2
‘1» 1<1 z **
Directory with eveay -file The kernel’s current" stack
Look od"
the process has openf for+he process. Useful if
it’s stuck, in a system call.
Run $ Is -I /proc/HZ/fd
+0 see -Vhe list of Piles for-
nnan proc
process H2.
List of process’^ memory •for' more information 1
These sym links are also ma^cc
maps. Shared libraries,
Z you can use fhero +o recover
heap 1 anonymous maps, etc.
deleted filesV
System coA\s 6
S i anal s 7
4Xe Linux kernel sends you can send signals
Tf- uou've ever used
* /MIX processes signals in ^ourself with the kill
lo+s of situations System call or command
you've used signals /+hatpTpe\
Sl6ll\ir Ctrl-C”1 various
/yourcKIdl closed? SlbTERtl kill ( levels of
1 terminated/
----------C.’ eS^*X. sioKiLL foil -q j “die"
-------------- C!nshrucf i oqj
rthe tinker you j —----- — SIOjHOP kill -HuP
'' process CJl^f^Pj£^Z fSCamenWfanA
often interpreted as
yfauit^/->' “feloo<d confi'q", e.g. nginx
Every siynoA has a. default Vour proyra.fr can set Signals can be hard
action, which is one of: Custom handlers "for to handle correctly since
almost ancy signal they can happen at
' Snore
(-------K /okvJT'iiX . ANY tinne
El Kill process J (clean up and \ %_.
I3H) kill process AND 00^h^db^a^signJ^
make core dump file exceptions'. precess
0 slop process S16STOP g SlbklLL r XsuRPRise-!
can't be ianored (another signal! J 'y
II 3“t
|c? resume process SIOkILLed L~~\
■File descriptors
Unix systems use Isof (list openfiles'! will Vite descriptors Can
integers to track Show you a process's open files refer to :
*♦ files on disk
open f iles______ $lsof -p 42H1*~.P)P ***[*
-+ pipes
^j^open too.bet) FO NAME
Sockets (network connections'!
k 0 /dev/pts/t-hjl
process XT L"77—XTkwnel "♦terminals (like xterm^
H /okacjTWs 1 /dev/pts/tKji
2 pipe.-. iqi7q 2 devices (your speaker? Zdev/^uf)
1 -File *7 for / 3 /home / bark /awesome txt L0J» M?£f (e''en+fd< inottfu,
, teu- J signalfd,epoll{ etc.) 3J
& /imp /
+hese integers are called PVFRyTMIlKO on Unix ■">
■File descriptors tD is for file descriptor
2^ \iS ft. file, but lots ot things arc
© Create a socket-
-fd= SOCkeKAF-IMET, SOCK-STREAM
(write in big a unks) Fn^/l'll call urit(TD p-r> /no seriously, actually'
"The default for files
and pipes. ftWftan j please J
Write to that pipe
memory a.UoG<xVion lfe
your program ha.5 your memory allocator Thing? 2 : Ask the OS
memory (malloc') is responsible tor more memory?
■for 2. -things.
1 ra used
binary
BM Yl/A nfcg&MIIWWBTOKiwwgl □ -Free
ThiwC? 1: keep track, of rrr-i ooy°h no!I'm being ousked}
1 ' 3Mg> stack
what memory is used/-free. “ ? for MO HB and I don't J
ra used ma oc
587 MB heap
CO-7 MQ n r
? ° ~rree
___ /can I have"")
the heap is what* YA vhh WMf/M____ WffitUOh
] C_z j^lMB more ?/ p~r|
your allocator
manages ____ malloc Q^ere you
V/////////ZZ//7SZ1
Linux keeps a mapping -from when your program everu fime. you switch
virtual memory pages to which process is running,
accesses a virtual address
physical memory pages called
Linux needs to switch
the page table rTTl/lm accessing)
the page table
'>9C" is o^£^^«"es
bdVPKZiooo 7
cpu ~~ ~ ~- rrrn.yhere's the address
l^chunk. of memory ] bigger ।__ ooKT'll look thai up in"X
15] 7 page fable and \ Crd ^process 21S0's page fable]
MMU V^en e'-“iess fhe right)
P1D virtual addr physical addr "m€moru\P5'‘iCa’' addfe« / /Thanks,I’ll uscX rrri
1171 0x20000 0x11200 0 managemenf^-’-------- - — |+ha+ now f /*b21
unj^- '■
2310 0x2000 0 0x22 ^000 ------------- :------ S MMU
2310 0x21000 0x1788’000 hardware
Shared libraries 18
C opuonvjr iVe.
Copying all that" memory
-the cloned process
new/ processes using every time we fork
has EXACTLY the same
the. fork^ or cloned Would be slow and a.
memorcy waste of RANI
—> same hexip
/often processes calH
—* Same stack. Exec right after fork,
fl. child process that's ~* Same memory maps which means they
CL copy of the caller don't use the parent
if the parent has 368 of process's memory
memor^/ +he child will too. basically at all ¥ j
parent CKild
So Linux lets them share Linux does this by \when a process tries
physical RAM and only giving bo-Hn the processes to write to a shared
identical page tables. memory address:
copies the mernorcy
when one of the_m © there s a 5 page faultr
Q) Linux makes a copy
ram of the page (. updates
1 I'd like to change -the page Table
5 \that memory CJ tne process continues,
blissfully ignorant
J I'll make ytH?| pr but it marks every page,
own copy* J—*'~L^
as read only. process
pa.<^e f aulfs 20
Linux process has what happens during
Some pages are marked
a page -table a. page fault?
as either
-♦ the MMU sends an
* pa^e. table. # *read or\lg interrupt
vir’hjotl nfiCmor^ physical memory ★ not resident in memorcy ~* your program stops running
address address
-» Linux kernel code to handle
Ox IR72.2000 Ox 1422000 when you trig -to access the page fault runs
Ox 11724000 Ox 1422000 <xpage that’s marked
Ox 1524000 not in memory __o»{ -T'" tix -Hie problem and!
"not resident in memory,
Ox ItHMOOO 0x4a0oo read onia liiJ f let your program keep /
it triaqers a I page, fault f Linux ( runninq