Inter-process-communication-process-resources
プロセスリソース
プロセスは、タスクを実行するためにCPUやメモリなどの特定のリソースを必要とします。 次に、関連コマンドとシステムコールを調べて、リソースの使用率と監視に関する情報を確認します。 また、リソースの各プロセスにはデフォルトで特定の制限があり、必要に応じて制限を強化してアプリケーション要件に対応できます。
以下は、コマンドを使用した重要なシステムまたはプロセスのリソース情報です-
トップコマンド
$ top
topコマンドは、システムリソースの使用状況を継続的に表示します。 プロセスによってシステムが何らかのハング状態(CPUまたはメモリをより多く消費する)になった場合、プロセス情報を書き留め、適切なアクション(関連プロセスの強制終了など)を行うことができます。
psコマンド
$ ps
psコマンドは、実行中のすべてのプロセスに関する情報を提供します。 これは、プロセスの監視と制御に役立ちます。
vmstatコマンド
$ vmstat
vmstatコマンドは、仮想メモリサブシステムの統計情報を報告します。 プロセス(実行待機、スリープ、実行可能プロセスなど)、メモリ(空き、使用中などの仮想メモリ情報)、スワップ領域、IOデバイス、システム情報(割り込みの数、コンテキストスイッチなど)の情報を報告します。 )およびCPU(ユーザー、システム、およびアイドル時間)。
lsofコマンド
$ lsof
lsofコマンドは、システムプロセスを含む現在実行中のすべてのプロセスの開いているファイルのリストを出力します。
getconfコマンド
$ getconf –a
getconfコマンドは、システム構成変数情報を表示します。
次に、関連するシステムコールを見てみましょう。
- システムコールのgetrusage()。システムリソースの使用状況に関する情報を提供します。 *リソース制限へのアクセスと設定に関連するシステムコール。つまり、getrlimit()、setrlimit()、prlimit()。
システムリソース使用状況の呼び出し
#include <sys/time.h>
#include <sys/resource.h>
int getrusage(int who, struct rusage* usage);
システムコールgetrusage()は、システムリソースの使用状況に関する情報を返します。 これには、自己、子、または「who」変数のフラグRUSAGE_SELF、RUSAGE_CHILDREN、RUSAGE_THREADを使用した呼び出しスレッドに関する情報を含めることができます。 呼び出し後、構造rusageの情報を返します。
この呼び出しは、成功すると「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には2つの値が含まれています-
- ソフト制限-電流制限
- ハード制限-拡張可能な最大制限。
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