//------------------------------------------------------------------- // datetest.cpp // // This program uses the 'fork()', 'execve()' and 'waitpid()' // functions to automate the testing of our 'date' algorithm, // provided 'secs2day' is located in your current directory. // // compile using: $ g++ datetest.cpp -o datetest // execute using: $ ./datetest // // programmer: ALLAN CRUSE // written on: 23 FEB 2007 //------------------------------------------------------------------- #include // for time() #include // for fork(), execve() #include // for sprintf() #include // for waitpid() int main( int argc, char **argv ) { int max_seconds = time( NULL ); int secs_in_day = 60 * 60 * 24; int max_day = 1 + (max_seconds / secs_in_day); for (int day = 1; day <= max_day; day++) { int seconds = day * secs_in_day; int pid = fork(); if ( pid == 0 ) { char arg0[ ] = "secs2day"; char arg1[ 20 ] = {0}; char *argv[ ] = { arg0, arg1, 0 }; sprintf( arg1, "%d", seconds ); execve( arg0, argv, NULL ); } else waitpid( pid, NULL, 0 ); } }