Open System Services Programmer's Guide
Example 29 Creating an OSS Process Using tdm_fork() and tdm_execvep()
#include <tdmext.h> /* Note 1 */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
/* Set defaults for input and output structures. Note \2 */
struct process_extension
procext = DEFAULT_PROCESS_EXTENSION;
struct process_extension_results
procresults = DEFAULT_PROCESS_EXTENSION_RESULTS;
extern char **environ;
char *argv[10];
int main(void)
{
char buf[4096];
pid_t pid;
int status, cnt;
char *ptr;
/* Get parameters for the child process from the command line. */
printf("%% "); /* print prompt (printf requires %% to print %) */
while (fgets(buf, 4096, stdin) != NULL) {
ptr = buf;
buf[strlen(buf) - 1] = '\0'; /* replace newline with NULL */
/* Build the parameter list for the child process. */
argv[0] = buf;
cnt = 1;
while(*ptr) {
if(*ptr == ' ') {
*ptr = '\0';
argv[cnt++] = ptr + 1; /* one blank between parameters */
}
ptr++;
}
argv[cnt] = NULL;
/* Request system-generated process name and create child process. */
procext.pe_name_options = _TPC_GENERATE_NAME;
if ( (pid = tdm_fork(&procext, &procresults)) < 0 ) {
/* If tdm_fork() fails, print diagnostic messages. */
fprintf(stderr, "pid = %o\n", pid);
fprintf(stderr, "procresults.pr_len = %o\n",\
procresults.pr_len);
fprintf(stderr, "procresults.pr_pid = %o\n",
procresults.pr_pid);
fprintf(stderr, "procresults.pr_errno = %o\n",
procresults.pr_errno);
fprintf(stderr, "procresults.pr_TPCerror = %o\n",
procresults.pr_TPCerror);
fprintf(stderr, "procresults.pr_TPCdetail = \
%o\n",procresults.pr_TPCdetail);
fprintf(stderr, "errno = %d\n", errno);
perror("tdm_fork error");
/* Pass values for the child process to execvep().*/
120 Managing Processes