1

We use getrusage() system call to find different values of resources it takes two arguments in which the first argument is RUSAGE_SELF or RUSAGE_CHILDREN, the other argument is a structure named rusage. This structure has many elements which can be accessed and give values but what does all of these elements represent?

#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();
} 

This program shows values of user time and system time.

What do the other elements of the structure represent and how can they be used in real-life programs, like I am getting value 0 for all other elements of structure if I am trying to access them. So how can I use them to get a value other than 0?

EDIT : I have written a program to find the value of ru_inblock and ru_oublock. It is giving the output as 0 for ru_inblock and 8 for ru_oublock for any input given. Why is this so? The code is as follow

#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; 
} 
codeczar
  • 273
  • 1
  • 8
  • 22
  • 1
    It's literally explained in the [man page](http://man7.org/linux/man-pages/man2/getrusage.2.html). What exactly is unclear to you? `like I am getting value 0 for all other elements of structure if I am trying to acces them` - please show the code. – KamilCuk Mar 20 '20 at 11:33
  • @KamilCuk if I add these lines in the above code its output is 0 for both `printf("\ninput operations : %ld \n", usage.ru_inblock); printf("\noutput operations : %ld \n", usage.ru_oublock);` What else can I add in the code so that ru_inblock and ru_oublock gives a value other than 0 – codeczar Mar 20 '20 at 18:10
  • @KamilCuk also for other elements of the structure do I need to call a different function or use fork() or what should I use to explore to see the use of getrusage() – codeczar Mar 20 '20 at 18:13
  • I guess do some actual some block operations for `inblock` and `oublock` to increment. I don't... what other elements of the structure? – KamilCuk Mar 20 '20 at 18:19
  • what are block operations can you give some examples – codeczar Mar 20 '20 at 18:20
  • 1
    ex. [`write()`](http://man7.org/linux/man-pages/man2/write.2.html). `read()`. Like any input output that are done on block device. – KamilCuk Mar 20 '20 at 18:21
  • Code speaks 1000 words. Please show the code. To what file descriptor did you `write()` to? To which `FILE` pointer did you `fwrite()` to? What output and from where? What is a "correct" output? What would be a "incorrect" output? – KamilCuk Apr 10 '20 at 20:08
  • @KamilCuk i have added a code for fwrite and fread can you suggest what changes should i make to get output as no of inputs or no of outputs – codeczar Apr 16 '20 at 17:50

0 Answers0