0% found this document useful (0 votes)
10 views

Compsys Cheatsheet

The document discusses concepts related to running programs and managing memory including registers, instructions, scheduling, processes, threads, paging, and memory allocation algorithms. It also briefly covers the application layer protocols DNS and HTTP.

Uploaded by

Jack Marlow
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Compsys Cheatsheet

The document discusses concepts related to running programs and managing memory including registers, instructions, scheduling, processes, threads, paging, and memory allocation algorithms. It also briefly covers the application layer protocols DNS and HTTP.

Uploaded by

Jack Marlow
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Registers Running an instruction Scheduling

GP: used by programs for operands - use MAR to get instruction from Critical Region: region of program where shared resources are ac-
(* + - /) + pointers (can be anything) memory cessed
SP: special functions - decode instruction into operands + Race condition: processes/threads having different results based on
Memory Address Register (MAR) operators who accesses critical region first
for fetching memory addresses - execute Fixed by Mutual Exclusion: only one process at a time in critical re-
Program Counter (PC) instruction gion (lock (optional to use TLS hardware), other process busy waits
memory address of next instruction until lock == 0)
Program Status Word (PSW) Creates Priority Inversion Problem: if p1 is waiting for lock and has
execution mode bit higher priority than p2 inside critical region, will busy wait forever
kernel = 0, user = 1 Fixed by Strict Alteration: take turns running in critical region (turn
Memory Boundaries (MEM) switching after each access)
boundary of current process Yield Mutex: allow other process to run while waiting for lock
Stack Pointer (SP)
points to top of stack

Call Instruction Ret Instruction Context


- store current values at SP - restore register values from stack Switching
- store return address (entry point - load PC with return address on stack
after subroutine) on stack - decrement SP to previous stack
- load PC with entry point to subrou-
tine
- increment SP to new top of stack
Pre-emptive Scheduling:
- let process run for quantum
- if still running at end of quantum, use clock interrupts to get control
- swap out and do context switching
FIFS: run in order processes arrive
SJF: processes with shortest estimated CPU usage put to top of
queue
Round Robin: FIFS with quantums
Priority Scheduling:
- each process has priority, highest first
- decrease priorities by CPU time spent to avoid starvation
Multi-level feedback queue: multiple queues stacked with bigger
and bigger quantums (2,4,8,etc)
User mode: no privileged instruc- - fed down queues so I/O done straight away while CPU-bound grad-
tions, only OS-allowed memory ually get more and more time
Kernal Mode: all instructions, all
memory (part of OS runs in kernel Memory Management
mode for management) Logical Address = Base + Limit Registers
Interrupts: events that stop CPU - Base = start of physical memory of process (is a physical address)
from executing next instruction—CPU enters kernel mode and executes OS - Limit = max logical memory address allowed to a process
interrupt handler (specified by interrupt vector) MMU—translates logical address to physical address
System Calls: user process asking OS for privileged access - checks logical address < Limit, if so physical address = logical + base
- switches to kernel mode and starts execution at fixed address - needs contiguous memory allocation + entire process must fit in
- control may be returned to user on completion, depends who else wants physical memory at once
Memory Allocation: keep track using linked list of blocks of memory
Processes (running programs) - First-fit = fit memory into first gap that is big enough
PCB: stores pro- - Best-fit = search entire list for gap that fits best
cess environment - Worst-fit = search entire list for gap that fits worst
info (process ID, Memory Deallocation: swap out processes as needed
state, parent, etc) - now total memory of all processes can be bigger than the physical
- update/merge memory nodes if empty after dealloc
Multiprogram- External Fragmentation: small holes created in lists since can’t exact-
ming: multiple processes can share CPU by quickly switching between ly fit process memory in
- isolated environments, slow communication and switching

(2m bits total)


Threads (bits of a process)
Execution context of a process—abstracts the CPU
Same shared process heap, but individual thread stacks
TCB: thread context data (thread ID, pro- Paged Memory: break up process memory into fixed-size pages,
cess PCB, SP + PC of process, etc) physical memory into frames of same size
Multithreading: multiple parts of process - pages now fit perfectly into frames
can share CPU - process can only have needed pages of memory allocated, can now
- threads that are waiting for I/O now won’t be bigger than physical memory itself
block whole process Internal Fragmentation: can’t fully fill up pages with process
- fast communication and switching - larger page size = more frags but faster to traverse
- process runs faster if using multi-core CPU, - smaller page size = less frags but slower to traverse
can have thread per core - (CPU speed vs memory efficiency)
Memory Management (cont’d) Application Layer: Domain Name System (DNS)
Page Table: track pages + allocated frames, translate page to frame Maps domain names to IP addresses (not case sensitive, back-to-front)
- [p|d] -> [f|d] Resource Records: instructions for DNS, held in authoritative DNS servers
- present bit = if doesn’t have frame, page fault if accessed - A = IPv4, AAAA = IPv6, MX = mail server, NS = name server, CNAME
- referenced bit = if page has been accessed Name Server Zones: translates domain name to IP, domain request sent up
- modified bit = if page has been modified hierarchy (returns if cached—may go all the way to root)
Paged MMU: sends page offset d to memory, page table translates p - top-level DNS servers: .org, .com, etc
Page Fault: put page in next free frame or replace, restart process at - other authoritative: organisation DNS servers ran internally by or ISPs
fault - local DNS servers: ISPs have default name servers to handle queries + for-
Page Replacement Algorithms: ward requests (min 2 per ISP)
- FIFO = first in first out - root name servers: contains all top-level domain names, last line of defence
- Second-chance = FIFO but if page is referenced, send to tail and try
again Application Layer: Hypertext Transfer Protocol (HTTP)
- LRU = evict least recently used, approximated by aging bit counter URL: scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
(shift right every cycle, evict page with lowest—if too short is bad) - nobody uses user\password anymore in URL, not secure at all
Temporal Locality—if process uses memory address, will use again WWW: all websites/pages that can be publicly accessed
Spatial Locality—if process uses memory address, will use nearby HTTP: gets resources asked for by URLs, allows hyperlinks
addresses soon - client initiates TCP connection on port 80
Translation Lookaside Buffer—hardware cache to store recently used - server accepts, browser/client asks for objects as needed
page’s physical ad- Response codes: 1xx info, 2xx success, 3xx redirection, 4xx client error, 5xx
dresses (saves time server error
translating through HTTP 1.0: single-use
page table) connection, only loads
- valid bit for each one object (2 requests
page address, invali- needed per object—
dated if swapped or one to initiate new
context switched connection each time,
- if not in TLB (miss), OS overhead)
translate in page table
and copy over HTTP 1.1: persistent
connection (loads as
Secure Communication many object as you
Want confidentiality + authentication + integrity like)
Encryption: confidentiality - allows pipelining (don’t need to wait for response before sending more
- AES = breaks data into blocks and encrypts separately requests)
- ECB = each blocked encrypted/decrypted using same key (but HTTP 2: decrease latency, do multiplexing + compression
patterns emerge, susceptible to replay attacks) HTTP 3: decrease latency, over QUIC
- Symmetric Encryption = same key for encryption/decryption HTTPS: run over TLS, adds encryption
- Asymmetric Encryption = public key to encrypt, private key to de- Web cache: browser caches website page, checks if not expired/modified
crypt (bad for encrypting large messages, is inefficient) before loading
- Hybrid Encryption = shared secret key generated from public key Web server: multi-threaded server, thread per request
(combines fast unsecure symmetric with slow secure asymmetric) - get requested data from cache/disk, log request for security
MAC: integrity (usually a fixed-length hash function)
- uses shared secret key, generates tag = mac(m, Ks) Application Layer: Email
- send (plaintext) message and tag, verify tag by mac of message + Ks User Agents encapsulate mail, MTAs send email to server (which then uses
Digital Signatures: verification (hash message if too long, asymmetric) own MTA to send to destination)
- sign message using private signing key, verify with public verify key SMTP: slow reliable delivery service over TCP port 25, lots of back-and-forth
- send sign(m, Ksign) + message, receiver can verify using public key - only allows 7-bit ASCII, gets email from client to server using MTAs
Authenticated Encryption: encrypt then MAC MIME: converts complex attachments (e.g. images) to ASCII for SMTP
Digital Certificate: verify public key is real - extra headers: version, [content:] description, id, transfer encoding, type
- self-signed CA’s bind person to their public key Receiving emails:
- local server that is always up
Open Systems Interconnection (OSI) Model - POP3: can download from mail server (most work offloaded to user)
- HTTP: gmail, outlook, etc (retrieve + compare + manage, online)
- IMAP: can query mail server (retrieve + manage, most work on server)

Transport Layer: Sockets


Creates pipe between client and server (client = whoever started socket)
2 sockets per interfaced
Listening socket = half socket [protocol, local IP, local port no.]
Connection socket = full socket
[half socket, remote IP, remote
port no.]
Blocking wait until more data
sent, Non-blocking read data
that has arrived

- can only interact with immediate layer above/below TLS: encrypts plain text data of
sockets, extra step to set up
- provide service to layer above (email, file sharing, etc)
crypto at start
- protocols define format for talking to entities in same layer - runs on top of transport layer
TCP/IP Model: Application, Transport, Internet/Network, Link (how HTTPS works)
Transport Layer: User Datagram Protocol (UDP) Transport Layer: TCP Sliding Window
Sends datagrams over IP, connectionless Controls how much data to send/receive based on whether its processed
- datagram = packet that is purely data, no connection set up Window Size: header in TCP, how much data left in receiver’s buffer
- 3-tuple sockets, 5-tuple packets - if full, sender waits until enough space for next segment
- simple + efficient thanks to small headers - can still send ACK packets with no data, zero window probes so the receiver
- unreliable since no error + flow control + no resending bad packets re-announces the ack number and Window Size
- only ever guessing client/server are same connection
- client: dynamic port <-> server: well-known port

Multiplexing: combine streams into single for IP, split out afterwards
- port numbers specify well-known port of server it wants
- servers spawned on packet request, don’t need to be always up Window Update: receiver tells sender packets are now all read, send more
Ports: 0-65535, well-known (0-1023), registered (1024-49151), rest Persist Timer: start on sender receiving zero-size window, periodically send
are dynamic (for clients) zero window probes
- can also send urgent (priority) segments using URG flag
Transport Layer: Transmission Control Protocol (TCP) Segment Loss: repeat ACK of last in-order packet for every received segment
Reliably sends data/segments over IP, connection-oriented - after 3 duplicate packets (4 identical ACKs), sender can be sure of loss and
- data given to TCP in byte streams, converts to segments (can’t tell resend lost packet
where data ends—data reconstructed by other TCP entities using the Go-back-N: retransmit everything again from lost packet
headers) - receiver doesn’t need to store packets but can cause buffer overflow
- data sent both ways simultaneously => fully duplex Selective Repeat: only send lost packet (fast retransmit)
- receiver must store packets since packets can arrive out of order

Transport Layer: TCP Congestion Control


CWND: additional window at sender that dynamically changes size
- sender uses whichever is smallest, sender window or congestion window
Slow-Start: initially CWND = max size of a segment
- size doubled if all segments in window ACKed
- if segment lost, halve size of CWND and begin Slow-Start again

Buffering segments: reduces header overhead, more latency


- buffers data by waiting until buffer full or timer runs out
- buffer size for segments decided per entity, constrained by Max
Transfer Unit of 1500 and IP header payload of 65535

Transport Layer: TCP Three-way Handshake


Ensure only one connection is active

- if timeout of size threshold hit, increment by 1 instead


ECE: if received segment has ECE flag, act as if lost a segment and put CWR
flag in next sent segment
- means receiver buffer is about to overflow
- layer violation, flags supported by IP

Network Layer: Packet Forwarding


Gets data end-to-end, network code mostly ran over routers
Connectionless: Datagram Network (IP)
- routers know other routers around them
Sequence number: ++length of data sent, set initially by agreed-upon - use routing algorithm to get packet to destination (treats each packet indi-
sequencing strategy (e.g. random) vidually, packet switched network)
Acknowledge number: last received sequence number - each router holds full routing table that is updated dynamically
- always acknowledge received segments, even if not sending any-
thing Connection-Oriented:
Flags: Virtual Circuit
- SYN = 1 if doing handshake - each node acts as single
- ACK = 0 if entity initiating connection link of network with a
- FIN: soft close, still receive data but not sending anymore defined path to all nodes
- RST: hard close, no more data accepted - each router holds small
set routing table, says where to go next if want node x
Network Layer: Internet Protocol (IP) Network Layer: Border Gateway Protocol (BGP)
Performs routing algorithms, best effort to destination for packet Protocol for collections of networks (ASes), e.g. Google
- negotiate parameters at runtime, want scalability + flexibility - determines best network routes for entire internet, BGP routes advertised
- redundancy paths in place for packets (typically by ISPs) to neighbouring ASes and spread across internet
- 2 addresses used per network for network interface + broadcasting - operates with manual oversight, peering rules + have to consider politics,
IPv4: 32-bits in 4 parts, 0.0.0.0 -> 255.255.255.255 e.g. don’t want to go through North Korea routes, ISPs don’t want other ISP
- IP address = network.host, hierarchical, given to network interfaces traffic on their network, no commercial traffic on academic networks
- need a network mask of 1’s to figure out which is network and - Bellman’s Optimality Principle fails here due to this, can’t always do short-
which is host (e.g. 128.250.0.0/19, /19 prefix means 13 bits available est path
for host, 213 possible) - provider advertises routes for entire internet, customer pays to use
Route Aggregation: can automatically let prefixes overlap in routers’ - peers can agree to move each other’s traffic for free
internal routing tables BGP Attacks: can maliciously advertise incorrect routes, or ones redirecting to
- halves size of routing table, longest prefix selected malicious websites

IPv6: 128-bits in 8 parts, 8000::123:4567:89AB:CDEF Network Layer: Subnetting


- improved security, smaller header => faster Break up internet into smaller networks (subnets)
- backwards-compatible with IPv4, ::ffff:192.31.2.46 Use subnet mask to find which subnet to send to, bitwise AND with mask of
each subnet to see if matches afterwards (external doesn’t need to know it)
Changes can be made to subnets without external impact, routing only chang-
Network Layer: Routing es internally not externally
Delay vs Bandwidth: simple, minimise number of hops for packet
- reduces packet bandwidth + improves delay
- hopefully also reduces distance travelled (not guaranteed)
Static Routing Algorithms: calculated at boot for all possible paths,
does not adapt to network topology (bad if a router fails)
Dynamic Routing Algorithms: adapts to topology, optimises based on
a chosen weight (e.g. distance, hops, etc)
Flooding: simplest adaptive routing, inefficient but robust
- forward received packets to all other immediate neighbours
- finds shortest path but lots of conges-
tion + duplicate packets

Bellman’s Optimality Principle: if J on Network Layer: Internet Control Protocols (ICPs)


optimal path from I to K, path from J to Data Plane vs Control Plane: network protocols don’t actually really form a
K is also optimal (not true for different single stack, e.g. BGP on network uses TCP for updates
weighting nor negative weights) - think of them as different planes of protocols
Sink Tree: best paths form a tree to/ - Data Plane: packet
from chosen node processing/forwarding
Dijkstra’s: unseen, open = visited its - Control Plane:
neighbour, closed = visited choosing routes
- select node with lowest cost as new - Management Plane:
root node setting BGP policies
- update cost of node if better cost
found
- shortest path found once destination
closed
Internet Control Message Protocol (ICMP): e.g. PING messages uses ECHO
- Traceroute exploits Time Exceeded message, increments TTL (no. hops) for
each successful hop—returns IP address at each hop in message
Dynamic Host Configuration Protocol (DHCP): each host/interface needs
unique IP address, DHCP does automatically
- host broadcasts DHCP DISCOVER using UDP
- DHCP responds with DHCP OFFER with unique IP address on a lease, expires
after set amount of time
Link State Routing: find current network topology, only for dynamic MAC Address: every network interface has globally unique ID
- each router maintains map of network topology - DHCP sends new IP address over network layer to host identified by MAC
- send HELLO packet to find neighbour, must respond with ID Address Resolution Protocol (ARP): link between internet and underlying
- find weight through ECHO packet or 1/bandwidth (auto or manual) network, translates IP to MAC
- use Flooding, every other router guaranteed to receive update - broadcasts packet asking who owns target IP address (everyone can see)
- avoid waste by only accepting if sequence number (32-bit) larger - owner responds with MAC address
- age decremented every second, discarded at 0 (fixes problem of - run a lot, e.g. to figure out how to talk to nearest router
routers crashing and restarting sequence number at 0—will send - simple but security nightmare, no authentication
round to everyone otherwise, ) - ARP spoofing is gateway attack for most man-in-the-middle attacks (can just
- updates all routers on router failure say IP address is mine, but need to be on same local network)

You might also like