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)).

これにより、現在直面している限界に関するアイデアを得ることができます。

おすすめ記事