Sie sind nicht angemeldet.

1

09.05.2011, 20:47

Wie lange lief ein Kindprozess

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 :)

Quellcode

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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

2

10.05.2011, 09:21

#!/bin/hi @forum

Ok. Vielleicht Präzision sowie Compileroptimierungen und so ... DENN:

Quellcode

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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;

    if (times(&t1) == (clock_t)-1) { 
        perror("times1");
        exit(0);
    }

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

        forkpid = fork();

        if (forkpid == 0) {
            for (x = 0; x < 100000; x++) {
                FILE* f = fopen("test.txt","a");
                fwrite("x", 1, 1, f);
                fclose(f);
            } 
            exit(0);
        }
        else if (forkpid == -1) {
            printf("\nfork()Error\n");
        }
        else { 
            pids[i] = forkpid;
        }

    }

    printf("CLOCKS_PER_SEC=%f\n", (double)CLOCKS_PER_SEC);
    while ((pid = wait(&status)) > 0) {

        if (times(&t2) == (clock_t)-1){ 
            perror("times2");
            exit(0);
        }

        user_time = (float)(t2.tms_cutime - t1.tms_cutime);
        system_time = (float)(t2.tms_cstime - t1.tms_cstime);
        printf("prozessID:%d user_time:%.2f systemtime:%.2f (clocks)\n",pid, user_time, system_time);
    }

    return 0;
}


Macht bei mir mit

Quellcode

1
2
3
4
5
6
7
$ gcc -O0 -o testtime testtime.c 
$ ./testtime 
CLOCKS_PER_SEC=1000000.000000
prozessID:13519 user_time:22.00 systemtime:238.00 (clocks)
prozessID:13518 user_time:44.00 systemtime:478.00 (clocks)
prozessID:13517 user_time:71.00 systemtime:715.00 (clocks)
prozessID:13516 user_time:92.00 systemtime:956.00 (clocks)

Geht also.
http://www.dyle.org
IM-Account (Jabber!) sind auf meiner HP ...
There is no place like /home

http://www.gentooforum.de
http://www.gentoofreunde.org

<div>how to annoy a web developer?</span>