프로그램에 할당된 파일의 수, soft limit의 값
그리고 그것을 설정 가능한 최대 값인 hard limit 값 등등..을 설정할 수 있다.
직접 해보던 중
구글링을 해봤더니
이해가 잘 되는 답변이 있어서 예제와 출처로 남긴다
결과적으로 말하면 rlim_cur 값을 X로 지정하고
무한루프를 돌리면 X초 이후로 CPU Time Out이 발생한다.
https://www.go4expert.com/articles/getrlimit-setrlimit-control-resources-t27477/
rlimit의 구조
1 2 3 4 5 6 | struct rlimit { rlim_t rlim_cur; /* soft limit */ rlim_t rlim_max; /* Hard limit */ }; | cs |
사용 가능한 자원들
RLIMIT_CPU // 초 단위의 CPU Time
RLIMIT_FSIZE // 파일의 최대 크기
RLIMIT_DATA // 데이터의 최대 크기
RLIMIT_STACK // 스택의 최대 크기
RLIMIT_CORE // 최대 코어 파일 크기
RLIMIT_RSS // 최대 거주 집합 크기
RLIMIT_NPROC // 최대 프로세스의 수
RLIMIT_NOFILE // 최대로 열 수 있는 파일의 수
RLIMIT_AS // 가상 메모리 제한
예제 1 (sudo의 유무에 따라서 결과가 조금 다르다)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <sys/time.h> #include <sys/resource.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { struct rlimit rlim; // rilm_cur : soft limit 설정하고자 하는 값 // rlim_max : hard limit 설정 가능한 최대 값 // 열수 있는 파일의 수 // 이 프로세스가 열리는 파일의 최대수 = RLIMIT_NOFILE getrlimit(RLIMIT_NOFILE, &rlim); printf("RLIMIT_NOFILE (이 프로세스가 열리는 파일의 최대수) : %d\n", (int) RLIMIT_NOFILE); printf("현재 열 수 있는 파일의 수 : %d\n",(int) rlim.rlim_cur); printf("현재 열 수 있는 rlim_cur의 최대 치 : %d\n",(int) rlim.rlim_max); // 생성 가능한 프로세스의 최대수가 가능하다. getrlimit(RLIMIT_NPROC, &rlim); printf("RLIMIT_NOFILE (이 프로세스가 열리는 파일의 최대수) : %d\n", (int) RLIMIT_NOFILE); printf("현재 열 수 있는 파일의 수 : %d\n",(int) rlim.rlim_cur); printf("현재 열 수 있는 rlim_cur의 최대 치 : %d\n",(int) rlim.rlim_max); // 오픈 가능한 파일의 최대를 변경시킨다. // 최고 명령권자만 가능 rlim.rlim_cur += 1024; rlim.rlim_max += 1024; if(setrlimit(RLIMIT_NOFILE, &rlim) == -1){ return 0; } // 오픈 가능한 파일의 갯수를 출력한다. printf("Open file %d : %d\n", (int)rlim.rlim_cur, (int)rlim.rlim_max); return 0; } | cs |
예제 2 (출처)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <sys/resource.h> #include <sys/time.h> #include <unistd.h> #include<stdio.h> int main () { // Define and object of structure // rlimit. struct rlimit rl; rl.rlim_cur = 10; setrlimit(RLIMIT_CPU, &rl); // First get the time limit on CPU getrlimit (RLIMIT_CPU, &rl); printf("\n Default value is : %lld\n", (long long int)rl.rlim_cur); // Change the time limit rl.rlim_cur = 1; // Now call setrlimit() to set the // changed value. setrlimit (RLIMIT_CPU, &rl); // Again get the limit and check getrlimit (RLIMIT_CPU, &rl); printf("\n Default value now is : %lld\n", (long long int)rl.rlim_cur); // Simulate a long time consuming work while (1); return 0; } | cs |
예제3 (예제2와 비교)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <sys/resource.h> #include <sys/time.h> #include <unistd.h> #include<stdio.h> int main () { // Define and object of structure // rlimit. struct rlimit rl; rl.rlim_cur = 1; setrlimit(RLIMIT_CPU, &rl); // First get the time limit on CPU getrlimit (RLIMIT_CPU, &rl); printf("\n Default value is : %lld\n", (long long int)rl.rlim_cur); // Change the time limit rl.rlim_cur = 10; // Now call setrlimit() to set the // changed value. setrlimit (RLIMIT_CPU, &rl); // Again get the limit and check getrlimit (RLIMIT_CPU, &rl); printf("\n Default value now is : %lld\n", (long long int)rl.rlim_cur); // Simulate a long time consuming work while (1); return 0; } | cs |
※ 본 글은 개인 포트폴리오 혹은 공부용으로 사용하기 때문에, 무단 복사 유포는 금지하지만, 개인 공부 용도로는 얼마든지 사용하셔도 좋습니다
반응형
'C_C++ 프로그래밍 > TCP_IP' 카테고리의 다른 글
멀티쓰레드를 이용한 소켓 통신 (0) | 2019.06.04 |
---|---|
[TCP/IP] [OOB] OOB 통신에 대해서 알아보자 (0) | 2019.06.04 |
1:1 TCP 소켓 통신 프로그램 (0) | 2019.05.21 |
매우 단순한 TCP 프로그램 (0) | 2019.05.20 |
댓글