A Strong Working Knowledge of K
Dennis Shasha
Courant Institute of Mathematical Sciences
Department of Computer Science
New York University
shasha@cs.nyu.edu
http://cs.nyu.edu/cs/faculty/shasha/index.html
Unit 4: Interprocess Communication
-
In K, the basic interprocess communication paradigm
consists of a client and a server (though each may take
on both roles).
-
One starts the server with a port number
on a well-known machine.
-
The client can then invoke procedures on that machine-port combination.
Topics
-
mechanics
-
examples
-
exercises
Mechanics of IPC
Example server.k
/ factorial
fact:{[n]
:[n > 0
:n*fact[n-1]
:1]
}
/ quadratic formula
quad:{[a;b;c]
x: ((-b) + _sqrt((b^2) - 4*a*c)) % (2*a)
y: ((-b) - _sqrt((b^2) - 4*a*c)) % (2*a)
:(x;y)
}
/ responds to messages one at a time;
/ buffering is automatic.
/ list[0] is the function. list[1] is the list of arguments.
.m.g:{[list]
args: list[1]
if[list[0] = `fact
:fact[args[0]]
]
if[list[0] = `quad
:quad[args[0]; args[1]; args[2]]
]
}
Example client.k
machine: _h / illustrates case where client and server
/ run on the same machine
machine: `
ret: (machine; 1234) 4: (`fact; ,5)
ret / should be 120
ret: (machine; 1234) 4: (`quad; (1; 4; 0))
ret / should be 0 -4
-
It is also possible to do asynchronous comunication
with a server using 3: instead of 4:.
See your reference card.