Open System Services Programmer's Guide
/* find next available server */
do {
cpuindex = (++cpuindex == cpucount) ? 0 : cpuindex;
} while ((cpumask >> (15-cpuindex)) & 1 == 0);
/* pass socket fd to next available server */
fdadata.fd = fd;
if (sendmsg(serverfd[cpuindex], &fdmsghdr, 0) < 0)
{
fprintf(stderr, "sendmsg() call failed [errno %d]\n", errno);
exit(1);
}
close(fd);
} /* end while (1) */
}
slaunch1 creates an AF_UNIX socket pair to use for interprocess communication with each of
its servers. (Note that slaunch1 fails if the OSS local sockets server has not been started on your
node.)
slaunch1 uses fork() to create a duplicate of itself for each available processor, and then
starts a copy of sserver in that processor using a tdm_spawn() call. slaunch1 closes unneeded
file descriptors and then passes the file descriptor of a requester2 session that needs service to
the next available copy of sserver, cycling among all of the copies and spreading the workload
across all processors.
Each copy of sserver inherits an AF_UNIX socket to receive the file descriptor of a requester2
process connection and handles all work associated with that connection. Note that sserver is
an iterative static server, like the program in Example 43 (page 179), and handles multiple requestors.
The sserver process runs continuously in the background until it is idle for an arbitrary length of
time. sserver could easily be modified to run until killed by an operator.
Using two terminal sessions, sample dialog for the terminal launching slaunch1 might be:
/users/chris: ./slaunch1
slaunch1 starting in cpu 1
sserver spawn'ed in cpu 0
sserver [cpu 0] - starting
sserver spawn'ed in cpu 1
sserver [cpu 1] - starting
sserver spawn'ed in cpu 2
sserver [cpu 2] - starting
sserver spawn'ed in cpu 3
sserver [cpu 3] - starting
sserver [cpu 1] - new connection on fd 3
sserver [cpu 1] - message received on fd 3 = hello
sserver [cpu 1] - connection on fd 3 closed
sserver [cpu 2] - new connection on fd 3
sserver [cpu 2] - message received on fd 3 = hello from another requester
sserver [cpu 2] - connection on fd 3 closed
while the terminal running the copy of requester2 might show:
/users/chris: ./requester2
Requester starting
?hello
Read 7 bytes back from server
?
/users/chris: ./requester2
Requester starting
?hello from another requester
Read 29 bytes back from server
?
/users/chris:
146 Managing Processes