Linux user_time von Kindprozessen

user2580

Grünschnabel
Hallo ich möchte messen wie lange ein Kindprozess lief.

Um an die user_time und die system_time von einem Kindprozess zu kommen, errechne ich die Differenz vor und nach dem wait.

Eigentlich bin ich mir sicher, dass ich so die Zeiten bekommen.

Naja eigentlich, ich bekomme immer wieder 0.00 für user_time und system_time bei den Kindprozessen.

Also mach ich doch irgendwas falsch :)
C++:
#include <stdio.h>
#include <stdlib.h>

#include <time.h>
#include <sys/times.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/types.h>

int main(void){
 
 pid_t forkpid,pid,pids[5];
 int i = 0; 
 int x = 0;
 int status = 0;
 float user_time,system_time;
 static struct tms t1,t2;
 

  for(i = 0; i < 4; i++){
   
   forkpid = fork();

   if(forkpid == 0){   
     for(x = 0; x < 5000000; x++){
      fopen("test.txt","a");
     } 
     exit(0);
   }

   else if(forkpid == -1){
    printf("\nfork()Error\n");
   }

   else{ 
    pids[i] = forkpid;
   }

  }
  
  if (times(&t1)==(clock_t)-1){ 
    perror("times1");
    exit(0);
  }
 
  while ((pid=wait(&status))>0){
 
   if (times(&t2)==(clock_t)-1){ 
    perror("times2");
    exit(0);
   }
   
   user_time = ((float)(t2.tms_cutime - t1.tms_cutime)) / CLOCKS_PER_SEC;
   system_time = ((float)(t2.tms_cstime - t1.tms_cstime)) / CLOCKS_PER_SEC;
   printf("prozessID:%d user_time:%.2f systemtime:%.2f\n",pid, user_time, system_time);
  }

  return 0;
}

Ich bin für jeden Beitrag dankbar.

MFG
 
Hi.

Hast du denn mal die Manpage von times gelesen?
man times hat gesagt.:
The number of clock ticks per second can be obtained using:

sysconf(_SC_CLK_TCK); [...]

Note that clock(3) also returns a value of type clock_t, but this value is measured in units of CLOCKS_PER_SEC, not the clock ticks used by
times().
Gruß
 
Hi danke für die Hilfe,
aber leider verstehe ich nicht ganz wie ich damit raus finde wie lange ein Kindprozess lief.

Kannst du mir vielleicht ein Beispiel in Form von einem Codeschnipsel dazu zeigen.
 
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <sys/times.h>
#include <sys/wait.h>
#include <sys/types.h>

int main(void){
 
 pid_t forkpid,pid,pids[5];
 int i = 0; 
 int x = 0;
 int status = 0;
 float user_time,system_time;
 struct tms t1;
 struct tms t2;
 

  for(i = 0; i < 4; i++){
   
   forkpid = fork();

   if(forkpid == 0){   
     for(x = 0; x < 5000000; x++){
      fopen("test.txt","a");
     } 
     exit(0);
   }

   else if(forkpid == -1){
    printf("\nfork()Error\n");
   }

   else{ 
    pids[i] = forkpid;
   }

  }
  
  if (times(&t1)==(clock_t)-1){ 
    perror("times1");
    exit(0);
  }
 
  while ((pid=wait(&status))>0){
 
   if (times(&t2)==(clock_t)-1){ 
    perror("times2");
    exit(0);
   }
   
   user_time   = ((float)(t2.tms_cutime - t1.tms_cutime)) / sysconf(_SC_CLK_TCK);
   system_time = ((float)(t2.tms_cstime - t1.tms_cstime)) / sysconf(_SC_CLK_TCK);
   printf("prozessID:%d user_time:%.2f systemtime:%.2f\n",pid, user_time, system_time);
  }

  return 0;
}

Nur warum konnte ich nicht CLOCKS_PER_SEC nehmen?
 
Zurück