Clocking proccesses' time

B

babis85

Hello guys, i have this part of code and i want to compute the time of
processes A, B and C :

/* process A */
pid_t pid1, pid2, pid;
struct rusage ru1, ru2;

pid1 = fork();
if (pid != 0) {/* parent process */
pid2 = fork();
if (pid2 != 0) {/* parent process */
pid = waitpid(-1, &status, 0);
getrusage(RUSAGE_CHILDREN, &ru1);
waitpid(-1, &status, 0);
getrusage(_CHILDREN, &ru2);

if (pid == pid1) {
printf("B took time...\n", ru1.ru_utime.tv_sec);
printf("C took time...\n", ru2.ru_utime.tv_sec);

} else {
printf("C took time...\n", ru1.ru_utime.tv_sec);
printf("B took time...\n", ru2.ru_utime.tv_sec);
}
getrusage(RUSAGE_SELF, &ru);
printf("A took overall time...\n", ru.ru_utime.tv_sec);

} else {
/* process C */
execp(...);
}

} else {
/* process B */
execp(...);
}


The question is : Do i compute the time of each process successfully?
I get shorter time for proccess A than for its children... Isn't it
peculiar?Why?
I think that the time for proccess B is right, but i wonder if the next
call of getrusage adds c's time over b's. Do anyone know sth about
that?
Thanks a lot...
 
M

Michael Mair

Hello guys, i have this part of code and i want to compute the time of
processes A, B and C :

/* process A */
pid_t pid1, pid2, pid;
struct rusage ru1, ru2;

pid1 = fork();
The question is : Do i compute the time of each process successfully?
I get shorter time for proccess A than for its children... Isn't it
peculiar?Why?
I think that the time for proccess B is right, but i wonder if the next
call of getrusage adds c's time over b's. Do anyone know sth about
that?

fork(), thread, child processes etc. are not topical to comp.lang.c,
thus you cannot rely on getting the best/good advice on that here.
comp.unix.programmer or similar may be a better place to ask.

Cheers
Michael
 
J

Jack Klein

Hello guys, i have this part of code and i want to compute the time of
processes A, B and C :

The problem here is that your question is not about the C language,
but about non-standard, system-specific extensions.
/* process A */
pid_t pid1, pid2, pid;
struct rusage ru1, ru2;

pid1 = fork();
if (pid != 0) {/* parent process */
pid2 = fork();
if (pid2 != 0) {/* parent process */
pid = waitpid(-1, &status, 0);
getrusage(RUSAGE_CHILDREN, &ru1);
waitpid(-1, &status, 0);
getrusage(_CHILDREN, &ru2);

if (pid == pid1) {
printf("B took time...\n", ru1.ru_utime.tv_sec);
printf("C took time...\n", ru2.ru_utime.tv_sec);

} else {
printf("C took time...\n", ru1.ru_utime.tv_sec);
printf("B took time...\n", ru2.ru_utime.tv_sec);
}
getrusage(RUSAGE_SELF, &ru);
printf("A took overall time...\n", ru.ru_utime.tv_sec);

} else {
/* process C */
execp(...);
}

} else {
/* process B */
execp(...);
}


The question is : Do i compute the time of each process successfully?
I get shorter time for proccess A than for its children... Isn't it
peculiar?Why?
I think that the time for proccess B is right, but i wonder if the next
call of getrusage adds c's time over b's. Do anyone know sth about
that?
Thanks a lot...

None of fork(), waitpid(), getrusage(), or execp() are standard C
library functions, although I imagine they are all POSIX.

You need to ask in a platform specific group. Since you posted via
Google, the headers do not reveal your platform, so I can only suggest
either or
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top