/****************************************************************** * Created: Josep Valls 20080111 * Modified: Josep Valls 20080124 * Timer assignment for CMPS105/Winter2008 * Practice the use of fork, system and gettimeofday ******************************************************************/ #include #include int main(int argc, char *argv[]) { pid_t pid; int retval; if (argc<2){ printf("No command nor parameters were specified.\n"); exit(0); } //Start timer struct timeval *run_time; time_t elapsed_sec; long elapsed_usec; gettimeofday(run_time,NULL); elapsed_sec=run_time->tv_sec; elapsed_usec=run_time->tv_usec; if (argc>2){ //Execute the command with its parameters printf("Running (execvp): %s\n",argv[1]); pid=fork(); if(pid==0) execvp(argv[1],&argv[1]); else wait(&retval); }else{ //Execute the command without any parameters printf("Running (system): %s\n",argv[1]); system(argv[1]); } //Compute timer gettimeofday(run_time,NULL); elapsed_sec=run_time->tv_sec-elapsed_sec; elapsed_usec=run_time->tv_usec-elapsed_usec; //Modified 20080124: Proper timming representation printf("Process completed in %d microsecs.\n",elapsed_usec+(elapsed_sec*1000000)); }