最初の引数が RUSAGE_SELF または RUSAGE_CHILDREN で、もう 1 つの引数がrusageという名前の構造体である 2 つの引数を取ります。この構造には、アクセスして値を与えることができる多くの要素がありますが、これらすべての要素は何を表しているのでしょうか?
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
void print_cpu_time()
{
struct rusage usage;
getrusage (RUSAGE_SELF, &usage);
printf ("CPU time: %ld.%06ld sec user, %ld.%06ld sec system\n",
usage.ru_utime.tv_sec, usage.ru_utime.tv_usec,
usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
}
int main()
{
print_cpu_time();
}
このプログラムは、ユーザー時間とシステム時間の値を表示します。
構造体の他の要素は何を表し、実際のプログラムでどのように使用できますか。アクセスしようとすると、構造体の他のすべての要素の値が 0 になります。では、それらを使用して 0 以外の値を取得するにはどうすればよいでしょうか?
編集: ru_inblock と ru_oublock の値を見つけるプログラムを作成しました。指定された入力に対して、ru_inblock の場合は 0、ru_oublock の場合は 8 として出力を与えています。これはなぜですか?コードは次のとおりです
#include <stdio.h>
#include <sys/resource.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
// a struct to read and write
struct person
{
int id;
char fname[20];
char lname[20];
};
int main ()
{
FILE *outfile;
char ch;
struct person Stu;
int r;
outfile = fopen ("student.dat", "w");
if (outfile == NULL)
{
fprintf(stderr, "\nError opened file\n");
exit (1);
}
do
{
printf("\nEnter Roll : ");
scanf("%d",&Stu.id);
scanf("%*c");
printf("Enter First Name : ");
scanf("%s",Stu.fname);
scanf("%*c");
printf("Enter Last Name : ");
scanf("%s",Stu.lname);
fwrite(&Stu,sizeof(Stu),1,outfile);
printf("\nDo you want to add another data (y/n) : ");
scanf("%*c");
ch = getchar();
}
while(ch=='y' || ch == 'Y');
if(fwrite != 0)
printf("contents to file written successfully !\n");
else
printf("error writing file !\n");
fclose (outfile);
outfile = fopen ("student.dat", "r");
if (outfile == NULL)
{
fprintf(stderr, "\nError opened file\n");
exit (1);
}
struct person input;
while(fread(&input, sizeof(struct person), 1, outfile))
printf ("id = %d name = %s %s\n", input.id,
input.fname, input.lname);
fclose (outfile);
struct rusage r_usage;
r=getrusage(RUSAGE_SELF,&r_usage);
printf("\n%d\n",r);
printf("Memory usage = %ld\n",r_usage.ru_maxrss);
printf("\ninput operations : %ld \n", r_usage.ru_inblock);
printf("\noutput operations : %ld \n", r_usage.ru_oublock);
return 0;
}