user_time von Kindprozessen



  • 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 🙂

    #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


Anmelden zum Antworten