The Core Concept
Minitalk transmits text by breaking each character into its 8 binary bits and sending them one at a time. Each bit is represented by a different signal:- Bit 0 → Send SIGUSR1
- Bit 1 → Send SIGUSR2
Character to Binary Conversion
Let’s see how the character'A' is transmitted:
Minitalk transmits bits in MSB (Most Significant Bit) first order, meaning the leftmost bit is sent first. This is the standard for network protocols.
Client: Bit Extraction and Transmission
Thesend_message function in client.c:42-68 handles the bit-by-bit transmission:
Understanding the Bit Extraction
The key line isclient.c:53:
Step-by-Step Example: Extracting bits from 'A' (65)
Step-by-Step Example: Extracting bits from 'A' (65)
Starting value: Iteration 2 (Iteration 3 (And so on for all 8 bits…
str[i] = 65 (binary: 01000001)Iteration 1 (bits = 7):bits = 6):bits = 5):Right shift operator (
>>): Shifts all bits to the right by N positionsBitwise AND (&): Compares bits; & 1 isolates the rightmost bitCombined, (value >> n) & 1 extracts the bit at position n from the right.Server: Bit Reconstruction
The server’shandle_sigusr function (server.c:16-32) reconstructs characters from incoming bits:
Understanding Bit Reconstruction
Two key operations happen inserver.c:21-24:
For SIGUSR1 (bit 0):
Step-by-Step Example: Reconstructing 'A' (65)
Step-by-Step Example: Reconstructing 'A' (65)
Goal: Receive Signal 2: SIGUSR2 (bit 1)Signal 3: SIGUSR1 (bit 0)Signal 4-7: SIGUSR1 (bits 0, 0, 0, 0)Signal 8: SIGUSR1 (bit 0)Signal 9: SIGUSR2 (bit 1)
01000001 and reconstruct the value 65Initial state: c = 0b00000000, i = 0Signal 1: SIGUSR1 (bit 0)Timing: The usleep(700) Mystery
Inclient.c:63, there’s a critical delay:
Timing Considerations
700 microseconds is a balance between:
- Reliability: High enough to prevent signal loss
- Performance: Low enough to maintain reasonable speed
Did you know? The optimal delay can vary by system. On faster systems with lower latency, you might get away with
usleep(500). On slower or heavily loaded systems, you might need usleep(1000) or more.Complete Transmission Example
Let’s trace the full transmission of the string"Hi":
Character ‘H’ (ASCII 72 = 0b01001000)
Character ‘i’ (ASCII 105 = 0b01101001)
"Hi"
Key Takeaways
- Bit extraction:
(value >> position) & 1isolates individual bits - Bit reconstruction:
(accumulator << 1) | bitbuilds the character - MSB first: Transmitting from bit 7 to bit 0 is standard
- Timing matters:
usleep(700)prevents signal loss - 8 bits = 1 byte: Each character requires exactly 8 signal transmissions
This binary transmission method can send any 8-bit data, not just printable ASCII characters. You could transmit binary files, Unicode (UTF-8 encoded), or any other byte stream using the same technique.