pids.current는 pids.max보다 낮지만 Ubuntu VPS에서 스레드를 생성할 수 없습니다.

pids.current는 pids.max보다 낮지만 Ubuntu VPS에서 스레드를 생성할 수 없습니다.

질문 Ubuntu 18.04 LTS, 4vCore 및 8GB RAM을 갖춘 VPS가 있습니다. 이 VPS의 최대 nprocs를 정확히 알지 못합니다.

내가 한 일/시도한 일 현재 프로세스를 확인하기 위해 이 명령을 실행했는데 sudo cat /sys/fs/cgroup/pids/pids.current출력은 73이었습니다. 그런 다음 이 명령을 실행하면 sudo cat /sys/fs/cgroup/pids/pids.max400이 출력됩니다. 내 VPS에는 생성해야 하는 스레드가 300개 이상 있다고 가정합니다. 나는 생성할 수 있는 스레드 수를 테스트하기 위해 이 C 프로그램을 실행했습니다.

/* compile with:   gcc -pthread -o thread-limit thread-limit.c */
/* originally from: http://www.volano.com/linuxnotes.html */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>

#define MAX_THREADS 100000
#define PTHREAD_STACK_MIN 1*1024*1024*1024
int i;

void run(void) {
  sleep(60 * 60);
}

int main(int argc, char *argv[]) {
  int rc = 0;
  pthread_t thread[MAX_THREADS];
  pthread_attr_t thread_attr;

  pthread_attr_init(&thread_attr);
  pthread_attr_setstacksize(&thread_attr, PTHREAD_STACK_MIN);

  printf("Creating threads ...\n");
  for (i = 0; i < MAX_THREADS && rc == 0; i++) {
    rc = pthread_create(&(thread[i]), &thread_attr, (void *) &run, NULL);
    if (rc == 0) {
      pthread_detach(thread[i]);
      if ((i + 1) % 100 == 0)
    printf("%i threads so far ...\n", i + 1);
    }
    else
    {
      printf("Failed with return code %i creating thread %i (%s).\n",
         rc, i + 1, strerror(rc));

      // can we allocate memory?
      char *block = NULL;
      block = malloc(65545);
      if(block == NULL)
        printf("Malloc failed too :( \n");
      else
        printf("Malloc worked, hmmm\n");
    }
  }
sleep(60*60); // ctrl+c to exit; makes it easier to see mem use
  exit(0);
}

원천:스레드 제한.c

출력은 다음과 같습니다.

sebo@h2885222:~$ ./thread-limit
Creating threads ...
Failed with return code 11 creating thread 10 (Resource temporarily unavailable).
Malloc worked, hmmm

스레드를 10개만 더 만들 수 있어서 궁금합니다. 이 스레드 수를 초과하지 않도록 VPS를 설정하는 데 제한이 있습니까? 서류가 pids.max가짜인가요?

답변1

오류 코드 11은 다음과 같습니다 EAGAIN.

// /usr/include/asm-generic/errno-base.h
#define   EAGAIN          11      /* Try again */

에서 man pthread_create:

   EAGAIN A system-imposed limit on the number of threads was encountered.
          There  are  a  number of limits that may trigger this error: the
          RLIMIT_NPROC soft resource limit (set via  setrlimit(2)),  which
          limits  the  number of processes and threads for a real user ID,
          was reached; the kernel's system-wide limit  on  the  number  of
          processes and threads, /proc/sys/kernel/threads-max, was reached
          (see proc(5)); or the maximum  number  of  PIDs,  /proc/sys/ker‐
          nel/pid_max, was reached (see proc(5)).

이를 통해 현재 직면하고 있는 한계에 대한 아이디어를 얻을 수 있습니다.

관련 정보