Monitoring and killing a process that runs too long
Nov 29, 2011 at 5:06pm UTC
Dear all,
I have a very simple question: I run a process, and want to know how long it takes in order afterwards to be able to kill it if it runs too long.
I use the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void test(){
int pid = 0;
double lasttime=0;
int status = 0;
pid = fork();
if ( pid == 0 ){
system("echo HELLO" );
}
else {
int counter = 0;
while (1)
{
counter++;
if ( counter > lasttime ) lasttime = counter;
if (waitpid(pid, &status, 0) == pid) break ;
}
}
return ;
}
so in principle 'lasttime' should be somehow the duration of my process. But it is always 0 when I print it out at the end...
I must be doing something wrong, but I don't see what.
Any remark would be welcome !
Cheers,
Xavier
Nov 29, 2011 at 5:16pm UTC
waitpid will not return until a state change happens in the child process. I think the only possible return value from waitpid is pid (except for errors) so the code inside the loop will never run more than once. This is a unix question so you probablt get better answers asking in UNIX/Linux Programming.
Topic archived. No new replies allowed.