Linux getrusage()系统调用参数中rusage结构的不同值是什么?
我们使用getrusage()系统调用来查找不同的资源值。它有两个参数,第一个参数是RUSAGE\u SELF或RUSAGE\u CHILDREN,另一个参数是名为RUSAGE的结构。这个结构有许多元素,可以访问这些元素并给出值,但所有这些元素代表什么Linux getrusage()系统调用参数中rusage结构的不同值是什么?,linux,getrusage,Linux,Getrusage,我们使用getrusage()系统调用来查找不同的资源值。它有两个参数,第一个参数是RUSAGE\u SELF或RUSAGE\u CHILDREN,另一个参数是名为RUSAGE的结构。这个结构有许多元素,可以访问这些元素并给出值,但所有这些元素代表什么 #include <sys/resource.h> #include <sys/time.h> #include <unistd.h> void print_cpu_time() { struct rusa
#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();
}
#包括
#包括
#包括
无效打印\u cpu\u时间()
{
结构图像使用;
getrusage(RUSAGE_SELF和usage);
printf(“CPU时间:%ld.%06ld秒用户,%ld.%06ld秒系统\n”,
usage.ru_utime.tv_sec,usage.ru_utime.tv_usec,
usage.ru_stime.tv_sec,usage.ru_stime.tv_usec);
}
int main()
{
打印cpu时间();
}
此程序显示用户时间和系统时间的值
结构的其他元素代表什么?它们如何在实际程序中使用,例如,如果我试图访问它们,则结构的所有其他元素的值都为0。那么,如何使用它们来获取除0以外的值呢
编辑:我编写了一个程序来查找ru_inblock和ru_oublock的值。对于任何给定的输入,它将RUU inblock的输出设为0,RUU 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;
}
#包括
#包括
#包括
#包括
#包括
#包括
//读写结构
结构人
{
int-id;
char-fname[20];
字符名称[20];
};
int main()
{
文件*输出文件;
char ch;
结构人Stu;
INTR;
outfile=fopen(“学生数据”,“w”);
if(outfile==NULL)
{
fprintf(stderr,“\n打开文件时出错\n”);
出口(1);
}
做
{
printf(“\n输入辊:”);
scanf(“%d”和Stu.id);
scanf(“%*c”);
printf(“输入名字:”);
scanf(“%s”,Stu.fname);
scanf(“%*c”);
printf(“输入姓氏:”);
scanf(“%s”,Stu.lname);
fwrite(和Stu,sizeof(Stu),1,outfile);
printf(“\n是否要添加其他数据(y/n):”;
scanf(“%*c”);
ch=getchar();
}
而(ch='y'| ch='y');
如果(fwrite!=0)
printf(“成功写入文件的内容!\n”);
其他的
printf(“写入文件时出错!\n”);
fclose(输出文件);
outfile=fopen(“学生数据”,“r”);
if(outfile==NULL)
{
fprintf(stderr,“\n打开文件时出错\n”);
出口(1);
}
结构人输入;
while(fread(&input,sizeof(struct person),1,outfile))
printf(“id=%d name=%s%s\n”,input.id,
input.fname,input.lname);
fclose(输出文件);
结构rusage r_用法;
r=获取图像(图像自身和图像使用);
printf(“\n%d\n”,r);
printf(“内存使用率=%ld\n”,r\u usage.ru\u maxrss);
printf(“\n输入操作:%ld\n”,r\u usage.ru\u inblock);
printf(“\n输出操作:%ld\n”,r\u usage.ru\u oublock);
返回0;
}
这在本手册中有详细说明。你到底不清楚什么就像我试图访问结构的所有其他元素时得到的值为0一样
-请显示代码。@KamilCuk如果我在上面的代码中添加这些行,它的输出对于printf(“输入操作:%ld\n”,usage.ru\u inblock)都是0;printf(“\n输出操作:%ld\n”,usage.ru\u oublock)代码>我还可以在代码中添加什么,以便ru_inblock和ru_oublock给出除0@KamilCuk对于结构的其他元素,我是否需要调用不同的函数或使用fork(),或者我应该使用什么来探索getrusage()的用法我想对inblock
和oublock
执行一些实际的块操作以增加。我不。。。结构的其他元素是什么?例如<代码>读取()
。就像在块设备上完成的任何输入输出一样,它在。你到底不清楚什么就像我试图访问结构的所有其他元素时得到的值为0一样
-请显示代码。@KamilCuk如果我在上面的代码中添加这些行,它的输出对于printf(“输入操作:%ld\n”,usage.ru\u inblock)都是0;printf(“\n输出操作:%ld\n”,usage.ru\u oublock)代码>我还可以在代码中添加什么,以便ru_inblock和ru_oublock给出除0@KamilCuk对于结构的其他元素,我是否需要调用不同的函数或使用fork(),或者我应该使用什么来探索getrusage()的用法我想对inblock
和oublock
执行一些实际的块操作以增加。我不。。。结构的其他元素是什么?例如<代码>读取()
。就像在块设备上完成的任何输入输出一样。