カーネルの勉強中なのですが、下記Cのコードを実行すると、値がマイナスとなってしまいます。CPU利用率がマイナスってどういうことでしょうか。
C
1 21 double foo() 2 22 { 3 23 char *buf[1024]; // 文字列"cpu"の部分の入力用 4 24 int usr, nice, sys; 5 25 double wd; 6 26 clock_t now; 7 27 struct tms wtms; 8 28 9 29 // 演算に使用されたTick値を取得 10 30 FILE *infile = fopen( "/proc/stat", "r" ); 11 31 if ( NULL == infile ) return 0.0; 12 32 fscanf( infile, "%s %d %d %d", buf, &usr, &nice, &sys ); 13 33 fclose( infile ); 14 34 15 35 // 現在の時刻を取得 16 36 now = times( &wtms ); 17 37 18 38 // CPU利用率を算出 19 39 wd = ( (double)( usr + nice + sys - old_use ) / ( now - old_time ) ); 20 40 21 41 // 取得したTick値、時刻を退避 22 42 old_use = usr + nice + sys; 23 43 old_time = now; 24 44 25 45 26 46 return wd; 27 47 }
C
18 int main(void) 2 9 { 3 10 double wd; 4 11 double foo(); 5 12 6 13 wd = foo(); 7 14 8 15 printf("値は%d" , wd); 9 16 return 0; 10 17 }
main()のprintfの結果の出力:-111615712
回答1件
あなたの回答
tips
プレビュー