当前位置: 首页 > 教程 > 进程通信 >

进程资源

精华
小牛编辑
146浏览
2023-03-14

进程需要一定的资源,如CPU和内存来执行任务。 现在我们将查看相关的命令和系统调用来了解资源利用和监视的信息。 此外,资源上的每个过程在默认情况下都有一定的限制,如果需要,可以增加限制以适应应用需求。

以下是使用命令的基本系统或过程资源信息 -

top命令

yiibai$ top

top命令不断显示系统资源的使用情况。 如果任何进程使系统处于某种挂起状态(消耗更多的CPU或内存),则可能会记录进程信息并采取相应的措施(例如,杀死相关进程)。

ps命令

yiibai$ ps

ps命令提供有关所有正在运行的进程的信息。 这有助于监视和控制过程。

vmstat命令

yiibai$ vmstat

vmstat命令报告虚拟内存子系统的统计信息。 它报告进程(等待运行,睡眠,可运行进程等),内存(虚拟内存信息,如空闲,已使用等),交换区域,IO设备,系统信息(中断数目,上下文切换 )和CPU(用户,系统和空闲时间)。

lsof命令

yiibai$ lsof

lsof命令打印所有当前正在运行的进程(包括系统进程)的打开文件列表。

getconf命令

yiibai$ getconf

getconf命令显示系统配置变量信息。现在,让我们看看相关的系统调用。

  • 系统调用getrusage(),它提供有关系统资源使用情况的信息。
  • 与访问和设置资源限制(即getrlimit()setrlimit()prlimit())有关的系统调用。

系统资源使用调用

参考以下代码 -

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

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

系统调用getrusage()返回有关系统资源使用情况的信息。 这可以包括关于自身,子进程或调用线程的信息,使用标志RUSAGE_SELFRUSAGE_CHILDRENRUSAGE_THREAD来表示“who”变量。 通话结束后,它返回结构中的信息。

这个调用会在成功时返回“0”,在失败时返回“-1”

让我们看看下面的示例程序。

文件: 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:\n");
   printf("User CPU time (seconds) is %d\n", (int)res_usage.ru_utime.tv_sec);
   printf("User CPU time (micro seconds) is %d\n", (int)res_usage.ru_utime.tv_usec);
   printf("Maximum size of resident set (kb) is %ld\n", res_usage.ru_maxrss);
   printf("Soft page faults (I/O not required) is %ld\n", res_usage.ru_minflt);
   printf("Hard page faults (I/O not required) is %ld\n", res_usage.ru_majflt);
   printf("Block input operations via file system is %ld\n", res_usage.ru_inblock);
   printf("Block output operations via file system is %ld\n", res_usage.ru_oublock);
   printf("Voluntary context switches are %ld\n", res_usage.ru_nvcsw);
   printf("Involuntary context switches are %ld\n", res_usage.ru_nivcsw);
   return;
}

编译和执行后,得到以下结果 -

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

现在让我们看看访问和设置资源限制有关的系统调用。

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

int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);
int prlimit(pid_t pid, int resource, const struct rlimit *new_limit, struct rlimit *old_limit);

系统调用getrlimit()通过输入一个需要的资源(如RLIMIT_NOFILERLIMIT_NPROCRLIMIT_STACK等等)来获取结构rlimit中的资源限制。
系统调用setrlimit()尽可能地在rlimit结构中设置资源限制。
系统调用prlimit()用于检索当前资源限制或将资源限制更新为新值。

结构rlimit包含两个值 -

  • 软限制 - 当前限制
  • 硬限制 - 可以扩展的最大限制。

RLIMIT_NOFILE - 返回此进程可以打开的最大文件描述符数量。 例如,如果它返回1024,那么该进程具有从01023的文件描述符。
RLIMIT_NPROC - 可以为该进程的用户创建的最大进程数。
RLIMIT_STACK - 该进程的堆栈段的最大字节数。

所有这些调用都会在成功时返回“0”,在失败时返回“-1”

让我们看看使用getrlimit()系统调用的例子。文件: sysinfo_getrlimit.c -

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

void main(void) {
   struct rlimit res_limit;
   int retval;
   int resources[] = {RLIMIT_NOFILE, RLIMIT_NPROC, RLIMIT_STACK};
   int max_res;
   int counter = 0;
   printf("Details of resource limits for NOFILE, NPROC, STACK are as follows: \n");
   max_res = sizeof(resources)/sizeof(int);
   while (counter < max_res) {
      retval = getrlimit(resources[counter], &res_limit);
      if (retval == -1) {
         perror("getrlimit error");
         return;
      }
      printf("Soft Limit is %ld\n", res_limit.rlim_cur);
      printf("Hard Limit (ceiling) is %ld\n", res_limit.rlim_max);
      counter++;
   }
   return;
}

编译和执行后,得到以下结果 -

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

下面是getrlimit()系统调用的另一个例子,但现在用prlimit()系统调用。
文件:sysinfo_prlimit.c -

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

void main(void) {
   struct rlimit res_limit;
   int retval;
   int resources[] = {RLIMIT_NOFILE, RLIMIT_NPROC, RLIMIT_STACK};
   int max_res;
   int counter = 0;
   printf("Details of resource limits for NOFILE, NPROC, STACK using prlimit are as follows: \n");
   max_res = sizeof(resources)/sizeof(int);
   while (counter < max_res) {
      retval = prlimit(getpid(), resources[counter], NULL, &res_limit);
      if (retval == -1) {
         perror("prlimit error");
         return;
      }
      printf("Soft Limit is %ld\n", res_limit.rlim_cur);
      printf("Hard Limit (ceiling) is %ld\n", res_limit.rlim_max);
      counter++;
   }
   return;
}

编译和执行后,得到以下结果 -

Details of resource limits for NOFILE, NPROC, STACK using prlimit are as follows: 
Soft Limit is 516
Hard Limit (ceiling) is 516
Soft Limit is 256
Hard Limit (ceiling) is 256
Soft Limit is 33554432
Hard Limit (ceiling) is 33554432