+/**
+ * shuber, 2008-04-24
+ *
+ * Little hack to let a process run only a specific amount of time. If process
+ * has not finished until that amount of time it is killed.
+ *
+ */
+
+
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
}
-int main(int argc, char** argv)
+int main(int argc, char* argv[], char* envp[])
{
bool argHelp=false;
bool argTime=false;
if( pid == 0)
{
char** newargv = (char**) malloc( sizeof(char*)*(argc-fiarg+1) );
- char* newenv[] = {NULL};
//Copy arguments
for( int i=fiarg; i<argc; i++)
//Execute child process
- execve(newargv[0], newargv, newenv);
+ execve(newargv[0], newargv, envp);
perror("execve");
return ERR_EXECVE;
}
//Nmb of loops=seconds
float loops=0;
//Status flags of child process
- int status, w;
+ int status;
+ //granularity
+ const int ussleep=25000;
- do
+ while(true)
{
//Kill the child
- if( loops >= maxtime )
+ if( loops*ussleep/1e6 >= maxtime )
{
if( argVerbose )
printf("Kill child...\n");
break;
}
- //Sleep a round
- sleep(1);
- loops++;
-
//Test for child
if( waitpid(-1, &status, WNOHANG | WUNTRACED | WCONTINUED) < 0 )
break;
- }while( !WIFEXITED(status) );
+ if( WIFEXITED(status) )
+ break;
+
+ //Sleep a round
+ usleep(ussleep);
+ loops++;
+
+ }
}