Interprocess Communication (IPC) · Message Handlers (.z Namespace) · Practice Question
If Process A Communicates With Process B, What Order Do the .z Functions Execute?
Question
When process A opens a connection to process B and sends a synchronous message, what is the sequence of .z handler calls across both processes?
Solution
/ Process B (server) — set up handlers
\p 5010
.z.po:{[h] show "B: connection opened from handle ",string h}
.z.pg:{[x] show "B: received sync query: ", .Q.s1 x; value x} / evaluate and return the result
.z.pc:{[h] show "B: connection closed from handle ",string h}
/ Process A (client)
h: hopen `:localhost:5010 / triggers .z.po on B
h "2+2" / triggers .z.pg on B; A blocks waiting for result
hclose h / triggers .z.pc on B
/ Execution order:
/ 1. A: hopen (A blocks until the connection is established)
/ 2. B: .z.po fires (B records the new connection)
/ 3. A: h "2+2" sent (A blocks)
/ 4. B: .z.pg fires (B evaluates "2+2", returns 4 to A)
/ 5. A: receives result 4 (A unblocks)
/ 6. A: hclose h
/ 7. B: .z.pc fires (B cleans up the connection)Explanation
The sequence of handler calls across both processes, in order:
Connection establishment:
- A calls
hopen— A blocks until the connection is established (including authentication) - A's
hopenreturns an integer handle — A unblocks - B's
.z.pofires for the new connection — receiving B's own handle for it as its argument (.z.po:{[h]...})
(Steps 2 and 3 happen in separate processes, so either can complete first.)
Synchronous message:
4. A calls h "2+2" — A blocks waiting for B's response
5. B's .z.pg fires — receives the string "2+2" as its argument; the return value of .z.pg is sent back to A
6. A receives the result — A unblocks and the result is available
Connection close:
7. A calls hclose h (or the process dies)
8. B's .z.pc fires — receives the closed handle as its argument; .z.w is no longer meaningful at this point
For asynchronous messages (steps 4–6 only differ):
- A calls
neg[h] "expression"— A does NOT block and continues immediately - B's
.z.psfires (not.z.pg) — B evaluates the expression but does not send a return value - No blocking on A's side; no result returned to A
Key distinction: .z.pg handles synchronous (blocking) requests and its return value becomes the response. .z.ps handles asynchronous (one-way) messages and its return value is discarded.