在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Linux/ 進程資源
命名管道
消息隊列
進程創(chuàng)建與終止
信號量
進程組,會話和作業(yè)控制
共享內(nèi)存
進程間通信簡介
子進程監(jiān)視
其他進程
覆蓋進程映像
進程信息
進程映像
內(nèi)存映射
相關(guān)系統(tǒng)調(diào)用(System V)
進程資源
System V & Posix
信號
進程間通信教程
管道

進程資源

進程需要一定的資源,如CPU和內(nèi)存來執(zhí)行任務(wù)。 現(xiàn)在我們將查看相關(guān)的命令和系統(tǒng)調(diào)用來了解資源利用和監(jiān)視的信息。 此外,資源上的每個過程在默認情況下都有一定的限制,如果需要,可以增加限制以適應(yīng)應(yīng)用需求。

以下是使用命令的基本系統(tǒng)或過程資源信息 -

top命令

yiibai$ top

top命令不斷顯示系統(tǒng)資源的使用情況。 如果任何進程使系統(tǒng)處于某種掛起狀態(tài)(消耗更多的CPU或內(nèi)存),則可能會記錄進程信息并采取相應(yīng)的措施(例如,殺死相關(guān)進程)。

ps命令

yiibai$ ps

ps命令提供有關(guān)所有正在運行的進程的信息。 這有助于監(jiān)視和控制過程。

vmstat命令

yiibai$ vmstat

vmstat命令報告虛擬內(nèi)存子系統(tǒng)的統(tǒng)計信息。 它報告進程(等待運行,睡眠,可運行進程等),內(nèi)存(虛擬內(nèi)存信息,如空閑,已使用等),交換區(qū)域,IO設(shè)備,系統(tǒng)信息(中斷數(shù)目,上下文切換 )和CPU(用戶,系統(tǒng)和空閑時間)。

lsof命令

yiibai$ lsof

lsof命令打印所有當前正在運行的進程(包括系統(tǒng)進程)的打開文件列表。

getconf命令

yiibai$ getconf

getconf命令顯示系統(tǒng)配置變量信息?,F(xiàn)在,讓我們看看相關(guān)的系統(tǒng)調(diào)用。

  • 系統(tǒng)調(diào)用getrusage(),它提供有關(guān)系統(tǒng)資源使用情況的信息。
  • 與訪問和設(shè)置資源限制(即getrlimit()setrlimit(),prlimit())有關(guān)的系統(tǒng)調(diào)用。

系統(tǒng)資源使用調(diào)用

參考以下代碼 -

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

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

系統(tǒng)調(diào)用getrusage()返回有關(guān)系統(tǒng)資源使用情況的信息。 這可以包括關(guān)于自身,子進程或調(diào)用線程的信息,使用標志RUSAGE_SELFRUSAGE_CHILDREN,RUSAGE_THREAD來表示“who”變量。 通話結(jié)束后,它返回結(jié)構(gòu)中的信息。

這個調(diào)用會在成功時返回“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;
}

編譯和執(zhí)行后,得到以下結(jié)果 -

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

現(xiàn)在讓我們看看訪問和設(shè)置資源限制有關(guān)的系統(tǒng)調(diào)用。

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

系統(tǒng)調(diào)用getrlimit()通過輸入一個需要的資源(如RLIMIT_NOFILE,RLIMIT_NPROCRLIMIT_STACK等等)來獲取結(jié)構(gòu)rlimit中的資源限制。
系統(tǒng)調(diào)用setrlimit()盡可能地在rlimit結(jié)構(gòu)中設(shè)置資源限制。
系統(tǒng)調(diào)用prlimit()用于檢索當前資源限制或?qū)①Y源限制更新為新值。

結(jié)構(gòu)rlimit包含兩個值 -

  • 軟限制 - 當前限制
  • 硬限制 - 可以擴展的最大限制。

RLIMIT_NOFILE - 返回此進程可以打開的最大文件描述符數(shù)量。 例如,如果它返回1024,那么該進程具有從01023的文件描述符。
RLIMIT_NPROC - 可以為該進程的用戶創(chuàng)建的最大進程數(shù)。
RLIMIT_STACK - 該進程的堆棧段的最大字節(jié)數(shù)。

所有這些調(diào)用都會在成功時返回“0”,在失敗時返回“-1”

讓我們看看使用getrlimit()系統(tǒng)調(diào)用的例子。文件: 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;
}

編譯和執(zhí)行后,得到以下結(jié)果 -

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()系統(tǒng)調(diào)用的另一個例子,但現(xiàn)在用prlimit()系統(tǒng)調(diào)用。
文件: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;
}

編譯和執(zhí)行后,得到以下結(jié)果 -

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

上一篇:進程信息下一篇:信號量