mfork_test.c

Go to the documentation of this file.
00001 
00007 /* $Id: mfork_test.c,v 1.10 2005/11/10 20:46:10 seymour Exp $ */
00008 /* $UTK_Copyright: $ */
00009 
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <signal.h>
00013 #include <sys/types.h>
00014 #include <unistd.h>
00015 
00016 #include "mfork.h"
00017 
00025 void
00026 child_action(void **args)
00027 {
00028   int foo;
00029 
00030   foo = *((int *)args[0]);
00031 
00032   printf("child [%d], foo = %d\n", (int)getpid(), foo);
00033 
00034   return;
00035 }
00036 
00047 void
00048 child_action_loop(void **args)
00049 {
00050   int foo;
00051 
00052   foo = *((int *)args[0]);
00053 
00054   while(1) {
00055     printf("child [%d], foo = %d\n", (int)getpid(), foo);
00056 
00057     if(!mfork_check_parent()) {
00058       ERRPRINTF("Parent died, so I am exiting\n");
00059       _exit(0);
00060     }
00061 
00062     sleep(1);
00063   }
00064 }
00065 
00073 void
00074 generic_cleanup(void **args)
00075 {
00076   int foo;
00077 
00078   foo = *((int *)args[0]);
00079 
00080   printf("child [%d], cleaning up.  arg was %d\n", (int)getpid(), foo);
00081 
00082   return;
00083 }
00084 
00091 void
00092 generic_pre(void **args)
00093 {
00094   int foo;
00095 
00096   foo = *((int *)args[0]);
00097 
00098   printf("child [%d], pre-fork.  arg was %d\n", (int)getpid(), foo);
00099 
00100   return;
00101 }
00102 
00109 void
00110 generic_post(void **args)
00111 {
00112   int foo;
00113 
00114   foo = *((int *)args[0]);
00115 
00116   printf("child [%d], post-fork.  arg was %d\n", (int)getpid(), foo);
00117 
00118   return;
00119 }
00120 
00128 void
00129 parent_loop(int foo)
00130 {
00131   int i;
00132 
00133   for(i=0;i<60;i++) {
00134     printf("parent [%d]...\n", (int)getpid());
00135     sleep(1);
00136   }
00137 }
00138 
00148 int main(int argc, char** argv)
00149 {
00150   int gs_server1 = 666, gs_server2 = 1234, gs_server3 = 777, gs_server4 = 222;
00151   void **child_args1, **child_args2, **child_args3, **child_args4;
00152   pid_t pid;
00153 
00154   child_args1 = (void **)malloc(sizeof(void*));
00155   child_args1[0] = &gs_server1;
00156 
00157   pid = mfork(child_action, 1, child_args1, generic_pre, 
00158     generic_post, generic_cleanup, 10);
00159 
00160   if(pid < 0) {
00161     fprintf(stderr,"Failed to fork workload manager process.\n");
00162     exit(-1);
00163   }
00164 
00165   child_args2 = (void **)malloc(sizeof(void*));
00166   child_args2[0] = &gs_server2;
00167 
00168   pid = mfork(child_action, 1, child_args2, generic_pre,
00169     generic_post, generic_cleanup, 10);
00170 
00171   if(pid < 0) {
00172     fprintf(stderr,"Failed to fork workload manager process.\n");
00173     exit(-1);
00174   }
00175 
00176   child_args3 = (void **)malloc(sizeof(void*));
00177   child_args3[0] = &gs_server3;
00178 
00179   pid = mfork(child_action, 1, child_args3, generic_pre, 
00180     generic_post, generic_cleanup, 10);
00181 
00182   if(pid < 0) {
00183     fprintf(stderr,"Failed to fork workload manager process.\n");
00184     exit(-1);
00185   }
00186 
00187   child_args4 = (void **)malloc(sizeof(void*));
00188   child_args4[0] = &gs_server4;
00189 
00190   pid = mfork(child_action_loop, -1, child_args4, NULL, NULL, NULL, 10);
00191 
00192   if(pid < 0) {
00193     fprintf(stderr,"Failed to fork workload manager process.\n");
00194     exit(-1);
00195   }
00196 
00197   printf("[%d] after forking last child...\n",(int)getpid());
00198   parent_loop(gs_server1);
00199 
00200   mfork_finalize();
00201   exit(EXIT_SUCCESS);
00202 }