Module 7 · 14 marks · Models, media & the link layer
Framing, error control & flow control
The data link layer turns a raw bit pipe into a reliable hop-to-hop link: it frames bits, detects or corrects errors (parity, checksum, CRC, Hamming), and paces and repairs transmission with stop-and-wait, Go-Back-N and Selective Repeat. HDLC and PPP combine it all.
High-yield 75 minSyllabus: “Functions and protocols of the data-link layer”
Mark as
The idea in one minute
Imagine dictating a long message over a crackly phone. You split it into numbered sentences (framing), add a quick check such as 'that was 42 letters' (error detection), wait for 'got it' before moving on or keep talking while the listener confirms (flow control), and repeat whatever was missed (retransmission). If the line is very bad, you might spell every word with extra redundant letters so the listener can fix small mistakes without asking (error correction).
Key points — the night-before list
Framing: character count, byte stuffing (insert ESC before FLAG/ESC in data), bit stuffing (flag 01111110; insert a 0 after every five consecutive 1s).
Parity detects odd numbers of bit errors; 2-D parity detects all 1-, 2- and 3-bit errors; Internet checksum = one's-complement sum of 16-bit words, complemented.
CRC: append r zeros (r = generator degree), divide modulo 2, send the r-bit remainder. Detects all single-bit errors and all bursts of length ≤ r.
Hamming: 2ʳ ≥ m + r + 1; parity bits at 1, 2, 4, 8…; syndrome = error position. Distance d detects d − 1, corrects ⌊(d − 1)/2⌋ errors.
Stop-and-wait efficiency = 1/(1 + 2a), a = Tp/Tt. A sliding window of W frames gives W/(1 + 2a), up to 100% when W ≥ 1 + 2a.
Go-Back-N: sender window ≤ 2ᵐ − 1, receiver window 1, resend everything after a loss. Selective Repeat: both windows ≤ 2ᵐ⁻¹, resend only the lost frame.
HDLC (bit-oriented, ISO): I, S, U frames; NRM, ARM, ABM modes. PPP (byte-oriented): LCP sets up the link, PAP/CHAP authenticate, NCP (IPCP) configures IP; no flow control.
01What the data link layer does
The physical layer only promises to move bits, and some of them arrive flipped. The data link layer turns that raw, error-prone pipe into a usable link between two directly connected nodes — hop-to-hop (node-to-node) delivery. Its functions are framing, physical (MAC) addressing, error control, flow control and, on shared media, access control.
Unacknowledged connectionless service — frames are sent without acknowledgements; lost frames are left to higher layers. Suits low-error links such as Ethernet.
Acknowledged connectionless service — each frame is acknowledged individually but no connection is set up. Suits unreliable links such as 802.11 wireless.
Acknowledged connection-oriented service — a connection is established, frames are numbered and delivered exactly once and in order. Suits long, noisy links (HDLC-style links, satellite).
IEEE splits the layer into two sublayers. The upper Logical Link Control (LLC, IEEE 802.2) provides the common interface to the network layer — identifying the upper protocol through DSAP/SSAP fields and optionally doing flow and error control (LLC type 1 is unacknowledged connectionless, type 2 connection-oriented, type 3 acknowledged connectionless). The lower Medium Access Control (MAC) sublayer is specific to each LAN technology: it owns framing, MAC addresses and the rules for sharing the medium (CSMA/CD, token passing, CSMA/CA).
Note
Flow control means don't overwhelm the receiver — the sender must wait for permission. Error control means detect damaged or lost frames and get them resent — usually ARQ (Automatic Repeat reQuest): acknowledgements, timers and sequence numbers.
02Framing: marking where frames begin and end
A receiver sees a continuous stream of bits; framing tells it where each frame starts and stops. Four methods are classic.
Character (byte) count: a header field says how many characters follow. Simple, but if the count itself is corrupted the receiver loses synchronization and every following frame is misread — so it is rarely used alone.
Flag bytes with byte stuffing (character-oriented): each frame begins and ends with a special FLAG byte. If a FLAG pattern occurs inside the data, the sender inserts an ESC (escape) byte before it — and also before any ESC byte in the data. The receiver deletes each ESC and keeps the byte after it. PPP uses FLAG = 0x7E and ESC = 0x7D (the escaped byte is also XORed with 0x20).
Flag bits with bit stuffing (bit-oriented): the flag is 01111110. Whenever the sender sees five consecutive 1s in the data it inserts a 0, so six 1s in a row can only ever be a flag. The receiver removes the 0 that follows any five 1s. HDLC uses this.
Physical-layer coding violations: use signal patterns that never occur in valid data as delimiters — for example, 4B/5B's spare code groups or the Manchester violations in token ring's start and end delimiters.
Bit stuffing by hand
Scan
Read the data left to right, counting consecutive 1s.
Stuff
After the fifth consecutive 1, write an extra 0 and reset the count — even if the next data bit is already 0.
Frame
Put the flag 01111110 before and after the stuffed data.
Destuff
At the receiver, delete the 0 that follows any run of five 1s; a 1 in that position means a flag (six 1s) or an abort.
PSC favourite
01111110 in the data becomes 011111010 on the line. Fixed-size frames (ATM's 53-byte cells) need no delimiters at all — their length marks the boundary.
03Error detection: errors, parity and the Internet checksum
A single-bit error changes one bit of a data unit; a burst error changes two or more bits, and its length is counted from the first corrupted bit to the last, including correct bits in between. Bursts are far more common: a 1 ms noise spike corrupts 1 bit at 1 kbps but about 1,000 bits at 1 Mbps.
All detection schemes add redundancy: a k-bit dataword becomes an n-bit codeword with n − k extra bits, and the receiver checks whether the received word is still a valid codeword. The Hamming distance between two words is the number of positions in which they differ (d(10101, 11110) = 3); a code's minimum distance d_min decides its power.
Detect up to s errors ⇔ d_min ≥ s + 1
Correct up to t errors ⇔ d_min ≥ 2t + 1
So a code with d_min = d detects d − 1 errors and corrects ⌊(d − 1)/2⌋ errors.
Single parity bit (d_min = 2): add one bit to make the number of 1s even (even parity) or odd. It detects any odd number of flipped bits but misses every even number.
Two-dimensional parity (d_min = 4): arrange the data in rows, add a parity bit to every row and every column. It detects all 1-, 2- and 3-bit errors and can even locate (correct) a single-bit error, but some 4-bit patterns at the corners of a rectangle slip through.
Internet checksum (IP, TCP, UDP headers): treat the data as 16-bit words, add them with one's-complement arithmetic (any carry out of the top bit is wrapped round and added back), then complement the sum. The receiver adds all words including the checksum; the result must be all 1s (its complement 0).
The checksum is weak: swapping two 16-bit words, or errors that cancel in the sum, go unnoticed. That is why links use the much stronger CRC, while the checksum survives in software-friendly IP/TCP/UDP headers.
04Cyclic redundancy check (CRC)
A CRC treats a bit string as a polynomial with coefficients 0 or 1: 100101 is x⁵ + x² + 1. Sender and receiver agree on a generator polynomial G(x) of degree r (r + 1 bits, first and last bits 1). Arithmetic is modulo 2: addition and subtraction are both XOR, with no carries or borrows.
Computing and checking a CRC
Append
Append r zeros to the dataword (multiply it by xʳ).
Divide
Divide by the generator using modulo-2 long division: whenever the leading bit of the current remainder is 1, XOR the generator under it; when it is 0, XOR zeros (quotient bit 0).
Remainder
The last r bits left are the CRC (the frame check sequence).
Transmit
Send dataword followed by the r-bit remainder. This codeword is exactly divisible by G(x).
Check
The receiver divides the whole received codeword by G(x). Remainder 0 → accept; anything else → an error is detected and the frame is discarded.
All single-bit errors are detected if G(x) has at least two terms (its x⁰ coefficient is 1).
All odd numbers of errors are detected if G(x) has (x + 1) as a factor.
All burst errors of length ≤ r are detected; a burst of length r + 1 slips through with probability (½)^(r − 1), and a longer one with probability (½)^r.
Double-bit errors are caught if G(x) does not divide xᵗ + 1 for any t up to the frame length.
In hardware a CRC is just a shift register with XOR gates at the positions of G(x)'s terms, so it runs at full line speed. Remember that a CRC only detects errors; recovery is left to retransmission.
Note
Work any CRC division step by step in the CRC & Hamming code tool on this page — type a dataword and a generator and watch each XOR line of the long division.
05Error correction: the Hamming code
Retransmission is impossible or too slow on some links (deep-space probes, memory chips, one-way broadcast). Forward error correction (FEC) adds enough redundancy for the receiver to fix errors itself. Richard Hamming's code (1950) corrects any single-bit error.
Number of redundant bits r for m data bits: 2ʳ ≥ m + r + 1
m = 4 → r = 3 (7-bit code, Hamming(7,4)) m = 7 → r = 4 (11 bits)
m = 8 → r = 4 (12 bits) m = 11 → r = 4 (15 bits) m = 16 → r = 5 (21 bits)
Number the bit positions 1, 2, 3, … (KPSC answer keys count from the right, so position 1 is the rightmost bit).
Parity bits sit at the powers of two: positions 1, 2, 4, 8, …; data bits fill the other positions, most significant bit at the highest position.
Parity bit p covers every position whose binary number has that bit set: P1 checks 1, 3, 5, 7, 9, 11…; P2 checks 2, 3, 6, 7, 10, 11…; P4 checks 4–7, 12–15…; P8 checks 8–15….
Each parity bit is chosen to make its group even (for even parity).
Decoding: recompute each check over its group including the parity bit. Writing the results as C8 C4 C2 C1 gives the syndrome; 0 means no error, otherwise it is the position of the flipped bit — invert it.
Parity bit
Positions checked
Binary pattern
P1
1, 3, 5, 7
positions with bit 0 set (xx1)
P2
2, 3, 6, 7
positions with bit 1 set (x1x)
P4
4, 5, 6, 7
positions with bit 2 set (1xx)
Coverage in Hamming(7,4)
Plain Hamming codes have d_min = 3: they correct one error or detect two, not both at once. Adding one overall parity bit gives d_min = 4 — the SECDED code (single-error correction, double-error detection) used in ECC memory.
PSC favourite
Two fast checks for options: the codeword must have m + r bits, and the parity bits must sit at positions 1, 2, 4, 8. Then verify one or two parity groups rather than recomputing everything.
06Flow and error control: stop-and-wait ARQ
In stop-and-wait ARQ the sender transmits one frame, keeps a copy, starts a timer and waits. A correct frame earns an ACK; a damaged frame is silently discarded, so the timer expires and the sender retransmits. Frames are numbered 0 and 1 alternately (1-bit sequence numbers): without numbers, a lost ACK would make the sender resend a frame the receiver already has, and the receiver would accept a duplicate. The ACK carries the number of the next frame expected.
Stop-and-wait wastes the link when it is long or fast, because the sender sits idle for a whole round trip after every frame. Let Tt be the frame transmission time and Tp the one-way propagation time; the ratio a = Tp/Tt measures how many frames' worth of bits fit along the link.
Cycle per frame = Tt + 2Tp (ignoring ACK size and processing)
Efficiency U = Tt / (Tt + 2Tp) = 1 / (1 + 2a)
Throughput = U × bandwidth
With frame-error probability p: U = (1 − p) / (1 + 2a)
When a < 1 (short LAN links) stop-and-wait is acceptable; when a ≫ 1 (satellite or long fibre at high speed) it is hopeless. Doubling the bandwidth halves Tt and so doubles a — faster links make stop-and-wait worse, not better.
PSC favourite
If every k-th transmission is lost, stop-and-wait needs N + ⌊(N − 1)/(k − 1)⌋ transmissions to deliver N frames: for N = 10 and every 4th frame lost, 10 + 3 = 13.
07Sliding windows: Go-Back-N and Selective Repeat
To keep the pipe full, the sender may have up to W unacknowledged frames outstanding. With m-bit sequence numbers the numbers run 0 … 2ᵐ − 1 and wrap around. The sender window covers frames sent but not yet acknowledged (plus those that may be sent); it slides forward as ACKs arrive. The receiver window covers the frame numbers it is willing to accept; it shrinks as frames arrive and grows (slides) as it delivers them and sends ACKs.
Efficiency with window W: U = W / (1 + 2a) if W < 1 + 2a, otherwise U = 1
Window needed for 100% utilisation: W ≥ 1 + 2a
Go-Back-N: Ws ≤ 2ᵐ − 1, Wr = 1
Selective Repeat: Ws = Wr ≤ 2ᵐ⁻¹
Go-Back-N ARQ: the receiver accepts frames only in order (window 1) and discards everything after a missing frame. ACKs are cumulative. On timeout the sender resends the lost frame and every frame after it. Simple receiver, but wasteful on noisy links.
Why Ws ≤ 2ᵐ − 1 in Go-Back-N: with m = 2, suppose the sender sends frames 0, 1, 2, 3 (window 4) and all ACKs are lost. The receiver now expects frame 0 of the next cycle; the timed-out sender resends the old frame 0, which is wrongly accepted. With a window of 3 the receiver expects 3, so the old 0 is rejected.
Selective Repeat ARQ: the receiver buffers out-of-order frames, ACKs each frame individually (and may send a NAK), and the sender resends only the lost frame. Better on noisy links, but it needs buffers and sorting at the receiver.
Why Ws ≤ 2ᵐ⁻¹ in Selective Repeat: if the windows were larger than half the sequence space, after a lost batch of ACKs the receiver's new window would overlap the old numbers and accept a retransmitted duplicate as new data.
Piggybacking: in two-way traffic, an ACK rides in the header of the next outgoing data frame instead of travelling as a separate frame — fewer frames, better link utilisation. A timer ensures a separate ACK is sent if no data frame is ready.
Feature
Stop-and-wait
Go-Back-N
Selective Repeat
Sender window
1
≤ 2ᵐ − 1
≤ 2ᵐ⁻¹
Receiver window
1
1
≤ 2ᵐ⁻¹ (same as sender)
Sequence bits needed
1
m
m
Out-of-order frames
—
Discarded
Buffered
Retransmission after a loss
That frame
Lost frame and all after it
Only the lost frame
ACK style
Individual
Cumulative
Individual (plus NAK)
Efficiency on noisy links
Lowest
Medium
Highest
Tip
Stop-and-wait is simply a sliding-window protocol with W = 1 and m = 1. Animate the window sliding in the TCP handshake & congestion tool, which uses the same idea at the transport layer.
08HDLC and PPP
HDLC (High-level Data Link Control) is ISO's bit-oriented protocol for point-to-point and multipoint links, derived from IBM's SDLC. It defines primary stations (send commands), secondary stations (send responses) and combined stations (both), arranged in unbalanced (one primary, one or more secondaries) or balanced (two combined stations) configurations.
NRM (Normal Response Mode): unbalanced; a secondary may transmit only when the primary polls it.
ARM (Asynchronous Response Mode): unbalanced; a secondary may start transmitting without being polled, though the primary still controls the line (rarely used).
ABM (Asynchronous Balanced Mode): balanced; either combined station may transmit at any time — the most common mode (LAPB in X.25 and LAPD in ISDN are based on it).
Field
Size
Purpose
Flag
8 bits
01111110 at each end; bit stuffing keeps it unique
Address
8 bits (extendable)
The secondary station's address
Control
8 bits (16 in extended mode)
Frame type, N(S), N(R) and the P/F (poll/final) bit
Information
Variable
User data (I-frames) or management data (some U-frames)
FCS
16 or 32 bits
CRC-CCITT or CRC-32
HDLC frame: Flag | Address | Control | Information | FCS | Flag
Type
Control starts with
Carries
Examples
I-frame (information)
0
User data plus N(S) (own number) and piggybacked N(R) (next expected)
SNRM, SABM (set modes), DISC, UA (unnumbered ack), FRMR, UI
Three frame types, told apart by the first control bits
PPP (Point-to-Point Protocol) is the byte-oriented protocol of dial-up, DSL (PPPoE) and many WAN links. Its frame is Flag 0x7E | Address 0xFF | Control 0x03 | Protocol (2 bytes) | Payload (default maximum 1,500 bytes) | FCS (2 or 4 bytes) | Flag, with byte stuffing using ESC 0x7D.
LCP (Link Control Protocol) establishes, configures (maximum frame size, authentication method), tests and terminates the link.
Authentication:PAP — a two-way exchange that sends the password in plain text; CHAP — a three-way challenge-response using a hash, repeated periodically, so the password never crosses the line.
NCP (Network Control Protocols) configure each network-layer protocol; IPCP negotiates IP addresses, so PPP provides network address configuration.
Phases: Dead → Establish (LCP) → Authenticate → Network (NCP) → Open (data) → Terminate → Dead.
Not provided: flow control, error correction (an errored frame is just discarded), sequence numbers and acknowledgements in the default mode, and multipoint addressing.
PSC favourite
HDLC = bit-oriented (bit stuffing); PPP = byte-oriented (byte stuffing). 'Which service is not provided by PPP?' → flow control.
Worked examples
Try the problem first, then reveal the working one step at a time.
EX 1Bit stuffing and destuffing
Show the bit stream actually transmitted (between the flags) for the data 0110111111111100, and show how the receiver recovers the data.
EX 2CRC by modulo-2 long division
Data 100100 is protected with the generator x³ + x² + 1. Find the CRC and the transmitted codeword, and show the receiver's check.
EX 3Hamming(7,4): encode, corrupt, correct
Encode the data 1011 with an even-parity Hamming code (positions numbered from the right). Then suppose bit 6 is flipped in transit; show how the receiver finds and corrects it.
EX 4Stop-and-wait vs sliding window on a long link
A 10 Mbps link is 2,000 km long (propagation speed 2 × 10⁸ m/s) and carries 10,000-bit frames. (a) Find the efficiency and throughput of stop-and-wait. (b) What window size gives 100% utilisation, and how many sequence-number bits do Go-Back-N and Selective Repeat need? (c) What efficiency does a window of 7 give?
Practice session
Work each problem on paper first, then check. Marking one solved earns 10 XP.0/5 solved
Q1
A character-oriented protocol uses FLAG and ESC bytes. Show the stuffed payload for the data bytes: A, FLAG, B, ESC, ESC, C.
Q2
Compute the Internet checksum of the 16-bit words 8F3C, A1B2 and 5E47 (hex). Then verify it as the receiver would.
Q3
Find the CRC for data 1101101 with generator x³ + x + 1, give the transmitted codeword, and check what the receiver sees if it receives 1111101111.
Q4
(a) How many redundant bits does a Hamming code need for 16 data bits? (b) Encode the 7-bit data 1100101 with even parity, positions numbered from the right.
Q5
A sender must deliver 7 packets with a window of 3. Every 4th packet it transmits is lost (ACKs are never lost). How many transmissions are needed with Go-Back-N, and how many with Selective Repeat?
Exam traps
Bit stuffing inserts a 0 after five consecutive 1s even when the next data bit is already 0 (0111110 → 01111100); the receiver removes the 0 after any five 1s.
For CRC, append r zeros where r is the generator's degree (number of generator bits − 1), not the number of generator bits.
Hamming positions: KPSC keys number positions from the right (rightmost = 1); some books number from the left, so the same code appears reversed. Check which convention the options use.
Go-Back-N sender window ≤ 2ᵐ − 1; Selective Repeat window ≤ 2ᵐ⁻¹. With m = 3: GBN 7, SR 4. Stop-and-wait is the case W = 1.
Single parity detects only an odd number of errors; 2-D parity catches all 1-, 2- and 3-bit errors but not every 4-bit pattern; CRC detects, Hamming corrects.
Window-size numericals differ in convention: some use 1 + 2a = (Tt + RTT)/Tt, others bandwidth × RTT ÷ frame size (ignoring Tt). For 64-byte frames, 64 kbps and an 80 ms RTT these give 11 and 10.
Quiz — KPSC style
12 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Framing, error control & flow control · Question 1 of 12✓ 0 · ✗ 0
What is the flag pattern used to delimit HDLC frames?
Past-paper questions on this topic
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.
Past papers · Question 1 of 18✓ 0 · ✗ 0
Modelled on QP 016/2018 · Higher Secondary School Teacher (Junior) Computer Science (1 Feb 2018), Q.39 (reworded)original paper (PDF)
Which bit-oriented protocol supports communication over both point-to-point and multipoint links?