Functions

mfork_test.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include "mfork.h"
Include dependency graph for mfork_test.c:

Go to the source code of this file.

Functions

void child_action (void **args)
void child_action_loop (void **args)
void generic_cleanup (void **args)
void generic_pre (void **args)
void generic_post (void **args)
void parent_loop (int foo)
int main (int argc, char **argv)

Detailed Description

This file contains a tester for the mfork code.

Definition in file mfork_test.c.


Function Documentation

void child_action ( void **  args  ) 

Action to be executed periodically by mforked children. Just print out the pid and the argument.

Parameters:
args -- argument array

Definition at line 26 of file mfork_test.c.

{
  int foo;

  foo = *((int *)args[0]);

  printf("child [%d], foo = %d\n", (int)getpid(), foo);

  return;
}

Here is the caller graph for this function:

void child_action_loop ( void **  args  ) 

Action to be executed by mforked child, but not called periodically. Here we must manually check whether the parent is still alive using mfork_check_parent().

Just print out the pid and the argument.

Parameters:
args -- argument array

Definition at line 48 of file mfork_test.c.

{
  int foo;

  foo = *((int *)args[0]);

  while(1) {
    printf("child [%d], foo = %d\n", (int)getpid(), foo);

    if(!mfork_check_parent()) {
      ERRPRINTF("Parent died, so I am exiting\n");
      _exit(0);
    }

    sleep(1);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void generic_cleanup ( void **  args  ) 

Called by mfork when the parent dies to allow the children to do any cleanup necessary before terminating.

Parameters:
args -- arg array

Definition at line 74 of file mfork_test.c.

{
  int foo;

  foo = *((int *)args[0]);

  printf("child [%d], cleaning up.  arg was %d\n", (int)getpid(), foo);

  return;
}

Here is the caller graph for this function:

void generic_post ( void **  args  ) 

Called by mfork after forking (or re-forking) a child.

Parameters:
args -- arg array

Definition at line 110 of file mfork_test.c.

{
  int foo;

  foo = *((int *)args[0]);

  printf("child [%d], post-fork.  arg was %d\n", (int)getpid(), foo);

  return;
}

Here is the caller graph for this function:

void generic_pre ( void **  args  ) 

Called by mfork before forking (or re-forking) a child.

Parameters:
args -- arg array

Definition at line 92 of file mfork_test.c.

{
  int foo;

  foo = *((int *)args[0]);

  printf("child [%d], pre-fork.  arg was %d\n", (int)getpid(), foo);

  return;
}

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

Entry point for mfork tester.

Parameters:
argc -- arg count
argv -- arg array
Returns:
0 on success, -1 on failure.

Definition at line 148 of file mfork_test.c.

{
  int gs_server1 = 666, gs_server2 = 1234, gs_server3 = 777, gs_server4 = 222;
  void **child_args1, **child_args2, **child_args3, **child_args4;
  pid_t pid;

  child_args1 = (void **)malloc(sizeof(void*));
  child_args1[0] = &gs_server1;

  pid = mfork(child_action, 1, child_args1, generic_pre, 
    generic_post, generic_cleanup, 10);

  if(pid < 0) {
    fprintf(stderr,"Failed to fork workload manager process.\n");
    exit(-1);
  }

  child_args2 = (void **)malloc(sizeof(void*));
  child_args2[0] = &gs_server2;

  pid = mfork(child_action, 1, child_args2, generic_pre,
    generic_post, generic_cleanup, 10);

  if(pid < 0) {
    fprintf(stderr,"Failed to fork workload manager process.\n");
    exit(-1);
  }

  child_args3 = (void **)malloc(sizeof(void*));
  child_args3[0] = &gs_server3;

  pid = mfork(child_action, 1, child_args3, generic_pre, 
    generic_post, generic_cleanup, 10);

  if(pid < 0) {
    fprintf(stderr,"Failed to fork workload manager process.\n");
    exit(-1);
  }

  child_args4 = (void **)malloc(sizeof(void*));
  child_args4[0] = &gs_server4;

  pid = mfork(child_action_loop, -1, child_args4, NULL, NULL, NULL, 10);

  if(pid < 0) {
    fprintf(stderr,"Failed to fork workload manager process.\n");
    exit(-1);
  }

  printf("[%d] after forking last child...\n",(int)getpid());
  parent_loop(gs_server1);

  mfork_finalize();
  exit(EXIT_SUCCESS);
}

Here is the call graph for this function:

void parent_loop ( int  foo  ) 

Here the parent loops for a while and eventually exits, which should cause the children to terminate.

Parameters:
foo -- meaningless argument

Definition at line 129 of file mfork_test.c.

{
  int i;

  for(i=0;i<60;i++) {
    printf("parent [%d]...\n", (int)getpid());
    sleep(1);
  }
}

Here is the caller graph for this function: