English 中文(简体)
Process Resources
  • 时间:2024-11-05

Process Resources


Previous Page Next Page  

The process needs certain resources such as CPU and memory to perform the tasks. Now we will look into the related commands and system calls to know the information on resource utipzation and monitoring. Also there are certain pmits by default for each process on the resources, and if required the pmits can be enhanced to accommodate the apppcation requirements.

Following are the essential system or process resources information using commands −

The top command

$ top

The top command continuously displays the usage of system resources. If any process puts the system in some kind of hang state (consuming more of CPU or Memory) it is possible to note the process information and take appropriate action (such as kilpng the related process).

The ps command

$ ps

The ps command provides information about all the running processes. This helps to monitor and control the processes.

The vmstat command

$ vmstat

The vmstat command reports the statistics of virtual memory subsystem. It reports the information of processes (waiting to run, sleeping, runnable processes, etc.), memory (virtual memory information such as free, used, etc.), swap area, IO devices, system information (number of interrupts, context switches) and CPU (user, system and idle time).

The lsof command

$ lsof

The lsof command prints the pst of open files of all the current running processes, including system processes.

The getconf command

$ getconf –a

The getconf command displays the system configuration variables information.

Now, let us take a look at the related system calls.

    System call getrusage(), which provides information on system resource usage.

    System calls related to accessing and setting resource pmits viz., getrpmit(), setrpmit(), prpmit().

System Resource Usage Call

#include <sys/time.h>
#include <sys/resource.h>

int getrusage(int who, struct rusage *usage);

The system call getrusage() returns the information on the system resource usage. This can include information on self, children, or calpng thread using flags RUSAGE_SELF, RUSAGE_CHILDREN, RUSAGE_THREAD for the “who” variable. After the call, it returns the information in the structure rusage.

This call would return “0” on success and “-1” on failure.

Let us look at the following sample program.

/* Filename: sysinfo_getrusage.c */

#include<stdio.h>
#include<sys/time.h>
#include<sys/resource.h>

void main(void) {
   struct rusage res_usage;
   int retval;
   retval = getrusage(RUSAGE_SELF, &res_usage);
   if (retval == -1) {
      perror("getrusage error");
      return;
   }
   printf("Details of getrusage:
");
   printf("User CPU time (seconds) is %d
", (int)res_usage.ru_utime.tv_sec);
   printf("User CPU time (micro seconds) is %d
", (int)res_usage.ru_utime.tv_usec);
   printf("Maximum size of resident set (kb) is %ld
", res_usage.ru_maxrss);
   printf("Soft page faults (I/O not required) is %ld
", res_usage.ru_minflt);
   printf("Hard page faults (I/O not required) is %ld
", res_usage.ru_majflt);
   printf("Block input operations via file system is %ld
", res_usage.ru_inblock);
   printf("Block output operations via file system is %ld
", res_usage.ru_oublock);
   printf("Voluntary context switches are %ld
", res_usage.ru_nvcsw);
   printf("Involuntary context switches are %ld
", res_usage.ru_nivcsw);
   return;
}

Compilation and Execution Steps

Details of getrusage:
User CPU time (seconds) is 0
User CPU time (micro seconds) is 0
Maximum size of resident set (kb) is 364
Soft page faults (I/O not required) is 137
Hard page faults (I/O not required) is 0
Block input operations via file system is 0
Block output operations via file system is 0
Voluntary context switches are 0
Involuntary context switches are 1

Let us now look at the system calls related to accessing and setting resource pmits.

#include <sys/time.h>
#include <sys/resource.h>

int getrpmit(int resource, struct rpmit *rpm);
int setrpmit(int resource, const struct rpmit *rpm);
int prpmit(pid_t pid, int resource, const struct rpmit *new_pmit, struct rpmit *old_pmit);

The system call getrpmit() gets the resource pmits in structure rpmit by inputting the resource one needs such as RLIMIT_NOFILE, RLIMIT_NPROC, RLIMIT_STACK, etc.

The system call setrpmit() sets the resource pmits as mentioned in the rpmit structure as far as within the pmits.

The system call prpmit() is used for varius purposes, such as either for retrieving the current resource pmits or for updating the resource pmits to new values.

The structure rpmit contains two values −

    Soft pmit − Current pmit

    Hard pmit − Maximum pmit to which it can be extended.

RLIMIT_NOFILE − Returns the maximum number of file descriptors that can be opened by this process. For example, if it returns 1024, then the process has file descriptors from 0 to 1023.

RLIMIT_NPROC − Maximum number of processes that can be created for a user of that process.

RLIMIT_STACK − The maximum size in bytes of the stack segment for that process.

All these calls would return “0” on success and “-1” on failure.

Let us consider the following example where we are using getrpmit() system call.

/* Filename: sysinfo_getrpmit.c */

#include<stdio.h>
#include<sys/time.h>
#include<sys/resource.h>

void main(void) {
   struct rpmit res_pmit;
   int retval;
   int resources[] = {RLIMIT_NOFILE, RLIMIT_NPROC, RLIMIT_STACK};
   int max_res;
   int counter = 0;
   printf("Details of resource pmits for NOFILE, NPROC, STACK are as follows: 
");
   max_res = sizeof(resources)/sizeof(int);
   while (counter < max_res) {
      retval = getrpmit(resources[counter], &res_pmit);
      if (retval == -1) {
         perror("getrpmit error");
         return;
      }
      printf("Soft Limit is %ld
", res_pmit.rpm_cur);
      printf("Hard Limit (ceipng) is %ld
", res_pmit.rpm_max);
      counter++;
   }
   return;
}

Compilation and Execution Steps

Details of resource pmits for NOFILE, NPROC, STACK are as follows: 
Soft Limit is 516
Hard Limit (ceipng) is 516
Soft Limit is 256
Hard Limit (ceipng) is 256
Soft Limit is 33554432
Hard Limit (ceipng) is 33554432

Let us consider another example with getrpmit() system call but now with prpmit() system call.

/* Filename: sysinfo_prpmit.c */

#include<stdio.h>
#include<unistd.h>
#include<sys/time.h>
#include<sys/resource.h>

void main(void) {
   struct rpmit res_pmit;
   int retval;
   int resources[] = {RLIMIT_NOFILE, RLIMIT_NPROC, RLIMIT_STACK};
   int max_res;
   int counter = 0;
   printf("Details of resource pmits for NOFILE, NPROC, STACK using prpmit are as follows: 
");
   max_res = sizeof(resources)/sizeof(int);
   while (counter < max_res) {
      retval = prpmit(getpid(), resources[counter], NULL, &res_pmit);
      if (retval == -1) {
         perror("prpmit error");
         return;
      }
      printf("Soft Limit is %ld
", res_pmit.rpm_cur);
      printf("Hard Limit (ceipng) is %ld
", res_pmit.rpm_max);
      counter++;
   }
   return;
}

Compilation and Execution Steps

Details of resource pmits for NOFILE, NPROC, STACK using prpmit are as follows: 
Soft Limit is 516
Hard Limit (ceipng) is 516
Soft Limit is 256
Hard Limit (ceipng) is 256
Soft Limit is 33554432
Hard Limit (ceipng) is 33554432
Advertisements