Open System Services Programmer's Guide
Example 38 Dynamic Launcher Program to Spawn Dynamic Server
/* dlaunch2.c
*
* Simple OSS example using OSS socket APIs for communication
* between requester and server.
*
* dlaunch2 (dynamic launcher 2) is an inetd-like program which
* accepts socket connections and spawn's a dserver process for
* each connection.
*
* dserver reads stdin and echoes to stdout. dlaunch2 and dserver
* write progress messages and error messages to stderr.
*
* dlaunch2 starts the dserver processes run on different cpus.
*/
#define _XOPEN_SOURCE_EXTENDED 1 /* needed for OSS sockets */
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <netinet/in.h>
#include <tdmext.h>
/* Guardian APIs */
#include <cextdecs.h(PROCESSHANDLE_GETMINE_, \
PROCESSHANDLE_DECOMPOSE_, \
PROCESSORSTATUS)>
short cpucount; /* cpucount - # cpu in system */
short cpuindex; /* cpuindex - current cpu for dserver */
short cpumask; /* cpumask - bit mask of available cpus /*
short cpu, phandle[10]; /* our cpu and process handle */
int newpid, fd, listenfd; /* process id, file descriptor,
* listening fd */
int readycount, bytesread; /* ready fd count, recv byte count */
struct sockaddr_in addr; /* socket address (family,addr,port) */
struct timeval timeval; /* timeout structure for select() call */
fd_set fdset; /* set of fds for select() call */
#define IDLE_PERIOD 180 /* 180 second timeout value */
extern char *environ[];
static char *childargv[] = {"dserver", NULL};
struct inheritance inherit;
struct process_extension peparm = DEFAULT_PROCESS_EXTENSION;
struct process_extension_results perslt =
DEFAULT_PROCESS_EXTENSION_RESULTS;
int main(int argc, char **argv)
{
/* get system cpu config */
cpumask = (short) PROCESSORSTATUS();
cpucount = (short) (PROCESSORSTATUS() >> 16);
cpuindex = -1;
/* get process handle and cpu number */
PROCESSHANDLE_GETMINE_(phandle);
PROCESSHANDLE_DECOMPOSE_(phandle, &cpu);
/* print startup message */
fprintf(stderr, "dlaunch2 starting in cpu %d\n", cpu);
/* create AF_INET stream socket */
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
140 Managing Processes