/* multiserver.h * Fork multiple times, create a socket for each. * The socket will accept accept()ed sockets from the main process. * After each finished request, it'll inform the main process. * - UziMonkey */ #ifndef MULTISERVER_H_INCLUDED #define MULTISERVER_H_INCLUDED #include #include #include struct multiserver_worker { struct timeval access_time; /* Last time accessed */ unsigned access_count; /* Number of times accessed */ pid_t pid; /* PID of worker process */ int sock[2]; /* socketpair for communication */ int free; /* Is the worker busy? */ /* Previous and next nodes in linked list */ struct multiserver_worker *prev, *next; }; struct multiserver_pool { /* Head and tail of linked list */ struct multiserver_worker *head, *tail; unsigned size; /* How many workers in the pool */ unsigned maximum_size; /* Maximum size pool can grow to */ unsigned free; /* How many free workers in the pool */ struct timeval idle_timeout; /* How long do idle extra workers linger? */ struct timeval access_time; /* Last time pool was accessed */ unsigned access_count; /* Number of times pool was accessed */ }; /* Create a multiserver pool. */ struct multiserver_pool multiserver_create_pool( unsigned size, /* Size of the pool */ unsigned maximum_size, /* Maximum size the pool can grow to */ struct timeval *idle_timeout, /* Time idle extra processes are killed */ void (*worker_func)(void) /* Function that does the work */ ); /* Send a socket to a multiserver pool. * One of three things will happen: * 1: A free process will answer the call * 2: There are no free processes, and a new process will be spawned to answer. * 3: The pool is at its maximum size. It's up to the caller to handle it. * * Returns: 0 if a process answered the socket, -1 if the pool was full */ int multiserver_send_job( struct multiserver_pool *pool, /* Pool to send to job to */ int socket /* Client socket */ ); /* Purge idle workers. Should be called by the controller every so often. * * Returns: Number of workers purged or -1 on error. */ int multiserver_purge( struct multiserver_pool *pool /* Pool to purge */ ); #endif /* MULTISERVER_H_INCLUDED */