Ping-pong
Eight request/reply exchanges between two tasks. Send waits until the other task replies. This is the byte-buffer form of the exchange demonstrated by the terminal’s ipc command.
Create(10, ping_pong);
#include "kern/syscall.h"
void pong() {
for (int round = 0; round < 8; ++round) {
tid_t sender;
char ping[4];
Receive(&sender, ping, sizeof(ping));
Reply(sender, "pong", 4);
}
Exit();
}
void ping_pong() {
tid_t server = Create(9, pong);
if (server < 0) { Exit(); return; }
for (int round = 0; round < 8; ++round) {
char reply[4];
Send(server, "ping", 4, reply, sizeof(reply));
// reply contains four bytes: p o n g.
// The server has replied before this line runs.
}
Exit();
}