進程需要一定的資源,如CPU和記憶體來執行任務。 現在我們將查看相關的命令和系統調用來瞭解資源利用和監視的資訊。 此外,資源上的每個過程在默認情況下都有一定的限制,如果需要,可以增加限制以適應應用需求。
以下是使用命令的基本系統或過程資源資訊 -
top命令
zaixian$ top
top
命令不斷顯示系統資源的使用情況。 如果任何進程使系統處於某種掛起狀態(消耗更多的CPU或記憶體),則可能會記錄進程資訊並採取相應的措施(例如,殺死相關進程)。
ps命令
zaixian$ ps
ps
命令提供有關所有正在運行的進程的資訊。 這有助於監視和控制過程。
vmstat命令
zaixian$ vmstat
vmstat
命令報告虛擬記憶體子系統的統計資訊。 它報告進程(等待運行,睡眠,可運行進程等),記憶體(虛擬記憶體資訊,如空閒,已使用等),交換區域,IO設備,系統資訊(中斷數目,上下文切換 )和CPU(用戶,系統和空閒時間)。
lsof命令
zaixian$ lsof
lsof
命令列印所有當前正在運行的進程(包括系統進程)的打開檔列表。
getconf命令
zaixian$ getconf
getconf
命令顯示系統配置變數資訊。現在,讓我們看看相關的系統調用。
- 系統調用
getrusage()
,它提供有關系統資源使用情況的資訊。 - 與訪問和設置資源限制(即
getrlimit()
,setrlimit()
,prlimit()
)有關的系統調用。
系統資源使用調用
參考以下代碼 -
#include <sys/time.h>
#include <sys/resource.h>
int getrusage(int who, struct rusage *usage);
系統調用getrusage()
返回有關系統資源使用情況的資訊。 這可以包括關於自身,子進程或調用線程的資訊,使用標誌RUSAGE_SELF
,RUSAGE_CHILDREN
,RUSAGE_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_NOFILE
,RLIMIT_NPROC
,RLIMIT_STACK
等等)來獲取結構rlimit
中的資源限制。
系統調用setrlimit()
盡可能地在rlimit
結構中設置資源限制。
系統調用prlimit()
用於檢索當前資源限制或將資源限制更新為新值。
結構rlimit
包含兩個值 -
- 軟限制 - 當前限制
- 硬限制 - 可以擴展的最大限制。
RLIMIT_NOFILE - 返回此進程可以打開的最大檔描述符數量。 例如,如果它返回1024
,那麼該進程具有從0
到1023
的檔描述符。
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