Monitoring and killing a process

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
#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
I see fork() in the Windows Programming forum? Shouldn't you be posting in the Unix/Linux forum?
Hello,
You are right, sorry I have picked the wrong forum, I should maybe move it to the C++ forum.
Topic archived. No new replies allowed.